原生js仿淘寶網(wǎng)商品放大鏡效果
效果圖:(實(shí)例圖片由自己添加)

代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>仿淘寶放大鏡特效</title>
<style type="text/css">
*{margin: 0;padding: 0;}
#demo{width:350px;height:350px;border: 1px solid #000;position:relative;margin:100px;}
#smil_box{width: 350px;height: 350px;position:relative;}
#mask{width:175px;height: 175px;background:rgba(255,255,0,0.4);position:absolute;top:0;left:0;display:none;cursor:move;}
#big_box{width: 400px;height: 400px;position:absolute;top:0;left:360px;border: 1px solid #000;overflow:hidden;display:none;}
#big_box img{position:absolute;top:0;left:0;}
</style>
</head>
<body>
<div id="demo">
<div id="smil_box">
<img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=193662baf63738c68b594f3ec920769f" height="350" width="350" alt="">
<div id="mask"></div>
</div>
<div id="big_box">
<img src="http://www.qdfuns.com/misc.php?mod=attach&genre=editor&aid=3e8b3554eb90dd13fa0f82465ac6d382" height="800" width="800" alt="" id="big_img">
</div>
</div>
</body>
<script type="text/javascript">
(function(window){
function $(id){
return document.getElementById(id);
};
// 獲取對(duì)象
var demo = $("demo"),smilBox = $("smil_box"),mask = $("mask"),bigImg = $("big_img"),bigBox = $("big_box");
// smilBox的hover事件
smilBox.onmouseover = function(){
mask.style.display = "block";
bigBox.style.display = "block";
};
smilBox.onmouseout = function(){
mask.style.display = "none";
bigBox.style.display = "none";
};
// 鼠標(biāo)移動(dòng)事件
smilBox.onmousemove = function(event){
var event = event || window.event;
// 獲取鼠標(biāo)在頁(yè)面上的坐標(biāo)
var pageX = event.pageX || event.clientX + document.documentElement.scrollLeft;
var pageY = event.pageY || event.clientY + document.documentElement.scrollTop;
// mask的位置坐標(biāo)
var boxX = pageX - demo.offsetLeft;
var boxY = pageY - demo.offsetTop;
var maskX = boxX - mask.offsetWidth / 2;
var maskY = boxY - mask.offsetHeight / 2;
// 限制mask的移動(dòng)范圍
if( maskX < 0 ){
maskX = 0;
};
if( maskX > smilBox.offsetWidth - mask.offsetWidth){
maskX = smilBox.offsetWidth - mask.offsetWidth;
};
if( maskY < 0 ){
maskY = 0;
};
if( maskY > smilBox.offsetHeight - mask.offsetHeight){
maskY = smilBox.offsetHeight - mask.offsetHeight;
};
// 黃色遮罩層的位置坐標(biāo)
mask.style.top = maskY + "px";
mask.style.left = maskX + "px";
// 大圖片移動(dòng)的比例
var prop = ( bigImg.offsetWidth - bigBox.offsetWidth ) / (smilBox.offsetWidth - mask.offsetWidth);
// 大圖片的坐標(biāo)
var bigImgX = prop * maskX;
var bigImgY = prop * maskY;
bigImg.style.top = -bigImgY + "px";
bigImg.style.left = -bigImgX + "px";
}
})(window)
</script>
</html>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
javascript引擎長(zhǎng)時(shí)間獨(dú)占線程造成卡頓的解決方案
這篇文章主要介紹了javascript引擎長(zhǎng)時(shí)間獨(dú)占線程造成卡頓的解決方案,需要的朋友可以參考下2014-12-12
JS獲取子節(jié)點(diǎn)、父節(jié)點(diǎn)和兄弟節(jié)點(diǎn)的方法實(shí)例總結(jié)
這篇文章主要介紹了JS獲取子節(jié)點(diǎn)、父節(jié)點(diǎn)和兄弟節(jié)點(diǎn)的方法,結(jié)合實(shí)例形式總結(jié)分析了JavaScript針對(duì)子節(jié)點(diǎn)、父節(jié)點(diǎn)和兄弟節(jié)點(diǎn)獲取相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2018-07-07
uniapp小程序自定義tabbar以及初次加載閃屏解決方法
Uniapp小程序可以通過(guò)自定義tabbar來(lái)實(shí)現(xiàn)更加個(gè)性化的界面設(shè)計(jì),下面這篇文章主要給大家介紹了關(guān)于uniapp小程序自定義tabbar以及初次加載閃屏解決方法,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
Javascript的并行運(yùn)算實(shí)現(xiàn)代碼
隨著多核cpu的普級(jí),并發(fā)/并行多線程運(yùn)算在主流的編程語(yǔ)言越來(lái)越流行,而在目前Javascript實(shí)現(xiàn)中還看不到在語(yǔ)言方面支持多線程,現(xiàn)在Javascript如此流行,真希望今后會(huì)在語(yǔ)言的層面有很大的變化.2010-11-11
Electron 隱藏頂部菜單功能實(shí)現(xiàn)
本文介紹了如何在Electron應(yīng)用中隱藏頂部菜單,具體方法是在main.js文件中添加一行代碼,這是一個(gè)簡(jiǎn)單有效的技巧,適用于需要簡(jiǎn)化界面的Electron應(yīng)用2024-09-09
js實(shí)現(xiàn)點(diǎn)擊添加一個(gè)input節(jié)點(diǎn)
本文給大家分享的是一段點(diǎn)擊自動(dòng)添加inpu節(jié)點(diǎn)的代碼,非常的簡(jiǎn)單實(shí)用,這里推薦給大家。2014-12-12

