这篇攻略是针对实现 JavaScript 图片放大镜(可拖放、缩放效果)的第四个页面进行细致的讲解。该页面主要实现的是放大镜的拖动和缩放效果。以下是详细的攻略:
步骤一:添加 HTML 结构
首先,我们需要在 HTML 文件中添加放大镜所需的结构,代码如下所示:
<div class="container">
<div class="image-container">
<img src="your-image-source" alt="your-image">
<div class="lens"></div>
</div>
</div>
其中,“container”类包括了全局的样式, “image-container”类用于包裹图片及其缩略图,并提供样式,而“lens”类则是放大镜的HTML标记,本页面中主要对该类进行操作。
步骤二:添加 CSS 样式
接下来,我们需要为HTML添加CSS样式,代码如下所示:
.container {
position: relative;
}
.image-container {
position: relative;
width: 600px;
}
img {
max-width: 100%;
}
.lens {
position: absolute;
display: none;
top: 0;
border: 1px solid #ddd;
}
其中,“container”类用于控制全局样式,position: relative; 的设置让 “image-container” 与其内部的的“lens”可以相对于该类进行定位。当光标穿过图片时,“lens”将变成可见状态(display: block;);当光标从图片上离开时, “lens”将再次隐藏。
步骤三:编写JS代码
最后,我们需要编写JS代码以实现放大镜的拖动和缩放效果。本页面中,主要是对鼠标事件进行监听,并根据事件来实现操作效果,代码如下:
const lens = document.querySelector(".lens");
const imageContainer = document.querySelector(".image-container");
const image = document.querySelector("img");
let zoomLevel = 1;
image.addEventListener("mousemove", moveLens);
function moveLens(e) {
const x = e.offsetX;
const y = e.offsetY;
lens.style.display = "block";
const lensWidth = lens.offsetWidth / 2;
const lensHeight = lens.offsetHeight / 2;
let lensX = x - lensWidth;
let lensY = y - lensHeight;
if (lensX > image.width - lensWidth) {
lensX = image.width - lensWidth;
}
if (lensX < lensWidth) {
lensX = lensWidth;
}
if (lensY > image.height - lensHeight) {
lensY = image.height - lensHeight;
}
if (lensY < lensHeight) {
lensY = lensHeight;
}
lens.style.left = lensX + "px";
lens.style.top = lensY + "px";
zoomLevel = (image.width / lens.offsetWidth) * 1.5;
image.style.transform = `scale(${zoomLevel})`;
}
imageContainer.addEventListener("mouseleave", () => {
lens.style.display = "none";
image.style.transform = "scale(1)";
});
imageContainer.addEventListener("mousedown", (e) => {
e.preventDefault();
let lensPosX = lens.offsetLeft;
let lensPosY = lens.offsetTop;
let cursorPosX = e.clientX;
let cursorPosY = e.clientY;
document.addEventListener("mousemove", dragLens);
document.addEventListener("mouseup", stopDragging);
function dragLens(e) {
let newCursorPosX = e.clientX;
let newCursorPosY = e.clientY;
let lensNewPosX = lensPosX + newCursorPosX - cursorPosX;
let lensNewPosY = lensPosY + newCursorPosY - cursorPosY;
if (lensNewPosX < lens.offsetWidth / 2) {
lensNewPosX = lens.offsetWidth / 2;
}
if (lensNewPosX > image.width - lens.offsetWidth / 2) {
lensNewPosX = image.width - lens.offsetWidth / 2;
}
if (lensNewPosY < lens.offsetHeight / 2) {
lensNewPosY = lens.offsetHeight / 2;
}
if (lensNewPosY > image.height - lens.offsetHeight / 2) {
lensNewPosY = image.height - lens.offsetHeight / 2;
}
lens.style.left = lensNewPosX + "px";
lens.style.top = lensNewPosY + "px";
let percentageMovedX = (newCursorPosX - cursorPosX) / image.width;
let percentageMovedY = (newCursorPosY - cursorPosY) / image.height;
image.style.transformOrigin = `${-percentageMovedX * 100}% ${-percentageMovedY * 100}%`;
}
function stopDragging() {
document.removeEventListener("mousemove", dragLens);
}
});
具体来说,JS代码主要实现了如下几个操作:
-
监听鼠标移动事件,根据鼠标的位置,计算出放大镜需要显示的位置,以及放大比例,并对图片使用transform生成缩放效果。
-
监听鼠标移出图像区域事件,隐藏放大镜并还原图片大小。
-
监听鼠标按下事件,保存鼠标当前位置和放大镜当前位置,同时监听鼠标移动事件,根据鼠标移动的距离来更新放大镜和图片的位置。
其中,第3个操作就是拖动的实现。
示例说明
如果您想要更好的理解上述步骤,并希望获得更多的学习资料,我们这里提供了两个简单的示例说明:
- 在页面中添加大图及其缩略图,并进行对应的样式设置和js代码的复制即可。
<div class="container">
<div class="image-container">
<img src="your-image-source" alt="your-image">
<div class="lens"></div>
</div>
</div>
.container {
position: relative;
}
.image-container {
position: relative;
width: 600px;
}
img {
max-width: 100%;
}
.lens {
position: absolute;
display: none;
top: 0;
border: 1px solid #ddd;
}
const lens = document.querySelector(".lens");
const imageContainer = document.querySelector(".image-container");
const image = document.querySelector("img");
let zoomLevel = 1;
image.addEventListener("mousemove", moveLens);
function moveLens(e) {
const x = e.offsetX;
const y = e.offsetY;
lens.style.display = "block";
const lensWidth = lens.offsetWidth / 2;
const lensHeight = lens.offsetHeight / 2;
let lensX = x - lensWidth;
let lensY = y - lensHeight;
if (lensX > image.width - lensWidth) {
lensX = image.width - lensWidth;
}
if (lensX < lensWidth) {
lensX = lensWidth;
}
if (lensY > image.height - lensHeight) {
lensY = image.height - lensHeight;
}
if (lensY < lensHeight) {
lensY = lensHeight;
}
lens.style.left = lensX + "px";
lens.style.top = lensY + "px";
zoomLevel = (image.width / lens.offsetWidth) * 1.5;
image.style.transform = `scale(${zoomLevel})`;
}
imageContainer.addEventListener("mouseleave", () => {
lens.style.display = "none";
image.style.transform = "scale(1)";
});
imageContainer.addEventListener("mousedown", (e) => {
e.preventDefault();
let lensPosX = lens.offsetLeft;
let lensPosY = lens.offsetTop;
let cursorPosX = e.clientX;
let cursorPosY = e.clientY;
document.addEventListener("mousemove", dragLens);
document.addEventListener("mouseup", stopDragging);
function dragLens(e) {
let newCursorPosX = e.clientX;
let newCursorPosY = e.clientY;
let lensNewPosX = lensPosX + newCursorPosX - cursorPosX;
let lensNewPosY = lensPosY + newCursorPosY - cursorPosY;
if (lensNewPosX < lens.offsetWidth / 2) {
lensNewPosX = lens.offsetWidth / 2;
}
if (lensNewPosX > image.width - lens.offsetWidth / 2) {
lensNewPosX = image.width - lens.offsetWidth / 2;
}
if (lensNewPosY < lens.offsetHeight / 2) {
lensNewPosY = lens.offsetHeight / 2;
}
if (lensNewPosY > image.height - lens.offsetHeight / 2) {
lensNewPosY = image.height - lens.offsetHeight / 2;
}
lens.style.left = lensNewPosX + "px";
lens.style.top = lensNewPosY + "px";
let percentageMovedX = (newCursorPosX - cursorPosX) / image.width;
let percentageMovedY = (newCursorPosY - cursorPosY) / image.height;
image.style.transformOrigin = `${-percentageMovedX * 100}% ${-percentageMovedY * 100}%`;
}
function stopDragging() {
document.removeEventListener("mousemove", dragLens);
}
});
- 在页面中添加多个放大镜,并进行对应的样式设置和js代码修改即可。
<div class="container">
<div class="image-container">
<img src="your-image-source" alt="your-image">
<div class="lens"></div>
</div>
<div class="image-container">
<img src="your-image-source" alt="your-image">
<div class="lens"></div>
</div>
<div class="image-container">
<img src="your-image-source" alt="your-image">
<div class="lens"></div>
</div>
</div>
.container {
position: relative;
}
.image-container {
position: relative;
width: 600px;
margin-bottom: 20px;
}
img {
max-width: 100%;
}
.lens {
position: absolute;
display: none;
top: 0;
border: 1px solid #ddd;
}
const lensList = document.querySelectorAll(".lens");
const imageContainerList = document.querySelectorAll(".image-container");
const imageList = document.querySelectorAll("img");
let zoomLevel = 1;
for (let i = 0; i < imageList.length; i++) {
imageList[i].addEventListener("mousemove", (e) => {
moveLens(e, lensList[i], imageList[i]);
});
imageContainerList[i].addEventListener("mouseleave", () => {
lensList[i].style.display = "none";
imageList[i].style.transform = "scale(1)";
});
imageContainerList[i].addEventListener("mousedown", (e) => {
e.preventDefault();
let lensPosX = lensList[i].offsetLeft;
let lensPosY = lensList[i].offsetTop;
let cursorPosX = e.clientX;
let cursorPosY = e.clientY;
document.addEventListener("mousemove", (e) => {
dragLens(e, lensList[i], lensPosX, lensPosY, cursorPosX, cursorPosY, imageList[i]);
});
document.addEventListener("mouseup", () => {
document.removeEventListener("mousemove", dragLens);
});
});
}
function moveLens(e, lens, image) {
const x = e.offsetX;
const y = e.offsetY;
lens.style.display = "block";
const lensWidth = lens.offsetWidth / 2;
const lensHeight = lens.offsetHeight / 2;
let lensX = x - lensWidth;
let lensY = y - lensHeight;
if (lensX > image.width - lensWidth) {
lensX = image.width - lensWidth;
}
if (lensX < lensWidth) {
lensX = lensWidth;
}
if (lensY > image.height - lensHeight) {
lensY = image.height - lensHeight;
}
if (lensY < lensHeight) {
lensY = lensHeight;
}
lens.style.left = lensX + "px";
lens.style.top = lensY + "px";
zoomLevel = (image.width / lens.offsetWidth) * 1.5;
image.style.transform = `scale(${zoomLevel})`;
}
function dragLens(e, lens, lensPosX, lensPosY, cursorPosX, cursorPosY, image) {
let newCursorPosX = e.clientX;
let newCursorPosY = e.clientY;
let lensNewPosX = lensPosX + newCursorPosX - cursorPosX;
let lensNewPosY = lensPosY + newCursorPosY - cursorPosY;
if (lensNewPosX < lens.offsetWidth / 2) {
lensNewPosX = lens.offsetWidth / 2;
}
if (lensNewPosX > image.width - lens.offsetWidth / 2) {
lensNewPosX = image.width - lens.offsetWidth / 2;
}
if (lensNewPosY < lens.offsetHeight / 2) {
lensNewPosY = lens.offsetHeight / 2;
}
if (lensNewPosY > image.height - lens.offsetHeight / 2) {
lensNewPosY = image.height - lens.offsetHeight / 2;
}
lens.style.left = lensNewPosX + "px";
lens.style.top = lensNewPosY + "px";
let percentageMovedX = (newCursorPosX - cursorPosX) / image.width;
let percentageMovedY = (newCursorPosY - cursorPosY) / image.height;
image.style.transformOrigin = `${-percentageMovedX * 100}% ${-percentageMovedY * 100}%`;
}
需要注意的时,如果您需要在同一页面中使用多个图片放大镜,请将JS代码修改为类似上述代码的形式,并对每个镜头使用相应的句柄设置事件监听和回调。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:JavaScript 图片放大镜(可拖放、缩放效果)第4/4页 - Python技术站