写一个js页面轮播图宽1350 高500
时间: 2023-05-17 18:06:09 浏览: 104
js图片轮播手动切换效果
可以使用以下代码实现:
HTML部分:
```
<div class="slider">
<img src="image1.jpg" alt="Image 1">
<img src="image2.jpg" alt="Image 2">
<img src="image3.jpg" alt="Image 3">
</div>
```
CSS部分:
```
.slider {
width: 1350px;
height: 500px;
overflow: hidden;
position: relative;
}
.slider img {
width: 1350px;
height: 500px;
position: absolute;
top: 0;
left: 0;
opacity: 0;
transition: opacity 1s ease-in-out;
}
.slider img:first-child {
opacity: 1;
}
.slider img.active {
opacity: 1;
}
.slider .prev, .slider .next {
position: absolute;
top: 50%;
transform: translateY(-50%);
font-size: 30px;
color: #fff;
cursor: pointer;
z-index: 2;
}
.slider .prev {
left: 20px;
}
.slider .next {
right: 20px;
}
```
JavaScript部分:
```
var slider = document.querySelector('.slider');
var images = slider.querySelectorAll('img');
var prev = document.createElement('span');
var next = document.createElement('span');
prev.className = 'prev';
next.className = 'next';
prev.innerHTML = '❮';
next.innerHTML = '❯';
slider.appendChild(prev);
slider.appendChild(next);
var currentImage = 0;
var slideInterval = setInterval(nextSlide, 5000);
function nextSlide() {
images[currentImage].className = '';
currentImage = (currentImage + 1) % images.length;
images[currentImage].className = 'active';
}
function prevSlide() {
images[currentImage].className = '';
currentImage = (currentImage - 1 + images.length) % images.length;
images[currentImage].className = 'active';
}
prev.addEventListener('click', function() {
clearInterval(slideInterval);
prevSlide();
slideInterval = setInterval(nextSlide, 5000);
});
next.addEventListener('click', function() {
clearInterval(slideInterval);
nextSlide();
slideInterval = setInterval(nextSlide, 5000);
});
```
这段代码实现了一个基本的轮播图,每隔5秒钟自动切换到下一张图片,同时也可以通过点击左右箭头手动切换图片。
阅读全文