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

鼠标滑过放大图片局部

时间:2018-07-23 23:38:37      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:title   style   cal   inf   block   div   poi   idt   out   

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin:0;
padding:0;
}
#box{
position:relative;
top:0;
left:0;
width:500px;
height:350px;
}
#box img{
width:100%;
height:100%;
}
#point{
position:absolute;
top:0;
width:150px;
height:100px;
background:rgba(0,0,0,0.2);
display:none;
}
#big{

position:absolute;
top:0;
left:520px;
width:500px;
height:350px;
overflow:hidden;
}
#big img{
position:absolute;
top:0;
left:0;
width:1000px;
height:700px;
display:none;
}
</style>
<script>
window.onload=function() {
var oPoint = document.getElementById(‘point‘);
var oBox = document.getElementById(‘box‘);
var oBig = document.getElementById(‘big‘);
var oMove = document.getElementById(‘move‘);


oBox.onmouseover = function (ev) {
var oEvent = ev || event;
oPoint.style.display = ‘block‘;
oMove.style.display = ‘block‘;
};

oBox.onmouseout = function (ev) {
var oEvent = ev || event;
oPoint.style.display = ‘none‘;
oMove.style.display = ‘none‘;
};


oBox.onmousemove = function (ev) {
var oEvent = ev || event;
var l = oEvent.clientX - oPoint.offsetWidth / 2;
var t = oEvent.clientY - oPoint.offsetHeight / 2;

if (l < 0) {
l = 0;
} else if (l > oBox.offsetWidth-oPoint.offsetWidth) {
l = oBox.offsetWidth-oPoint.offsetWidth;
}
if (t < 0) {
t = 0;
} else if (t > oBox.offsetHeight-oPoint.offsetHeight) {
t = oBox.offsetHeight-oPoint.offsetHeight;
}

var scale = l * (oBig.offsetWidth - oMove.offsetWidth) / (oBox.offsetWidth - oPoint.offsetWidth);
var scale2 = t * (oBig.offsetHeight - oMove.offsetHeight) / (oBox.offsetHeight - oPoint.offsetHeight);

oMove.style.left = scale + ‘px‘;
oMove.style.top = scale2 + ‘px‘;
oPoint.style.left = l + ‘px‘;
oPoint.style.top = t + ‘px‘;


};
}


</script>
</head>
<body>

<div id="box">
<img src="image/a(1).jpg"/>
<span id="point"></span>
</div>

<div id="big">
<img src="image/a(1).jpg" id="move"/>
</div>
<!--<div id="point"></div>-->
</body>
</html>

效果如下:

技术分享图片

 

此处的图片路径可以自行更换。

技术分享图片

 

鼠标滑过放大图片局部

标签:title   style   cal   inf   block   div   poi   idt   out   

原文地址:https://www.cnblogs.com/myhanyu/p/9357386.html

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