$(function(){ var x = 10; var y = 20; var _windowwidth = $(window).width() * 0.8; var _windowheight = $(window).height(); $("a.tooltip").mouseover(function(e){ this.mytitle = this.title; this.myname = this.name; this.title = ""; var imgtitle = this.mytitle? "
" + this.mytitle : ""; var imgname = this.myname? "
" + this.myname : ""; var tooltip = "
产品预览图

"+imgname+"

"+imgtitle+"
<\/div>"; //创建 div 元素 $("body").append(tooltip); //把它追加到文档中 var _boxwidth = $('#tooltip').width(); var _boxheight = $('#tooltip').height(); var _eventx = e.pagex; var _eventy = e.pagey; var _width = 0; if (_boxwidth + _eventx > _windowwidth) { _width = _boxwidth + _eventx - _windowwidth - 20; } else { _width = 0; } $("#tooltip") .css({ "top": (e.pagey+y) + "px", "left": (e.pagex-_width) + "px" }); //设置x坐标和y坐标,并且显示 settimeout(function() {$('#tooltip').show();}, 200); }).mouseout(function(){ this.title = this.mytitle; $("#tooltip").remove(); //移除 }).mousemove(function(e){ var _boxwidth = $('#tooltip').width(); var _boxheight = $('#tooltip').height(); var _eventx = e.pagex; var _eventy = e.pagey; if (_boxwidth + _eventx > _windowwidth) { _width = _boxwidth + _eventx - _windowwidth - 20; } else { _width = 0; } $("#tooltip") .css({ "top": (e.pagey+y) + "px", "left": (e.pagex-_width) + "px" }); }); })