jQuery實現(xiàn)鼠標(biāo)跟隨效果
更新時間:2017年02月20日 10:39:29 作者:前端工程師_錢成
本文主要分享了jQuery實現(xiàn)鼠標(biāo)跟隨效果的示例代碼。具有很好的參考價值,下面跟著小編一起來看下吧
所謂鼠標(biāo)跟隨,一般就是指鼠標(biāo)移到哪張圖片上,那該張圖片的放大圖片就會出現(xiàn),并且放大圖片會隨著鼠標(biāo)在該張圖片上移動而移動。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
*{
margin:0;
padding:0;
}
img{
border:none;
}
.box{
width:660px;
position: relative;
}
.box .mark{
position: absolute;
width: 400px;
height: 300px;
display: none;
}
.box .mark img{
width: 100%;
}
.box img{
width: 150px;
float: left;
margin:5px;
}
</style>
</head>
<body>
<div class="box" id="box">
<img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=e95708d565639d99576ae7cb00729334"
realImg="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=5328802dc943fc046e109f70359add0a" alt=""/>
<img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=9e5459a7c0098c27adf4bdd73889caa9"
realImg="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=846f4d1987765dc4cfd5a06fcdd2dcc1" alt=""/>
<img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=3cd1c8e301007f0c94850139ac79cb5a"
realImg="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=747bf3f7092ebd2b0bf9fcd27e28bbe5" alt=""/>
<img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=f391169b2cf678aa6fd253cf40d9821d"
realImg="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=fec8d2f20fad1f28d540337a831e89d0" alt=""/>
<div id="mark" class="mark"><img src="" alt=""/></div>
</div>
<script src="http://s0.kuaizhan.com/res/skin/js/lib/jquery-2.0.3.min.js"></script>
<script>
//1.鼠標(biāo)移入哪張圖片的時候,讓他對應(yīng)的大圖顯示;
//2.當(dāng)鼠標(biāo)在img中移動的時候,大圖跟著走;
var $box=$('.box');
var $aImg=$box.children('img');
var $mark=$('.mark');
var $offset=$box.offset();
$aImg.mouseover(function(){
//當(dāng)鼠標(biāo)移入每張圖片的時候,讓mark顯示,并且,讓mark里面的img標(biāo)簽,src屬性值為當(dāng)前這個圖片的realImg屬性上拿到的值;
$mark.show().find('img').attr('src',$(this).attr('realImg'));
});
$aImg.mousemove(function(e){
//拿鼠標(biāo)的x坐標(biāo),減去$box距離body的left位置;
var left= e.clientX-$offset.left+10;
var top= e.clientY-$offset.top+10;
$mark.css({left:left,top:top})
});
$aImg.mouseout(function(){
$mark.hide();
})
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
在Web項目中引入Jquery插件報錯的完美解決方案(圖解)
這篇文章主要介紹了在Web項目中引入Jquery插件報錯的完美解決方案的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
JQuery插件tablesorter表格排序?qū)崿F(xiàn)過程解析
這篇文章主要介紹了JQuery插件tablesorter表格排序?qū)崿F(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
jQuery實現(xiàn)網(wǎng)頁頂部固定導(dǎo)航效果代碼
這篇文章主要介紹了jQuery實現(xiàn)網(wǎng)頁頂部固定導(dǎo)航效果代碼,涉及jQuery響應(yīng)scroll事件動態(tài)操作頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下2015-12-12

