我正在尝试在JS中使用玻璃放大镜。
但是当我尝试将玻璃的位置设置到光标位置时出现问题:如果我尝试将玻璃位置设置为中心位置光标,滞后发生,我无法弄清楚原因。
我试图设置一个转换:转换(-50%, - 50%)到我的玻璃,但结果是一样的。
超过我的玻璃位置远离光标的中心,更少的是错误的。
let img = document.querySelector('.longboard');
let glass = document.querySelector('#zoom');
let displayCoor = document.querySelector('.coordonnees');
let a = glass.offsetWidth / 2;
let b = glass.offsetHeight / 2;
img.onmousemove = function(e) {zoomMovement(e)};
function mousemovement(e) {
let x = e.pageX;
let y = e.pageY;
let coor = "Coordinates: (" + x + "," + y + ")";
displayCoor.innerHTML = coor;
return {x : x, y : y};
};
function zoomMovement (e){
var pos = mousemovement(e);
glass.style.top = (pos.y-b) +'px'; //<-- increase b to avoid the lag
glass.style.left = (pos.x-a) +'px';//<-- increase a to avoid the lag
}
/*img.addEventListener('mouseenter', function () {
document.getElementById('zoom').className = "visible";
});
img.addEventListener('mouseleave', function () {
document.getElementById('zoom').className = "invisible";
});*/
.visible{
display: block;
box-shadow: 3px 3px 10px 0px rgba(0,0,0,0.5);
}
.invisible{
display: none;
}
#zoom{
position: absolute;
background-image: url("https://scene7.zumiez.com/is/image/zumiez/Zoom_PDP/Santa-Cruz-Space-Wolf-40%26quot%3B-Drop-Down-Longboard-Complete-_288601-front-US.jpg");
background-repeat: no-repeat;
width: 150px;
height: 150px;
border-radius: 100px;
z-index: 2;
}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Loupe</title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="container">
<img class="longboard" src="https://scene7.zumiez.com/is/image/zumiez/Zoom_PDP/Santa-Cruz-Space-Wolf-40%26quot%3B-Drop-Down-Longboard-Complete-_288601-front-US.jpg" style="position: relative; display: block;margin: 100px auto; width: 30%;"/>
</div>
<div class="coordonnees" style="display: block;margin: 30px auto"></div>
<div id="zoom" class="visible"> </div>
<script src="js/zoom3.js"></script>
</body>
</html>
如果有人能解释我原因,我将非常感激 谢谢你的帮助!
答案 0 :(得分:1)
我不确定我理解“滞后”是什么意思。但我发现的一个问题是你只是在img
上听了mousemove事件。因此,如果您将光标移动到缩放范围内,则玻璃管理器不会移动。这是因为您的glass
的{{1}}比z-index
高img
。因此,只要您不将光标移出glass
,就不会触发mousemove事件。
为了解决这个问题,我只将你的事件监听器附加到glass
元素,它看起来更平滑。
编辑:我在pointer-events:none
元素上使用CSS glass
找到了更好的解决方案。
然后,当您离开/输入img
let img = document.querySelector('.longboard');
let glass = document.querySelector('#zoom');
let displayCoor = document.querySelector('.coordonnees');
let a = glass.offsetWidth / 2;
let b = glass.offsetHeight / 2;
img.onmousemove = function(e) {zoomMovement(e)};
// previous solution : listening to mousemove on the magnifier :
// glass.onmousemove = function(e) {zoomMovement(e)};
function mousemovement(e) {
let x = e.pageX;
let y = e.pageY;
let coor = "Coordinates: (" + x + "," + y + ")";
displayCoor.innerHTML = coor;
return {x : x, y : y};
};
function zoomMovement (e){
var pos = mousemovement(e);
glass.style.top = (pos.y-b) +'px'; //<-- increase b to avoid the lag
glass.style.left = (pos.x-a) +'px';//<-- increase a to avoid the lag
}
img.addEventListener('mouseenter', function () {
document.getElementById('zoom').className = "visible";
});
img.addEventListener('mouseleave', function () {
document.getElementById('zoom').className = "invisible";
});
.visible{
display: block;
box-shadow: 3px 3px 10px 0px rgba(0,0,0,0.5);
}
.invisible{
display: none;
}
#zoom{
position: absolute;
background-image: url("https://scene7.zumiez.com/is/image/zumiez/Zoom_PDP/Santa-Cruz-Space-Wolf-40%26quot%3B-Drop-Down-Longboard-Complete-_288601-front-US.jpg");
background-repeat: no-repeat;
width: 150px;
height: 150px;
border-radius: 100px;
z-index: 2;
pointer-events:none;
}
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Loupe</title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<div class="container">
<img class="longboard" src="https://scene7.zumiez.com/is/image/zumiez/Zoom_PDP/Santa-Cruz-Space-Wolf-40%26quot%3B-Drop-Down-Longboard-Complete-_288601-front-US.jpg" style="position: relative; display: block;margin: 100px auto; width: 30%;"/>
</div>
<div class="coordonnees" style="display: block;margin: 30px auto"></div>
<div id="zoom" class="visible"> </div>
<script src="js/zoom3.js"></script>
</body>
</html>