小缩略图在一个位置以稍大的形式展现,鼠标点击小缩略图,大图更换,鼠标精工大图某个部分,该部分以放大镜的形式放大显示
时间: 2024-03-09 15:49:09 浏览: 61
实现您所提到的这种效果,需要使用 JavaScript 和 CSS 技术。以下是一种实现方式:
1. HTML部分:
```html
<div class="thumbnail">
<img src="small.jpg" alt="Thumbnail">
<div class="magnifier"></div>
</div>
<div class="large-image">
<img src="large.jpg" alt="Large Image">
</div>
```
2. CSS部分:
```css
.thumbnail {
position: relative;
width: 200px;
height: 200px;
overflow: hidden;
}
.thumbnail img {
display: block;
width: 100%;
height: 100%;
}
.magnifier {
position: absolute;
top: 0;
left: 0;
width: 100px;
height: 100px;
border: 1px solid #ccc;
display: none;
cursor: move;
}
.large-image {
position: relative;
margin-top: 20px;
}
.large-image img {
display: block;
width: 100%;
}
```
3. JavaScript部分:
```javascript
// 获取DOM元素
var thumbnail = document.querySelector('.thumbnail');
var magnifier = document.querySelector('.magnifier');
var largeImage = document.querySelector('.large-image img');
// 当鼠标悬停在小图上时,显示放大镜和大图
thumbnail.addEventListener('mouseenter', function(e) {
magnifier.style.display = 'block';
largeImage.src = 'large.jpg';
});
// 当鼠标移出小图时,隐藏放大镜和大图
thumbnail.addEventListener('mouseleave', function(e) {
magnifier.style.display = 'none';
largeImage.src = '';
});
// 当鼠标在小图上移动时,移动放大镜,同时更新大图的显示区域
thumbnail.addEventListener('mousemove', function(e) {
var thumbnailRect = thumbnail.getBoundingClientRect();
var magnifierRect = magnifier.getBoundingClientRect();
var x = e.clientX - thumbnailRect.left - magnifierRect.width / 2;
var y = e.clientY - thumbnailRect.top - magnifierRect.height / 2;
x = Math.max(0, x);
y = Math.max(0, y);
x = Math.min(thumbnailRect.width - magnifierRect.width, x);
y = Math.min(thumbnailRect.height - magnifierRect.height, y);
magnifier.style.left = x + 'px';
magnifier.style.top = y + 'px';
largeImage.style.left = -x * 2 + 'px';
largeImage.style.top = -y * 2 + 'px';
});
// 当鼠标按下放大镜时,禁用文本选择和拖拽
magnifier.addEventListener('mousedown', function(e) {
e.preventDefault();
magnifier.style.cursor = 'move';
thumbnail.style.userSelect = 'none';
thumbnail.style.MozUserSelect = 'none';
thumbnail.style.webkitUserSelect = 'none';
thumbnail.style.msUserSelect = 'none';
document.addEventListener('mousemove', moveMagnifier);
document.addEventListener('mouseup', releaseMagnifier);
});
// 移动放大镜
function moveMagnifier(e) {
var thumbnailRect = thumbnail.getBoundingClientRect();
var magnifierRect = magnifier.getBoundingClientRect();
var x = e.clientX - thumbnailRect.left - magnifierRect.width / 2;
var y = e.clientY - thumbnailRect.top - magnifierRect.height / 2;
x = Math.max(0, x);
y = Math.max(0, y);
x = Math.min(thumbnailRect.width - magnifierRect.width, x);
y = Math.min(thumbnailRect.height - magnifierRect.height, y);
magnifier.style.left = x + 'px';
magnifier.style.top = y + 'px';
largeImage.style.left = -x * 2 + 'px';
largeImage.style.top = -y * 2 + 'px';
}
// 释放放大镜
function releaseMagnifier(e) {
magnifier.style.cursor = 'default';
thumbnail.style.userSelect = 'auto';
thumbnail.style.MozUserSelect = 'auto';
thumbnail.style.webkitUserSelect = 'auto';
thumbnail.style.msUserSelect = 'auto';
document.removeEventListener('mousemove', moveMagnifier);
document.removeEventListener('mouseup', releaseMagnifier);
}
```
以上代码实现了一个简单的图片放大镜效果,您可以根据需要进行修改和优化。
阅读全文