JavaScript實(shí)現(xiàn)簡易放大鏡最全代碼解析(ES5)
更新時(shí)間:2021年09月10日 10:33:40 作者:颯爾
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡易放大鏡最全代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)簡易放大鏡的具體代碼,供大家參考,具體內(nèi)容如下

完整代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ES5放大鏡</title>
<style>
.box {
width: 170px;
height: 180px;
margin: 100px 200px;
border: 1px solid #ccc;
position: relative;
}
.small {
position: relative;
}
.big {
width: 300px;
height: 320px;
position: absolute;
top: 30px;
left: 220px;
overflow: hidden;
border: 1px solid #ccc;
display: none;
}
.mask {
width: 100px;
height: 100px;
background: rgba(255,255,0,0.3);
position: absolute;
top: 0;
left: 0;
cursor: move;
display: none;
}
.big img{
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box" id="box">
<div class="small">
<img src="img/shirt_1.jpg" alt="picture"/>
<div class="mask"></div>
</div>
<div class="big">
<img src="img/shirt_1_big.jpg" alt="picture"/>
</div>
</div>
</body>
<script>
var box = document.getElementById('box');
var small = box.children[0];//放小圖的盒子
var big = box.children[1];//放大圖的盒子
var mask = small.children[1];//半透明鼠標(biāo)移動跟隨盒子
var bigImage = big.children[0];//大圖片
small.onmouseover = function(event){
big.style.display = 'block';
mask.style.display = 'block';
}
small.onmouseout = function(){
big.style.display = 'none';
mask.style.display = 'none';
}
//移動事件,注意mask的定位相對的是samll
small.onmousemove = function(event){
var event = event || window.event;//事件對象
// console.log(this.offsetLeft);//0,注意offsetLeft返回距離是一個(gè)帶有定位的父級的左側(cè)距離
var x = event.clientX-this.offsetParent.offsetLeft-mask.offsetWidth/2;//此處不能用small的offsetLeft,用obj的offsetLeft
var y = event.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";
//大圖片放大
bigImage.style.left = -x*big.offsetWidth/small.offsetWidth + 'px';
bigImage.style.top = -y*big.offsetHeight/small.offsetHeight + 'px';
//big.offsetWidth/small.offsetWidth是放大倍數(shù)
//因?yàn)樾D鼠標(biāo)下移,大圖上移,故用負(fù)數(shù)
}
</script>
</html>
圖片:


以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js獲取GridView中行數(shù)據(jù)的兩種方法 分享
這篇文章介紹了js獲取GridView中行數(shù)據(jù)的方法,有需要的朋友可以參考一下2013-07-07
javascript同頁面多次調(diào)用彈出層具體實(shí)例代碼
一個(gè)在同一個(gè)頁面可多次調(diào)用的javascript彈出層效果,有需要的同學(xué)可以參考一下2013-08-08
實(shí)用Javascript調(diào)試技巧分享(小結(jié))
這篇文章主要介紹了實(shí)用Javascript調(diào)試技巧分享(小結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
用JavaScript實(shí)現(xiàn)UrlEncode和UrlDecode的腳本代碼
用js自定義函數(shù)寫的實(shí)現(xiàn)url加密解密的實(shí)現(xiàn)代碼,需要的朋友可以參考下2008-07-07
Bootstrap中g(shù)lyphicons-halflings-regular.woff字體報(bào)404錯(cuò)notfound的解
這篇文章主要介紹了 Bootstrap中g(shù)lyphicons-halflings-regular.woff字體報(bào)404錯(cuò)notfound的解決方法,需要的朋友可以參考下2017-01-01

