jQuery鼠标经过图片出现提示文字

原创 2016-11-08 10:47:54 393
摘要:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>reminder</title>
<script src="jquery.min.js" type="text/javascript"></script>
<style>
.box{
 position:absolute;
 border:1px solid #ccc;
 background:#333;
 padding:2px;
 color:#fff;
}
img{ border:#000 1px solid;}
</style>
</head>
  
<body>
<img src="11.jpg" />
<div class="content" style="display:none;">夏目友人帐:</div>
  
<script type="text/javascript">
$(function(){
 var x = 20;
 var y = 10;
 $("img").mouseover(function(e){ //当鼠标位于img上时执行下面的函数
 var title = $(".content").text(); // 定义变量box,获取content中的内容
 var box = "<div class='box'>"+title+"</div>"; // 定义变量box,获取要显示的内容
 $("body").append(box); //向页面中插入变量box引入的信息
 });
 $("img").mouseout(function(){ //当鼠标从img移出时执行
 $(".box").remove(); //清除页面加载的内容
 });
 $("img").mousemove(function(e){ //当鼠标在img上移动时
 $(".box").css({"top": (e.pageY+y) + "px","left": (e.pageX+x) + "px"}); //页面中插入内容box的位置为鼠标的位置,再加上偏移量
 });
})
  
</script>
</body>
</html>


发布手记

热门词条