DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 原生js寫的放大鏡效果
原生js寫的放大鏡效果
編輯:關於JavaScript     
我的大體思路是:時時監聽鼠標的坐標,當鼠標移動時,透明層隨著鼠標移動,大圖片相對透明層的移動而移動。不廢話了,看代碼。
復制代碼 代碼如下:
<!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,鼠標在窗口任意位置點擊,圖片都會跟隨
document.onmousemove=test2;//document如果改為outer,鼠標在outer內才起作用
document.onmouseup=test3;
function test1(event){//鼠標按下時方法
var event=event || window.event;//調試兼容,各個浏覽器認識event有差別.
n=true;//當n=true(n的值可隨便設定)時,假定為鼠標按下的事件
x=event.clientX-inner.offsetLeft;//鼠標在透明層的相對橫坐標=鼠標坐標-方塊左邊距
y=event.clientY-inner.offsetTop;//鼠標在透明層的相對縱坐標=鼠標坐標-方塊上邊距
}
function test2(event){//鼠標移動時方法
var event=event || window.event;
if(n==true){
////////鼠標移動范圍
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";
////////////////////////////限定鼠標移動的范圍
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(){//鼠標松開時方法
n=false;
}
</script>
</body>
</html>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved