Home > Web Front-end > JS Tutorial > How to achieve seamless left and right sliding switching effect of images in JavaScript while limiting them to the container?

How to achieve seamless left and right sliding switching effect of images in JavaScript while limiting them to the container?

WBOY
Release: 2023-10-26 10:31:43
Original
757 people have browsed it

JavaScript 如何实现图片的左右无缝滑动切换效果同时限制在容器内?

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>
Copy after login

Next, we need to use JavaScript to achieve the effect of seamless sliding switching. The specific steps are as follows:

  1. Get the reference of the relevant element:
var container = document.getElementById('container');
var imageList = document.getElementById('imageList');
var images = imageList.getElementsByTagName('img');
var currentIndex = 0; // 当前显示的图片索引
Copy after login
  1. Set the initial style:
// 设置imageList的宽度,保证所有图片水平排列
imageList.style.width = images.length * 100 + '%';

// 设置每张图片的宽度
for (var i = 0; i < images.length; i++) {
  images[i].style.width = 100 / images.length + '%';
}
Copy after login
  1. Implementation Seamless sliding switching effect:
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);
  });
});
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template