原生js寫的放大鏡效果
更新時間:2012年08月22日 21:00:39 作者:
在淘寶上購物時,總會看到類似放大鏡的效果。以下為原生js寫的一個放大鏡效果,其中肯定有很多不足,請大牛們指正,謝啦
我的大體思路是:時時監(jiān)聽鼠標(biāo)的坐標(biāo),當(dāng)鼠標(biāo)移動時,透明層隨著鼠標(biāo)移動,大圖片相對透明層的移動而移動。不廢話了,看代碼。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>放大鏡</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
/*重置{*/
html{color:#000;background:#fff;}
body,div{padding:0;margin:0;}
img{border:none;}
/*}重置*/
.outer{width:200px;height:150px;position:relative;margin:20px auto;}
.inner{width:80px;height:60px;background:#f55;position:absolute;opacity:0.5;filter:alpha(opacity=50);left:0;top:0;cursor:pointer;}
.aa{width:320px;height:240px;position:relative;border:1px red solid;margin:20px auto;overflow:hidden;}
.imgs{position:absolute;}
.outer img{width:200px;height:150px;}
</style>
</head>
<body>
<div>
<div class="outer" id="outer">
<img src="images/pobabyb.gif" alt="pobaby小圖"/>
<div class="inner" id="inner"></div>
</div>
<div class="aa" id="aa">
<div class="imgs" id="imgs" ><img src="images/pobabyb.gif" alt="pobaby大圖"/></div>
</div>
</div>
<script type="text/javascript">
var outer=document.getElementById("outer");
var inner=document.getElementById("inner");
var aa=document.getElementById("aa");
var imgs=document.getElementById("imgs");
var x,y,n=false;
inner.onmousedown=test1;//如果把inner改為document,鼠標(biāo)在窗口任意位置點擊,圖片都會跟隨
document.onmousemove=test2;//document如果改為outer,鼠標(biāo)在outer內(nèi)才起作用
document.onmouseup=test3;
function test1(event){//鼠標(biāo)按下時方法
var event=event || window.event;//調(diào)試兼容,各個瀏覽器認(rèn)識event有差別.
n=true;//當(dāng)n=true(n的值可隨便設(shè)定)時,假定為鼠標(biāo)按下的事件
x=event.clientX-inner.offsetLeft;//鼠標(biāo)在透明層的相對橫坐標(biāo)=鼠標(biāo)坐標(biāo)-方塊左邊距
y=event.clientY-inner.offsetTop;//鼠標(biāo)在透明層的相對縱坐標(biāo)=鼠標(biāo)坐標(biāo)-方塊上邊距
}
function test2(event){//鼠標(biāo)移動時方法
var event=event || window.event;
if(n==true){
////////鼠標(biāo)移動范圍
inner.style.left=event.clientX-x+"px";
inner.style.top=event.clientY-y+"px";
////////圖片移動范圍
imgs.style.left=-4*parseInt(inner.style.left)+"px";
imgs.style.top=-4*parseInt(inner.style.top)+"px";
////////////////////////////限定鼠標(biāo)移動的范圍
if(parseInt(inner.style.left)<0){
inner.style.left=0+"px";
}
if(parseInt(inner.style.top)<0){
inner.style.top=0+"px";
}
if(parseInt(inner.style.left)>outer.clientWidth-inner.clientWidth){
inner.style.left=outer.clientWidth-inner.clientWidth+"px";
}
if(parseInt(inner.style.top)>outer.clientHeight-inner.clientHeight){
inner.style.top=outer.clientHeight-inner.clientHeight+"px";
}
//////////////////////////////限定圖片移動的范圍
if(parseInt(imgs.style.left)>0){
imgs.style.left=0+"px";
}
if(parseInt(imgs.style.top)>0){
imgs.style.top=0+"px";
}
if(parseInt(imgs.style.left)<-4*(outer.clientWidth-inner.clientWidth)){
imgs.style.left=-4*parseInt(outer.clientWidth-inner.clientWidth)+"px";
}
if(parseInt(imgs.style.top)<-4*(outer.clientHeight-inner.clientHeight)){
imgs.style.top=-4*parseInt(outer.clientHeight-inner.clientHeight)+"px";
}
}
}
function test3(){//鼠標(biāo)松開時方法
n=false;
}
</script>
</body>
</html>
復(fù)制代碼 代碼如下:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>放大鏡</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
/*重置{*/
html{color:#000;background:#fff;}
body,div{padding:0;margin:0;}
img{border:none;}
/*}重置*/
.outer{width:200px;height:150px;position:relative;margin:20px auto;}
.inner{width:80px;height:60px;background:#f55;position:absolute;opacity:0.5;filter:alpha(opacity=50);left:0;top:0;cursor:pointer;}
.aa{width:320px;height:240px;position:relative;border:1px red solid;margin:20px auto;overflow:hidden;}
.imgs{position:absolute;}
.outer img{width:200px;height:150px;}
</style>
</head>
<body>
<div>
<div class="outer" id="outer">
<img src="images/pobabyb.gif" alt="pobaby小圖"/>
<div class="inner" id="inner"></div>
</div>
<div class="aa" id="aa">
<div class="imgs" id="imgs" ><img src="images/pobabyb.gif" alt="pobaby大圖"/></div>
</div>
</div>
<script type="text/javascript">
var outer=document.getElementById("outer");
var inner=document.getElementById("inner");
var aa=document.getElementById("aa");
var imgs=document.getElementById("imgs");
var x,y,n=false;
inner.onmousedown=test1;//如果把inner改為document,鼠標(biāo)在窗口任意位置點擊,圖片都會跟隨
document.onmousemove=test2;//document如果改為outer,鼠標(biāo)在outer內(nèi)才起作用
document.onmouseup=test3;
function test1(event){//鼠標(biāo)按下時方法
var event=event || window.event;//調(diào)試兼容,各個瀏覽器認(rèn)識event有差別.
n=true;//當(dāng)n=true(n的值可隨便設(shè)定)時,假定為鼠標(biāo)按下的事件
x=event.clientX-inner.offsetLeft;//鼠標(biāo)在透明層的相對橫坐標(biāo)=鼠標(biāo)坐標(biāo)-方塊左邊距
y=event.clientY-inner.offsetTop;//鼠標(biāo)在透明層的相對縱坐標(biāo)=鼠標(biāo)坐標(biāo)-方塊上邊距
}
function test2(event){//鼠標(biāo)移動時方法
var event=event || window.event;
if(n==true){
////////鼠標(biāo)移動范圍
inner.style.left=event.clientX-x+"px";
inner.style.top=event.clientY-y+"px";
////////圖片移動范圍
imgs.style.left=-4*parseInt(inner.style.left)+"px";
imgs.style.top=-4*parseInt(inner.style.top)+"px";
////////////////////////////限定鼠標(biāo)移動的范圍
if(parseInt(inner.style.left)<0){
inner.style.left=0+"px";
}
if(parseInt(inner.style.top)<0){
inner.style.top=0+"px";
}
if(parseInt(inner.style.left)>outer.clientWidth-inner.clientWidth){
inner.style.left=outer.clientWidth-inner.clientWidth+"px";
}
if(parseInt(inner.style.top)>outer.clientHeight-inner.clientHeight){
inner.style.top=outer.clientHeight-inner.clientHeight+"px";
}
//////////////////////////////限定圖片移動的范圍
if(parseInt(imgs.style.left)>0){
imgs.style.left=0+"px";
}
if(parseInt(imgs.style.top)>0){
imgs.style.top=0+"px";
}
if(parseInt(imgs.style.left)<-4*(outer.clientWidth-inner.clientWidth)){
imgs.style.left=-4*parseInt(outer.clientWidth-inner.clientWidth)+"px";
}
if(parseInt(imgs.style.top)<-4*(outer.clientHeight-inner.clientHeight)){
imgs.style.top=-4*parseInt(outer.clientHeight-inner.clientHeight)+"px";
}
}
}
function test3(){//鼠標(biāo)松開時方法
n=false;
}
</script>
</body>
</html>

相關(guān)文章
JS addEventListener()和attachEvent()方法實現(xiàn)注冊事件
這篇文章主要介紹了JS addEventListener()和attachEvent()方法實現(xiàn)注冊事件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01js中把JSON字符串轉(zhuǎn)換成JSON對象最好的方法
這篇文章主要介紹了js中把JSON字符串轉(zhuǎn)換為JSON對象最好的方法,需要的朋友可以參考下2014-03-03JavaScript 正則表達式中g(shù)lobal模式的特性
這篇文章主要介紹了JavaScript 正則表達式中g(shù)lobal模式的特性 的相關(guān)資料,需要的朋友可以參考下2016-02-02js css實現(xiàn)垂直方向自適應(yīng)的三角提示菜單
這篇文章主要為大家詳細介紹了js css實現(xiàn)垂直方向自適應(yīng)的三角提示菜單的相關(guān)資料,需要的朋友可以參考下2016-06-06js獲取對象、數(shù)組的實際長度,元素實際個數(shù)的實現(xiàn)代碼
下面小編就為大家?guī)硪黄猨s獲取對象、數(shù)組的實際長度,元素實際個數(shù)的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享 給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-06-06setTimeout內(nèi)不支持jquery的選擇器的解決方案
在JS中無論是setTimeout還是setInterval,在使用函數(shù)名作為調(diào)用句柄時都不能帶參數(shù),而在許多場合必須要帶參數(shù),這就需要想方法解決。2015-04-04