Two people looking at a computer Description automatically generated with medium confidence

INTEGRATED SWIPE STYLE FOR SLIDESHOWS

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.

Two people looking at a computer Description automatically generated with medium confidence

Shopee product slideshow

Graphical user interface Description automatically generated

Tiki product slides

II. Implementation:

Step 1: We will code in HTML.

A screenshot of a computer Description automatically generated with medium confidence

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

Text Description automatically generated

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.

Text Description automatically generated

Text Description automatically generated

Text Description automatically generated

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

Background pattern Description automatically generated with low confidence

This is when we have finished coding the HTML and CSS above.

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

Text Description automatically generated

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.

Graphical user interface, text, application Description automatically generated

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.

Text Description automatically generated

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).

Text Description automatically generated

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.

Text Description automatically generated

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).

Text Description automatically generated

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.

Text Description automatically generated

This command calculates the number we will assign to translateX to execute the next or previous slide.

Graphical user interface, text Description automatically generated

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.

Text Description automatically generated

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”

Leave a Reply

Your email address will not be published. Các trường bắt buộc được đánh dấu *