JavaScript How to achieve the seamless left and right sliding switching effect of images while limiting them to the container?
In web development, we often encounter situations where we need to achieve a picture carousel effect. This article will introduce how to use JavaScript to achieve the seamless left and right sliding switching effect of images and limit them to specified containers.
First, we need to create a container in HTML to display images. This container can be a div element. We give it a fixed width and height, and set overflow to hidden to limit the display range. The code is as follows:
<div id="container"> <ul id="imageList"> <li><img src="image1.jpg"/></li> <li><img src="image2.jpg"/></li> <li><img src="image3.jpg"/></li> ... </ul> </div>
Next, we need to use JavaScript to achieve the effect of seamless sliding switching. The specific steps are as follows:
var container = document.getElementById('container'); var imageList = document.getElementById('imageList'); var images = imageList.getElementsByTagName('img'); var currentIndex = 0; // 当前显示的图片索引
// 设置imageList的宽度,保证所有图片水平排列 imageList.style.width = images.length * 100 + '%'; // 设置每张图片的宽度 for (var i = 0; i < images.length; i++) { images[i].style.width = 100 / images.length + '%'; }
function slideTo(index) { // 计算需要滑动的距离 var distance = -index * container.offsetWidth; // 设置imageList的动画效果 imageList.style.transition = 'transform 0.5s ease'; imageList.style.transform = 'translate(' + distance + 'px, 0)'; } function reset() { // 当滑动到最后一张图时,切换到第一张图 if (currentIndex === images.length) { currentIndex = 0; } // 当滑动到第一张图之前时,切换到最后一张图 if (currentIndex < 0) { currentIndex = images.length - 1; } // 移除过渡效果,快速切换到目标位置 imageList.style.transition = 'none'; imageList.style.transform = 'translate(' + (-currentIndex * container.offsetWidth) + 'px, 0)'; } function slideNext() { currentIndex++; slideTo(currentIndex); } function slidePrev() { currentIndex--; slideTo(currentIndex); } // 监听容器的滑动事件 container.addEventListener('touchstart', function (event) { var startTouchPos = event.touches[0].clientX; var lastTouchPos = startTouchPos; // 监听容器的滑动过程 container.addEventListener('touchmove', function (event) { var currentTouchPos = event.touches[0].clientX; var diff = currentTouchPos - lastTouchPos; // 判断滑动方向 if (diff > 0) { slideNext(); } else { slidePrev(); } lastTouchPos = currentTouchPos; }); // 监听容器的滑动结束事件 container.addEventListener('touchend', function (event) { reset(); container.removeEventListener('touchmove', onTouchMove); container.removeEventListener('touchend', onTouchEnd); }); });
Through the above code, we realize the seamless sliding switching effect of pictures and limit it to the specified container. When the picture in the container is touched and slid, it switches to the corresponding picture according to the sliding direction. When switching to the last picture, switching again will jump to the first picture, and vice versa.
I hope this article will be helpful in understanding how to use JavaScript to achieve the seamless left and right sliding switching effect of images.
The above is the detailed content of How to achieve seamless left and right sliding switching effect of images in JavaScript while limiting them to the container?. For more information, please follow other related articles on the PHP Chinese website!