- 相關(guān)推薦
JavaScript簡(jiǎn)單實(shí)現(xiàn)放大鏡效果代碼
導(dǎo)語:JavaScript簡(jiǎn)單實(shí)現(xiàn)放大鏡效果代碼是有百分網(wǎng)小編給大家提供的,大家可以參考閱讀,更多詳情請(qǐng)關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
img{
vertical-align: top;
}
.fdj {
width: 350px;
height: 350px;
position: relative;
margin: 100px auto;
border: 1px solid gainsboro;
}
.small {
position: relative;
}
.small img {
width: 350px;
}
.mask {
width: 100px;
height: 100px;
background: rgba(255, 255, 0, 0.4);
position: absolute;
left: 0;
top: 0;
cursor: move;
display: none;
}
.big {
position: absolute;
top: 0;
left: 360px;
width: 500px;
height: 500px;
border: 1px solid gainsboro;
overflow: hidden;
display: none;
}
.big img{
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<p class="fdj">
<p class="small">
<img src="http://cdn.attach.qdfuns.com/notes/pics/201702/08/162503mw0fawb5b02va22i.jpg" />
<p class="mask"></p>
</p>
<p class="big">
<img src="http://cdn.attach.qdfuns.com/notes/pics/201702/08/162503mw0fawb5b02va22i.jpg" />
</p>
</p>
</body>
<script type="text/javascript">
var fdj = document.querySelector('.fdj') // 獲得最大的盒子
var small = document.querySelector('.small'); //獲取小圖片盒子
var big = document.querySelector('.big'); //獲取大圖片盒子
var bigs = big.children[0] //大圖片
var smalls = small.children[0] //小圖片
var mask = small.children[1]; //遮罩
//鼠標(biāo)移入小圖片盒子
small.onmouseover = function() {
//鼠標(biāo)移入圖片盒子將遮罩與大圖片顯示
mask.style.display = 'block';
big.style.display = ' block';
};
//鼠標(biāo)移出小圖片盒子
small.onmouseout = function() {
//鼠標(biāo)移出小圖片盒子將遮罩與大圖片隱藏
mask.style.display = 'none';
big.style.display = 'none';
};
var x=0;
var y=0;
//鼠標(biāo)在小圖片上移動(dòng)時(shí)
small.onmousemove = function(ev) {
var ev = event || window.event;
//讓鼠標(biāo)在遮罩正中
//鼠標(biāo)X坐標(biāo)與Y坐標(biāo)
x = ev.clientX -this.offsetParent.offsetLeft- mask.offsetWidth / 2 ;
y = ev.clientY -this.offsetParent.offsetTop- mask.offsetHeight / 2 ;
//將遮罩限制在小圖片盒子中
if (x<0) {
x=0;
}else if(x>small.offsetWidth-mask.offsetWidth){
x = small.offsetWidth-mask.offsetWidth;
}
if(y<0){
y=0;
}else if(y>small.offsetHeight-mask.offsetHeight){
y= small.offsetHeight-mask.offsetHeight
}
mask.style.left = x + 'px';
mask.style.top = y + 'px';
//大圖與小圖的比例
/*var scalX = bigs.offsetWidth/small.offsetWidth;
var scalY = bigs.offsetHeight/small.offsetHeight;*/
var scalX = x/(small.offsetWidth-mask.offsetWidth);
var scalY = y/(small.offsetHeight-mask.offsetHeight);
bigs.style.left = -(x*scalX)+'px';
bigs.style.top = -(y*scalY)+'px';
};
</script>
</html>
【JavaScript簡(jiǎn)單實(shí)現(xiàn)放大鏡效果代碼】相關(guān)文章:
javascript實(shí)現(xiàn)小球的自由移動(dòng)代碼08-26
java實(shí)現(xiàn)gif動(dòng)畫效果代碼參考07-23
Javascript實(shí)現(xiàn)全選并賦值給文本框代碼實(shí)例08-24
如何實(shí)現(xiàn)JavaScript的DIV塊來回滾動(dòng)效果06-30
如何使用javascript實(shí)現(xiàn)瀑布流及效果加載06-17
Java 隊(duì)列實(shí)現(xiàn)原理及簡(jiǎn)單實(shí)現(xiàn)代碼09-13
CSS實(shí)現(xiàn)自適應(yīng)寬度的菜單按鈕效果代碼09-22