Currently, to create more convenience for users and provide the best possible experience, especially for users of our website on smartphones, in addition to simply creating navigation buttons to switch slides, we can integrate swipe gestures to make it easier for users to view slides on our page. Many popular websites also use this function for their slides, such as Tiki and Shopee.


II. Implementation:
Step 1: We will code in HTML.

Step 2: We'll add some CSS to make it look better.

These commands are used later to calculate the data and assign it to translateX to transition slides, and the two commands below are used to create smoother slide-swiping effects.



Step 3: We will now move on to the JavaScript code.

We will use a for loop to add images to the HTML.

We will initialize some variables to store values during the process and create an array of slides to store the classes with the slide names we set above.

We use `forEach` to assign events to each element of the `slides` array. Below are the events that occur when the user swipes the mouse.

![]()
This function is used to get the user's coordinates when a mouse swipe event occurs.

The touchstart event occurs when the user touches an element (after clicking down with the mouse).

This variable is used to store the user's mouse click coordinates when they first click down on the screen.
![]()
This variable is used to check whether the user has clicked on our slide. Since they have clicked, we assign the value = true;
![]()
This event occurs when the user moves their finger across the screen.

This command is used to retrieve the last known position of the mouse cursor after the user has completed a swipe.
![]()
The touchEnd event occurs when the user removes their finger from an element (ending the swipe).

This statement is used to calculate the user's swipe distance. The idea is that if the distance is within the range (-100px ???? 0 && 0 ???? 100px), the slide transition will not occur; only if it is outside this range will the slide transition occur. It also checks whether the user is swiping left or right to proceed to the next slide.
![]()

This command calculates the number we will assign to translateX to execute the next or previous slide.
![]()

And that's how we've completed the swipe-and-slide feature.
2. This function automatically switches slides and assigns events to the next and previous slide buttons. We just need to change the variable currentIndex(slide position) and call the setlaivitri() function.

Author: Thy Dao Quoc - Date written: March 22, 2023
Category: Training program, IT - Website Design, IT - Software Applications, Information Technology - Software Applications, TECHNOLOGY AND ENGINEERING SECTOR



0 thoughts on “TÍCH HỢP DẠNG VUỐT CHO SLIDESHOW”