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?

Oct 26, 2023 am 10:31 AM
Switch effect Seamless sliding In-container restrictions

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:

1

2

3

4

5

6

7

8

<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:

1

2

3

4

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:

1

2

3

4

5

6

7

// 设置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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use WeChat applet to achieve carousel switching effect Use WeChat applet to achieve carousel switching effect Nov 21, 2023 pm 05:59 PM

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the &lt;swiper&gt; tag to achieve the switching effect of the carousel. In this component, you can pass b

How to implement menu bar switching effect in JavaScript? How to implement menu bar switching effect in JavaScript? Oct 18, 2023 am 09:45 AM

How does JavaScript implement the switching effect of the menu bar? In web development, the switching effect of the menu bar is a very common function. Through JavaScript, we can achieve the switching effect of the menu bar, allowing users to switch between different menus and display the corresponding content. Next, I will introduce how JavaScript can achieve the switching effect of the menu bar through specific code examples. First, we need to define the structure of the menu bar in HTML. Here is a simple example

How to achieve seamless left and right sliding switching effect of images in JavaScript? How to achieve seamless left and right sliding switching effect of images in JavaScript? Oct 19, 2023 am 08:56 AM

How to achieve seamless left and right sliding switching effect of images with JavaScript? With the development of the Internet, images are often used as important elements of the page in web design. The switching effect of pictures plays an important impact on the beauty and interactivity of the page. In this article, we will explore how to use JavaScript to achieve a seamless left-right sliding switching effect of images, and attach specific code examples. To achieve the seamless left and right sliding switching effect of pictures, you first need to do the following: Create a picture container and use

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? Oct 26, 2023 am 10:31 AM

How to achieve seamless left and right sliding switching effect of images in JavaScript 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 h

Use JavaScript to achieve page sliding switching effect Use JavaScript to achieve page sliding switching effect Aug 09, 2023 pm 01:57 PM

Using JavaScript to achieve page sliding switching effects In modern web page design, page sliding switching effects have become a common design requirement, which can improve user experience and increase page interactivity. This article will achieve this effect through JavaScript. First, we need to add some basic structure and styling to the HTML. Here's a simple example: &lt;!DOCTYPEhtml&gt;&lt;html&gt;&lt;head&gt

How to implement tab switching effect using JavaScript? How to implement tab switching effect using JavaScript? Oct 20, 2023 pm 12:58 PM

How to implement tab switching effect using JavaScript? The tab switching effect is a common interactive effect in web pages. It allows users to switch content without refreshing the page, providing a better user experience. To achieve this effect, we can use JavaScript to process it. The idea of ​​realizing the tab switching effect is to display the corresponding content by clicking on different tab buttons. Below we will introduce in detail how to use JavaScript to achieve the tab switching effect and provide

How to use JavaScript to achieve the finger sliding switching effect of tab content while limiting it to the container? How to use JavaScript to achieve the finger sliding switching effect of tab content while limiting it to the container? Oct 20, 2023 am 11:31 AM

How to use JavaScript to achieve the finger sliding switching effect of tab content while limiting it to the container? Tabs are a common web page layout that can switch to display different content in the same area. Compared with the traditional click switching method, the finger sliding switching effect is more friendly and intuitive on mobile devices. This article will introduce how to use JavaScript to implement the finger sliding switching effect of tab content and limit it to the container. First, we need an HTML structure to host the tab content. Fake

How to use Layui to achieve picture sliding switching effect How to use Layui to achieve picture sliding switching effect Oct 26, 2023 am 10:12 AM

How to use Layui to achieve the image sliding switching effect requires specific code examples. Layui is a lightweight front-end UI framework. It provides a wealth of components and interfaces, making page development more convenient and faster. In this article, I will introduce how to use Layui to achieve the picture sliding switching effect, and provide specific code examples. First, introduce Layui's core files and style files into the HTML page. &lt;linkrel="stylesheet&qu

See all articles