码迷,mamicode.com
首页 > 其他好文 > 详细

照着别人的demo自己试着做了个放大镜效果

时间:2015-04-21 14:23:06      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>放大镜效果</title>

<style>

*{
margin:0;
padding:0;
}

#demo{
position: relative;
margin:30px 50px;
width: 1000px;
height: 600px;
border: 1px solid #000;
}

#zhezhao{
position: absolute;
z-index:2;
background:red;
width:402px;
height:402px;
left: 20px;
top:20px;
opacity: 0;
}

#small{
position: absolute;
width:402px;
height:402px;
left: 20px;
top:20px;
border: 1px solid #000;
z-index: 1;
}


#small img{
position: absolute;

}


#big{
position: relative;
top: 20px;
left: 460px;
width:500px;
height:500px;
border: 1px solid #000;
overflow: hidden;
display: none;
z-index: 1;
}


#big img{
position: absolute;

}


#glass{
position: absolute;
width:100px;
height: 100px;
opacity: 0.3;
background:orange;
display: none;
}

</style>

<script>

window.onload=function(){
var demo =document.getElementById(‘demo‘);
var small =document.getElementById(‘small‘);
var big =document.getElementById(‘big‘);
var glass =document.getElementById(‘glass‘)
var image =document.getElementById(‘big‘).getElementsByTagName(‘img‘)[0];
var zhezhao=document.getElementById(‘zhezhao‘);

zhezhao.onmouseover=function(){
glass.style.display=‘block‘
big.style.display=‘block‘
}
zhezhao.onmouseout=function(){
glass.style.display=‘none‘
big.style.display=‘none‘
}

//弄清楚clientX,offsetLeft,left的关系,注意区分
zhezhao.onmousemove=function(ev){
var event=ev
var left=event.clientX-demo.offsetLeft-small.offsetLeft-glass.offsetWidth/2;
var top =event.clientY-demo.offsetTop -small.offsetTop -glass.offsetHeight/2;
if(left<0){
left=0;
}else if(left>(small.offsetWidth-glass.offsetWidth)){
left=small.offsetWidth-glass.offsetWidth
}

if(top<0){
top=0;
}else if(top>(small.offsetHeight- glass.offsetHeight)){
top=small.offsetHeight- glass.offsetHeight
}
glass.style.left =left+‘px‘;
glass.style.top =top+‘px‘;

 

var percent=(image.offsetWidth-big.offsetWidth)/(small.offsetWidth-glass.offsetWidth)

image.style.left=-percent*left+‘px‘
image.style.top =-percent*top+‘px‘


}
}

</script>
</head>


<body>
<div id=‘demo‘>
<div id=‘zhezhao‘> </div>
<!-- 在ie浏览器中,当鼠标在放大镜上是,浏览器并不认为鼠标同样在小图的div上,所以加个遮罩层 兼容ie-->


<div id=‘small‘>
<img src=‘images/small.png‘ alt=‘‘>
<div id=‘glass‘></div>
</div>

<div id=‘big‘>
<img src=‘images/big.jpg‘ alt=‘‘ >

</div>

</div>


<!-- takecolor:alt+c -->
</body>

</html>

照着别人的demo自己试着做了个放大镜效果

标签:

原文地址:http://www.cnblogs.com/yzg1/p/4444046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!