效果:https:///keleyi/phtml/image/8.htm
這個效果常被稱為 tooltip 預覽:滑鼠移到縮圖上,就在旁邊彈出大圖並跟著滑鼠移動,移開立刻消失。做法是動態建一個絕對定位的 div,把連結的 href 當作大圖來源、title 當作說明文字,再用 pageX/pageY 決定位置。
程式碼裡有幾個細節值得注意:滑入時先把 this.title 存起來再清空,是為了避免瀏覽器原生的黃色提示框跳出來搶戲;移出時再還原。每次 mouseover 都先 $("#tooltip-keleyi-com").remove() 清掉舊節點,可以防止連續移動時堆疊出多個提示層。位置則同時綁在 mouseover 與 mousemove 上,才能做到「跟手」。
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>JQ實現鼠標經過顯示大圖效果-</title>
<script type="text/javascript" src="https:///keleyi/pmedia/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function () {
var x = 10;
var y = 20;
$("a.tooltip-keleyi-com").mouseover(function (e) {
this.myTitle = this.title;
this.title = "";
var imgTitle = this.myTitle ? "<br />" + this.myTitle + " 產品預覽圖" : "";
var tooltip_keleyi_com = "<div id='tooltip-keleyi-com'><img src='" + this.href + "' alt='產品預覽圖' width='765' height='574' />" + imgTitle + "</div>";
$("body").append(tooltip_keleyi_com);
$("#tooltip-k"+"eleyi-com").css({ "top": (e.pageY + y) + "px", "left": (e.pageX + x) + "px" }).show("fast");
}).mouseout(function (e) {
this.title = this.myTitle;
$("#tooltip-keleyi-com").remove();
}).mousemove(function (e) {
$("#tooltip-keley"+"i-com").css({ "top": (e.pageY + y) + "px", "left": (e.pageX + x) + "px" });
});
})
</script>
<style>
ul{margin: 30px auto; width:1440px;}
ul li{float: left; padding-right: 20px; list-style: none;}
ul li img{width: 77px; height: 57px; padding: 6px; border: 1px solid #ccc; background-color:#eee; -webkit-border-radius: 8px;}
#tooltip-keleyi-com{ position: absolute; background-color: #eee; border: 1px solid #999; width: 765px; height: 590px; -webkit-border-radius: 8px; font-family: "微軟雅黑"; padding: 20px; }
</style>
</head>
<body>
<div><ul>
<li><a href="https:///keleyi/phtml/image/img/bmw_m1_hood.jpg" title="1" class="tooltip-keleyi-com"><img src="https:///keleyi/phtml/image/img/bmw_m1_hood.jpg" alt="1" /></a></li>
<li><a href="https:///keleyi/phtml/image/img/bmw_m1_side.jpg" title="2" class="tooltip-keleyi-com"><img src="https:///keleyi/phtml/image/img/bmw_m1_side.jpg" alt="2" /></a></li>
<li><a href="https:///keleyi/phtml/image/img/bmw_m3_gt.jpg" title="3" class="tooltip-keleyi-com"><img src="https:///keleyi/phtml/image/img/bmw_m3_gt.jpg" alt="3" /></a></li>
<li><a href="https:///keleyi/phtml/image/img/corvette_pitstop.jpg" title="4" class="tooltip-keleyi-com"><img src="https:///keleyi/phtml/image/img/corvette_pitstop.jpg" alt="4" /></a></li>
</ul></div>
<div>提醒您:把光標移動到圖片上查看效果。</div>
</body>
</html>
程式碼中用到捕捉滑鼠位置的功能,請參考:https:///a/bjac/8p1g5yeq.htm
原文:https:///a/bjac/h12vjfyv.htm