基础概念: jQuery鼠标滑过图片放大镜效果是一种常见的网页交互设计,它允许用户在鼠标悬停在图片上时,显示一个放大的图片视图。这种效果通常通过CSS和JavaScript(特别是jQuery库)来实现。
优势:
类型:
应用场景:
常见问题及解决方法:
position
属性设置为absolute
,并正确计算其相对于图片的位置。示例代码: 以下是一个简单的jQuery鼠标滑过图片放大镜效果的实现示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Image Zoom Effect</title>
<style>
.zoom-container {
position: relative;
display: inline-block;
}
.zoom-lens {
position: absolute;
border: 1px solid #d4d4d4;
width: 100px;
height: 100px;
background-color: rgba(255, 255, 255, 0.4);
cursor: none;
}
.zoom-result {
position: absolute;
top: 0;
right: -40%;
width: 400px;
height: 400px;
border: 1px solid #d4d4d4;
overflow: hidden;
}
.zoom-result img {
position: absolute;
width: 900px;
height: auto;
}
</style>
</head>
<body>
<div class="zoom-container">
<img src="small-image.jpg" alt="Sample Image" id="image">
<div class="zoom-lens"></div>
<div class="zoom-result"><img src="large-image.jpg" alt="Zoomed Image"></div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
var native_width = 0;
var native_height = 0;
var mouse = {x: 0, y: 0};
var zoom = 3; // 放大倍数
var image = $("#image");
var lens = $(".zoom-lens");
var result = $(".zoom-result");
image.on("mousemove", function(e){
mouse.x = e.pageX - this.offsetLeft;
mouse.y = e.pageY - this.offsetTop;
if(mouse.x < 0 || mouse.x > this.width || mouse.y < 0 || mouse.y > this.height) {
lens.hide();
result.hide();
} else {
lens.show();
result.show();
}
var lens_x = (mouse.x / this.width) * 100;
var lens_y = (mouse.y / this.height) * 100;
lens.css({
left: lens_x + '%',
top: lens_y + '%'
});
result.css({
backgroundImage: 'url(' + image.attr('src') + ')',
backgroundSize: (this.width * zoom) + 'px ' + (this.height * zoom) + 'px',
backgroundPosition: '-' + (mouse.x * zoom - lens.width() / 2) + 'px -' + (mouse.y * zoom - lens.height() / 2) + 'px'
});
});
image.on("mouseleave", function(){
lens.hide();
result.hide();
});
});
</script>
</body>
</html>
这个示例展示了如何使用jQuery和CSS创建一个简单的鼠标滑过图片放大镜效果。你可以根据需要调整放大倍数和其他参数。
没有搜到相关的文章