圖片放大鏡效果,供大家參考,具體內(nèi)容如下

一難點:不讓黃盒子出界
二難點:讓大盒子相應(yīng)移動(比例)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我的放大鏡</title>
<style>
*{
margin: 0;
padding: 0;
}
.box{
margin: 100px;
position: relative;
}
.small{
width: 350px;
height: 350px;
border: 1px solid #999;
position: relative;
}
.select{
display: none;
width: 100px;
height: 100px;
background: rgba(255,255,0,0.4);
position: absolute;
left: 0;
top: 0;
cursor: move;
}
.big{
display: none;
position: absolute;
left: 360px;
top: 0;
width: 450px;
height: 450px;
border: 1px solid #ccc;
overflow: hidden;
}
.big img{
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box">
<div class="small" id="smallbox">
<img src="images/001.jpg" alt="">
<div class="select" id="mask" ></div>
</div>
<div class="big" id="bigbox">
<img src="images/0001.jpg" alt="">
</div>
</div>
<script>
var smallbox = document.getElementById('smallbox');
var bigbox = document.getElementById('bigbox');
var mask = document.getElementById('mask');
var bigImg = bigbox.children[0];
smallbox.onmouseover = function(){
mask.style.display = "block";
bigbox.style.display = "block";
}
smallbox.onmouseout = function(){
mask.style.display = "none";
bigbox.style.display = "none";
}
smallbox.onmousemove = function(event){
var event = event || window.event;
var x = event.clientX - this.offsetParent.offsetLeft - mask.offsetWidth/2;
var y = event.clientY - this.offsetParent.offsetTop - mask.offsetHeight/2;
//不讓黃盒子出界
if(x < 0){
x = 0;
}else if(x > smallbox.offsetWidth - mask.offsetWidth){
x = smallbox.offsetWidth - mask.offsetWidth;
}
if(y<0) {
y = 0;
}else if(y > smallbox.offsetHeight - mask.offsetHeight){
y = smallbox.offsetHeight - mask.offsetHeight;
}
mask.style.left = x + "px";
mask.style.top = y + "px";
bigImg.style.left = -x/smallbox.offsetWidth * bigbox.offsetWidth + "px"; //注意是-x
bigImg.style.top = -y/smallbox.offsetHeight * bigbox.offsetHeight + "px";
}
</script>
</body>
</html>另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文標(biāo)題:js放大鏡放大購物圖片效果-創(chuàng)新互聯(lián)
文章分享:http://www.chinadenli.net/article22/ccsdjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、軟件開發(fā)、服務(wù)器托管、自適應(yīng)網(wǎng)站、網(wǎng)頁設(shè)計公司、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容