#wrap {
width: 100%;
height: 100%;
}
#wrap .container {
width: 1210px;
margin: 0 auto;
position: relative;
}
#wrap .container .small-wrap {
position: relative;
width: 325px;
height: 325px;
border: 1px solid #eee;
position: relative;
}
#wrap .container .small-wrap img {
width: 100%;
height: 100%;
}
#wrap .container .small-wrap .yellow-box {
width: 200px;
height: 200px;
border: 1px solid #aaa;
background: rgba(255, 0, 0, 0.3);
position: absolute;
left: 0;
;
var yellowBox = document.querySelector(".yellow-box");
var bigWrap = document.querySelector(".big-wrap");
var bigImg = document.querySelector(".big-wrap img");
yellowBox.style.width = smallWrap.clientWidth / (bigImg.clientWidth / bigWrap.clientWidth) + 'px';
yellowBox.style.height = smallWrap.clientHeight / (bigImg.clientHeight / bigWrap.clientHeight) + 'px';
bigWrap.style.display = 'none';
smallWrap.addEventListener('mouseover', function () {
yellowBox.style.display = 'block';
bigWrap.style.display = 'block';
});
smallWrap.addEventListener('mouseout', function () {
yellowBox.style.display = 'none';
bigWrap.style.display = 'none';
});
smallWrap.addEventListener('mousemove', function (e) {
var x = e.pageX - this.parentNode.offsetLeft - (yellowBox.offsetWidth / 2);
var y = e.pageY - this.parentNode.offsetTop - (yellowBox.offsetHeight / 2);
var maxMove = smallWrap.offsetWidth - yellowBox.offsetWidth;
if (x < 0) {
x = 0;
} else if (x > maxMove) {
x = maxMove;
}
if (y < 0) {
y = 0;
} else if (y > maxMove) {
y = maxMove;
}
yellowBox.style.left = x + 'px';
yellowBox.style.top = y + 'px';
var bigMaxMove = bigImg.offsetHeight - bigWrap.offsetHeight;
bigImg.style.marginLeft = -(x / maxMove) * bigMaxMove + 'px';
bigImg.style.marginTop = -(y / maxMove) * bigMaxMove + 'px';
});
</script>
</html>
我的屏幕尺寸比较小,这个代码是按照b站上一个程序员媛恩予写的。但是现在出现点问题如下:大图没有放大镜效果,求解!
