您的位置:首页 > Web前端 > JQuery

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

2014-12-26 00:00 405 查看
<!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>

pageX() 属性是鼠标指针的位置,相对于文档的左边缘。

pageY() 属性是鼠标指针的位置,相对于文档的上边缘。

remove() 方法删除被选元素及其子元素。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: