在JavaScript中创建一个简单的点击切换大图的电子相册完整代码
时间: 2024-09-22 16:08:50 浏览: 73
在JavaScript中创建一个简单的点击切换大图的电子相册,我们可以使用纯DOM操作,不过为了更简洁,这里我会引入现代的`IntersectionObserver` API 来处理图片的懒加载,并配合`querySelectorAll`选择器和一些基本的事件监听。以下是完整的代码示例:
```html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>简单点击切换大图的电子相册</title>
<style>
.carousel {
display: flex;
justify-content: center;
align-items: center;
height: 400px;
overflow: hidden;
}
img {
display: none; /* 隐藏图片直到加载完成 */
}
.active {
display: block;
}
</style>
</head>
<body>
<div class="carousel">
<img id="slide1" src="image1.jpg">
<img id="slide2" src="image2.jpg">
<!-- 更多图片... -->
</div>
<button id="prevBtn">上一张</button>
<button id="nextBtn">下一张</button>
<script>
const slides = document.querySelectorAll('img');
let currentSlide = 0;
let observer = null;
function showSlide(index) {
for (let i = 0; i < slides.length; i++) {
slides[i].classList.remove('active');
}
slides[index].classList.add('active');
if (observer) {
observer.disconnect(); // 取消旧的观察者
}
if (index > 0 && !slides[index - 1].offsetParent) { // 检查前一张是否可见
slides[index - 1].addEventListener('scroll', loadPrevSlide);
}
if (index < slides.length - 1 && !slides[index + 1].offsetParent) { // 检查后一张是否可见
slides[index + 1].addEventListener('scroll', loadNextSlide);
}
// 设置新的观察者
observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.src = entry.target.dataset.src; // 图片懒加载完成
}
});
}, {
rootMargin: '0px',
threshold: 1.0 // 当图片完全进入可视区域时触发
});
if (!observer.observe(slides[index])) { // 观察新显示的图片
throw new Error("Could not observe the image");
}
}
function loadPrevSlide(e) {
e.preventDefault();
if (currentSlide > 0) {
showSlide(currentSlide - 1);
}
}
function loadNextSlide(e) {
e.preventDefault();
if (currentSlide < slides.length - 1) {
showSlide(currentSlide + 1);
}
}
function init() {
document.getElementById('prevBtn').addEventListener('click', () => {
showSlide(currentSlide - 1);
});
document.getElementById('nextBtn').addEventListener('click', () => {
showSlide(currentSlide + 1);
});
showSlide(0); // 初始化显示第一张
}
window.onload = init;
</script>
</body>
</html>
```
这个代码会根据用户点击“上一张”或“下一张”按钮来切换图片,同时利用`IntersectionObserver`确保只有在图片完全显示在视口内时才加载图片资源,提高了用户体验。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)