js放大鏡放大購物圖片效果
更新時間:2021年08月26日 10:46:03 作者:秋天1014童話
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實現(xiàn)放大鏡放大購物圖片效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
圖片放大鏡效果,供大家參考,具體內(nèi)容如下
一難點(diǎn):不讓黃盒子出界
二難點(diǎn):讓大盒子相應(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" style="display:none;"></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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ionic App問題總結(jié)系列之ionic點(diǎn)擊系統(tǒng)返回鍵退出App
本篇文章主要介紹了ionic App問題總結(jié)系列之ionic點(diǎn)擊系統(tǒng)返回鍵退出App,具有一定的參考價值,有興趣的可以了解一下2017-08-08
js中substring和substr兩者區(qū)別和使用方法
這篇文章主要介紹了js中substring和substr兩者區(qū)別和使用方法,每一個步驟都有相應(yīng)的文字介紹,感興趣的小伙伴們可以參考一下2015-11-11
Javascript根據(jù)指定下標(biāo)或?qū)ο髣h除數(shù)組元素
刪除數(shù)組元素在工作中經(jīng)常會用到,本文講解一下Javascript根據(jù)下標(biāo)刪除數(shù)組元素的方法,需要了解的朋友可以參考下2012-12-12
ECharts柱狀排名圖柱子上方顯示文字與圖標(biāo)代碼實例
我們在繪制柱狀圖時如果想要柱條上顯示文字,可以參考本文,這篇文章主要給大家介紹了關(guān)于ECharts柱狀排名圖柱子上方顯示文字與圖標(biāo)的相關(guān)資料,需要的朋友可以參考下2023-11-11
微信小程序input框中加入小圖標(biāo)的實現(xiàn)方法
這篇文章主要介紹了微信小程序input框中加入小圖標(biāo)的實現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06

