jQuery鼠標經過圖片出現提示文字
<!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></pre>
pageX() 屬性是鼠標指針的位置,相對于文檔的左邊緣。
pageY() 屬性是鼠標指針的位置,相對于文檔的上邊緣。
remove() 方法刪除被選元素及其子元素。
![]()
本文由用戶 cdop 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!