原生js實現(xiàn)商品放大鏡效果
實現(xiàn)原理
大圖上的放大鏡:小圖的顯示區(qū)域=大圖片大?。盒D片大小=大圖片的offsetLeft:小圖片的offsetLeft
那么以上的公式中只有大圖片的offsetLeft 是未知的,所以大圖片的offsetLeft=大圖片大小/小圖片大小*小圖片的offsetLeft
代碼中有詳細注釋
完整代碼
注:復(fù)制到本地后自行替換圖片查看效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
#demo{display:block;width:400px;height:255px;margin:50px;position:relative;border:1px solid#ccc}
#small-box{position:relative;z-index:1}
#float-box{display:none;width:160px;height:120px;position:absolute;background:#ffffcc;border:1px solid#ccc;filter:alpha(opacity=50);opacity:0.5}
#mark{position:absolute;display:block;width:400px;height:255px;background-color:#fff;filter:alpha(opacity=0);opacity:0;z-index:10}
#big-box{display:none;position:absolute;top:0;left:460px;width:400px;height:300px;overflow:hidden;border:1px solid#ccc;z-index:1}
#big-box img{position:absolute;z-index:5}
</style>
</head>
<body>
<div id="demo">
<div id="small-box">
<div id="mark"></div>
<div id="float-box"></div>
<img src="images/small.jpg"/>
</div>
<div id="big-box">
<img src="images/big.jpg"/>
</div>
</div>
<script type="text/javascript">
//在頁面加載完后立即執(zhí)行多個函數(shù)方案
function addloadEvent(func){
var oldonload=window.onload;
if(typeof window.onload !="function"){
window.onload=func;
}
else{
window.onload=function(){
if(oldonload){
oldonload();
}
func();
}
}
}
//在頁面加載完后立即執(zhí)行多個函數(shù)方案結(jié)束
addloadEvent(b);
function b(){
//獲取外圍容器
var demo=document.getElementById("demo");
//獲取小圖片容器
var s_Box=document.getElementById("small-box");
//獲取大圖片容器
var b_Box=document.getElementById("big-box");
//獲取大圖片
var b_Image=b_Box.getElementsByTagName("img")[0];
//獲取放大鏡
var f_Box=document.getElementById("float-box");
//覆蓋在最上面的蓋板為鼠標移動用
var mark=document.getElementById("mark");
//移入放大鏡和大圖片容器顯示
mark.onmouseover=function(){
f_Box.style.display="block";
b_Box.style.display="block";
}
//移出放大鏡和大圖片容器隱藏
mark.onmouseout=function(){
f_Box.style.display="none";
b_Box.style.display="none";
}
//移動事件
mark.onmousemove=function(ev){
//獲取鼠標坐標window兼容ie
var e=ev||window.event;
//當前鼠標x軸-容器相對body偏移量-小容器相對父容器偏移值-放大鏡寬度的一半=放大鏡的當前位置
var left=e.clientX-demo.offsetLeft-s_Box.offsetLeft-f_Box.offsetWidth/2;
//公式同上
var top=e.clientY-demo.offsetTop-s_Box.offsetTop-f_Box.offsetHeight/2;
//判斷當放大鏡移出容器時在邊緣顯示
if(left<0){
left=0;
}else if(left>(s_Box.offsetWidth-f_Box.offsetWidth)){
left=s_Box.offsetWidth-f_Box.offsetWidth;
}
if(top<0){
top=0;
}else if(top>(s_Box.offsetHeight-f_Box.offsetHeight)){
top=s_Box.offsetHeight-f_Box.offsetHeight;
}
//放大鏡當前位置
f_Box.style.left=left+"px";
f_Box.style.top=top+"px";
//獲取比例
var z=b_Image.offsetWidth/s_Box.offsetWidth;
//用放大鏡偏移量*比例=大圖片的偏移量,方向相反所以負值
b_Image.style.left=-left*z+"px";
b_Image.style.top=-top*z+"px";
}
}
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
JavaScript實現(xiàn)LRU緩存的三種方式詳解
LRU全稱為Least?Recently?Used,即最近使用的。針對的是在有限的內(nèi)存空間內(nèi),只緩存最近使用的數(shù)據(jù)(即get和set的數(shù)據(jù))。本文介紹了JavaScript實現(xiàn)LRU緩存的三種方式,需要的可以參考一下2022-06-06
JavaScript實現(xiàn)數(shù)組去重的十種方法分享
去重是開發(fā)中經(jīng)常會碰到的一個熱點問題,這篇文章主要介紹了JavaScript中實現(xiàn)數(shù)組去重的10種方法,文中的示例代碼講解詳細,感興趣的可以了解一下2022-11-11
我要點爆”微信小程序云開發(fā)之項目建立與我的頁面功能實現(xiàn)
這篇文章主要介紹了我要點爆”微信小程序云開發(fā)之項目建立與我的頁面功能實現(xiàn),本文圖文并茂給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05
在IE下獲取object(ActiveX)的Param的代碼
在IE下,獲取Param的時候有個詭異現(xiàn)象(不知道算不算bug)。2009-09-09

