Using jQuery to implement the carousel effect

Using jQuery to implement the carousel effect

This article shares the specific code for implementing the carousel chart with jQuery for your reference. The specific content is as follows

(With small dots and left and right arrow switching effects)

Principle: define indexes, schedule tasks to switch between carousels, and also switch the style of small dots when switching

 var j = 0;//Define the index, the picture and the small dot are shared var cusTimer;//Define the timing function $('.cons-middle .cons-mid').eq(0).show().siblings().hide();//Define the default display image, which is the image with index 0 cusStart();//Start to implement the image carousel, using the timer $('.luobo-circle li').hover(function(){//When the mouse moves to a small dot, switch the image clearInterval(cusTimer);//And clear the timing j=$(this).index();//Get the index of the small dot that the mouse currently moves to cusChange();//Execute the function of switching images });
        $('.luobo-circle li').mouseleave(function(){
            cusStart(); //Define that when the mouse leaves the small dot, the timing function will continue to execute and the carousel will start });
        
        $('.cons-left img').click(() => {
            j--;
            if (j < 0) {
                j = 3;
            };
            cusChange();
            $('.luobo-circle li').eq(j).css('background-color','#4C80E7')
            $('.luobo-circle li').eq(j).siblings().css('background-color','#B5C9F3');
        });
        
        $('.cons-right img').click(() => {
            j++;
            if (j > 3) {
                j = 0;
            }
            cusChange();
            $('.luobo-circle li').eq(j).css('background-color','#4C80E7')
            $('.luobo-circle li').eq(j).siblings().css('background-color','#B5C9F3');
        })
        function cusStart(){//Carousel start function cusTimer = setInterval(function(){//Automatic carousel timing function j++;//Index is accumulated to prevent only one picture from being displayed if(j==4){
                    j=0;//I am using 8 pictures here. When the index is 8, the picture is gone, so the index is cleared to zero}
                cusChange(); //Continue to execute the picture carousel},5000) //2000 is how often the picture is switched, which means two seconds};
        function cusChange(){//Picture display function, where fadeOut and fadeIn are the picture display modes of fading in and out$('.cons-middle .cons-mid').eq(j).fadeIn(300).siblings().stop().fadeOut(300);
            //eq selects the current image, siblings excludes other images, stop stops switching other images and only switches the current image$('.luobo-circle li').eq(j).css('background-color','#4C80E7')
            $('.luobo-circle li').eq(j).siblings().css('background-color','#B5C9F3');
        }

(No dots, only automatic rotation and left and right switching)

//Home page banner carousel var i = 0;
    var bannerTimer;
    function bannerChange(){//Image display function, where fadeOut and fadeIn are the image display modes of fading in and out $('.banner ul li').eq(i).fadeIn(300).siblings().stop().fadeOut(300);
    }
    function bannerStart(){//Carousel start function bannerTimer = setInterval(function(){//Automatic carousel timing function i++;
            if(i==2){
                i=0;
            }
            bannerChange();
        },3000)
    };
    $('.banner ul li').eq(0).show().siblings().hide();
    bannerStart();
    $('.pagination .prev').click(() => {
        i--;
        if (i < 0) {
            i = 2;
        };
        bannerChange();
    });
    $('.pagination .next').click(() => {
       i++;
        if (i > 2) {
            i = 0;
        }
        bannerChange();
    });

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • jQuery implements carousel map detailed explanation and example code
  • Using jQuery to write left and right carousel effects
  • jQuery implements seamless left and right carousel
  • jQuery implements carousel chart and its principle detailed explanation
  • jQuery plugin slides to achieve seamless carousel effects
  • jQuery implements left and right sliding carousel
  • jQuery adaptive carousel plug-in Swiper usage example
  • JQuery and html+css to achieve a carousel with small dots and left and right buttons
  • Simple carousel function implemented by jQuery [suitable for beginners]
  • Implementing fade-in and fade-out effect carousel based on jQuery

<<:  jQuery implements article collapse and expansion functions

>>:  Summary of Linux environment variable configuration methods (differences between .bash_profile and .bashrc)

Recommend

Detailed explanation of the use of stat function and stat command in Linux

stat function and stat command Explanation of [in...

Analysis of mysql view functions and usage examples

This article uses examples to illustrate the func...

How to introduce scss into react project

First download the dependencies yarn add sass-loa...

Sample code using vue-router in html

Introducing vue and vue-router <script src=&qu...

Detailed explanation of the use of vue-resource interceptors

Preface Interceptor In some modern front-end fram...

Nginx's practical method for solving cross-domain problems

Separate the front and back ends and use nginx to...

How to solve the problem that Seata cannot use MySQL 8 version

Possible reasons: The main reason why Seata does ...

How to install suPHP for PHP5 on CentOS 7 (Peng Ge)

By default, PHP on CentOS 7 runs as apache or nob...

SQL method for calculating timestamp difference

SQL method for calculating timestamp difference O...

(MariaDB) Comprehensive explanation of MySQL data types and storage mechanisms

1.1 Data Type Overview The data type is a field c...

Learn MySQL execution plan

Table of contents 1. Introduction to the Implemen...

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

Detailed steps for installing and configuring mysql 5.6.21

1. Overview MySQL version: 5.6.21 Download addres...