要创建一个像这样的无限全屏滑块,可以通过以下步骤实现:
以下是一个示例代码:
HTML:
<div class="slider-container">
<div class="slider">
<div class="slide">Slide 1</div>
<div class="slide">Slide 2</div>
<div class="slide">Slide 3</div>
</div>
</div>
CSS:
.slider-container {
width: 100%;
height: 100vh;
overflow: hidden;
}
.slider {
display: flex;
width: 300%;
height: 100vh;
}
.slide {
width: 33.33%;
height: 100vh;
}
JavaScript:
const slider = document.querySelector('.slider');
let currentIndex = 0;
function handleSlide(event) {
const direction = event.deltaY > 0 ? 1 : -1;
currentIndex += direction;
if (currentIndex < 0) {
currentIndex = 0;
} else if (currentIndex > 2) {
currentIndex = 2;
}
slider.style.transform = `translateX(-${currentIndex * 33.33}%)`;
}
window.addEventListener('wheel', handleSlide);
这个示例代码创建了一个包含三个滑块的容器,滑块可以通过鼠标滚轮向上或向下滑动。滑块的宽度为容器的三倍,通过改变滑块容器的transform属性来实现滑动效果。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云