jQuery 是一个快速、简洁的 JavaScript 库,它简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。使用 jQuery 制作焦点图是一种常见的网页设计技巧,用于展示一系列图片,并允许用户通过点击按钮或自动切换来查看不同的图片。
焦点图通常由一组图片和一个控制组件(如导航按钮或指示器)组成。用户可以通过点击导航按钮或等待自动切换来查看不同的图片。
以下是一个简单的 jQuery 焦点图示例,包含手动切换和自动切换功能:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Focus Image</title>
<style>
#focus {
width: 600px;
height: 400px;
overflow: hidden;
position: relative;
}
#focus img {
width: 100%;
height: 100%;
position: absolute;
opacity: 0;
transition: opacity 1s;
}
#focus img.active {
opacity: 1;
}
.nav {
position: absolute;
bottom: 10px;
width: 100%;
text-align: center;
}
.nav button {
margin: 0 5px;
}
</style>
</head>
<body>
<div id="focus">
<img src="image1.jpg" alt="Image 1" class="active">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
<div class="nav">
<button class="btn" data-index="0">1</button>
<button class="btn" data-index="1">2</button>
<button class="btn" data-index="2">3</button>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
let currentIndex = 0;
const $images = $('#focus img');
const $buttons = $('.nav button');
function showImage(index) {
$images.removeClass('active');
$images.eq(index).addClass('active');
}
$buttons.click(function() {
const index = $(this).data('index');
currentIndex = index;
showImage(currentIndex);
});
setInterval(function() {
currentIndex = (currentIndex + 1) % $images.length;
showImage(currentIndex);
}, 3000); // 自动切换时间间隔为3秒
});
</script>
</body>
</html>
transition
属性应用在正确的元素上。通过以上步骤,你可以创建一个基本的 jQuery 焦点图,并根据需要进行扩展和优化。
没有搜到相关的文章