Home Web Front-end JS Tutorial jquery implements mouse hover stop carousel special effect_jquery

jquery implements mouse hover stop carousel special effect_jquery

May 16, 2016 pm 03:26 PM
jquery Carousel

The example in this article describes the jquery implementation of mouse hover stop carousel special effects code. Share it with everyone for your reference. The details are as follows:
The screenshot of the running effect is as follows:

The specific code is as follows:

1. Main program

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

<!DOCTYPE html>

<html>

  <head>

    <meta charset="utf-8" />

    <title>轮播图①(手动点击轮播)</title>

    <link type="text/css" rel="stylesheet" href="css/layout.css" />

  </head>

  <body>

    <div class="slideShow">

      <!--图片布局开始-->

      <ul>

        <li><a href="#"><img src="img/picture01.jpg" /></a></li>

        <li><a href="#"><img src="img/picture02.jpg" /></a></li>

        <li><a href="#"><img src="img/picture03.jpg" /></a></li>

        <li><a href="#"><img src="img/picture04.jpg" /></a></li>

      </ul>

      <!--图片布局结束-->

       

      <!--按钮布局开始-->

      <div class="showNav">

        <span class="active">1</span>

        <span>2</span>

        <span>3</span>

        <span>4</span>

      </div>

      <!--按钮布局结束-->

    </div>

    <script src="js/jquery-1.11.3.js"></script>

    <script src="js/layout.js"></script>

  </body>

</html>

Copy after login

2. CSS style

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

*{

  margin: 0;

  padding: 0;

}

ul{

  list-style: none;

}

.slideShow{

  width: 346px;

  height: 210px;   /*其实就是图片的高度*/

  border: 1px #eeeeee solid;

  margin: 100px auto;

  position: relative;

  overflow: hidden;  /*此处需要将溢出框架的图片部分隐藏*/

}

.slideShow ul{

  width: 2000px;

  position: relative;   /*此处需注意relative : 对象不可层叠,但将依据left,right,top,bottom等属性在正常文档流中偏移位置,如果没有这个属性,图片将不可左右移动*/

}

.slideShow ul li{

  float: left;   /*让四张图片左浮动,形成并排的横着布局,方便点击按钮时的左移动*/

  width: 346px;

}

.slideShow .showNav{   /*用绝对定位给数字按钮进行布局*/

  position: absolute;

  right: 10px;

  bottom: 5px;

  text-align:center;

  font-size: 12px; 

  line-height: 20px;

}

.slideShow .showNav span{

  cursor: pointer;

  display: block;

  float: left;

  width: 20px;

  height: 20px;

  background: #ff5a28;

  margin-left: 2px;

  color: #fff;

}

.slideShow .showNav .active{

  background: #b63e1a;

}

Copy after login

3. jQuery program
First let’s talk about the principle of stopping the mouse hover image carousel:

  • 1. When the mouse is hovering over the frame, clear the timer and use clearInterval(timer) to close the timer to stop the automatic carousel
  • 2. When the mouse leaves the frame, restart the timer
  • 3. The hover function is used to hover and leave the mouse

hover(over,out) A method that simulates hover events (the mouse moves over and out of an object). This is a custom method that provides a "keep in it" state for frequently used tasks.
Parameters:
Over (Function): The function to be triggered when the mouse moves over the element.
out (Function): The function to be triggered when the mouse moves out of the element.

Let’s take a look at the jQuery program:

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

$(document).ready(function(){

  var slideShow=$(".slideShow"),   //获取最外层框架的名称

    ul=slideShow.find("ul"),  

    showNumber=slideShow.find(".showNav span"),//获取按钮

    oneWidth=slideShow.find("ul li").eq(0).width();  //获取每个图片的宽度

    var timer=null;  //定时器返回值,主要用于关闭定时器

    var iNow=0;   //iNow为正在展示的图片索引值,当用户打开网页时首先显示第一张图,即索引值为0

     

    /*手动点击按钮进行图片轮播代码开始*/

    showNumber.on("click",function(){      //为每个按钮绑定一个点击事件  

      $(this).addClass("active").siblings().removeClass("active");  //按钮点击时为这个按钮添加高亮状态,并且将其他按钮高亮状态去掉

      var index=$(this).index(); //获取哪个按钮被点击,也就是找到被点击按钮的索引值

      iNow=index;

      ul.animate({

        "left":-oneWidth*iNow,  //注意此处用到left属性,所以ul的样式里面需要设置position: relative; 让ul左移N个图片大小的宽度,N根据被点击的按钮索引值iNow确定

      })

    });

    /*手动点击按钮进行图片轮播代码结束*/

     

     

    /*定时自动轮播图片代码开始*/

    timer=setInterval(function(){    //打开定时器

      iNow++;             //让图片的索引值次序加1,这样就可以实现顺序轮播图片

      if(iNow>showNumber.length-1){  //当到达最后一张图的时候,让iNow赋值为第一张图的索引值,轮播效果跳转到第一张图重新开始

        iNow=0;

      }

      showNumber.eq(iNow).trigger("click");  //模拟触发数字按钮的click

    },2000);  //2000为轮播的时间

    /*定时自动轮播图片代码结束*/

     

    /*鼠标悬浮图片停止轮播代码开始*/

    slideShow.hover(

      function(){

        clearInterval(timer);

      },function(){

        timer=setInterval(function(){    //打开定时器

          iNow++;             //让图片的索引值次序加1,这样就可以实现顺序轮播图片

          if(iNow>showNumber.length-1){  //当到达最后一张图的时候,让iNow赋值为第一张图的索引值,轮播效果跳转到第一张图重新开始

            iNow=0;

          }

          showNumber.eq(iNow).trigger("click");  //模拟触发数字按钮的click

        },2000);  //2000为轮播的时间

      }

    );

    /*鼠标悬浮图片停止轮播代码结束*/

})

Copy after login

As can be seen from the picture above, the code for starting the timer is repeated, so here you can define an automatic playback function autoPlay() to simplify the code. The simplified code is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

/*定时自动轮播图片代码开始*/

    function autoPlay(){

      timer=setInterval(function(){    //打开定时器

      iNow++;             //让图片的索引值次序加1,这样就可以实现顺序轮播图片

      if(iNow>showNumber.length-1){  //当到达最后一张图的时候,让iNow赋值为第一张图的索引值,轮播效果跳转到第一张图重新开始

        iNow=0;

      }

      showNumber.eq(iNow).trigger("click");  //模拟触发数字按钮的click

      },2000);  //2000为轮播的时间

    }

    autoPlay();

 /*定时自动轮播图片代码结束*/

Copy after login

Don’t forget to call this function after the definition is completed, i.e. autoPlay();
Then the final version of the jQuery program is as follows:

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

$(document).ready(function(){

  var slideShow=$(".slideShow"),   //获取最外层框架的名称

    ul=slideShow.find("ul"),  

    showNumber=slideShow.find(".showNav span"),//获取按钮

    oneWidth=slideShow.find("ul li").eq(0).width();  //获取每个图片的宽度

    var timer=null;  //定时器返回值,主要用于关闭定时器

    var iNow=0;   //iNow为正在展示的图片索引值,当用户打开网页时首先显示第一张图,即索引值为0

     

    /*手动点击按钮进行图片轮播代码开始*/

    showNumber.on("click",function(){      //为每个按钮绑定一个点击事件  

      $(this).addClass("active").siblings().removeClass("active");  //按钮点击时为这个按钮添加高亮状态,并且将其他按钮高亮状态去掉

      var index=$(this).index(); //获取哪个按钮被点击,也就是找到被点击按钮的索引值

      iNow=index;

      ul.animate({

        "left":-oneWidth*iNow,  //注意此处用到left属性,所以ul的样式里面需要设置position: relative; 让ul左移N个图片大小的宽度,N根据被点击的按钮索引值iNow确定

      })

    });

    /*手动点击按钮进行图片轮播代码结束*/

     

     

    /*定时自动轮播图片代码开始*/

    function autoPlay(){

      timer=setInterval(function(){    //打开定时器

      iNow++;             //让图片的索引值次序加1,这样就可以实现顺序轮播图片

      if(iNow>showNumber.length-1){  //当到达最后一张图的时候,让iNow赋值为第一张图的索引值,轮播效果跳转到第一张图重新开始

        iNow=0;

      }

      showNumber.eq(iNow).trigger("click");  //模拟触发数字按钮的click

      },2000);  //2000为轮播的时间

    }

    autoPlay();

    /*定时自动轮播图片代码结束*/

     

    /*鼠标悬浮图片停止轮播代码开始*/

    slideShow.hover(

      function(){

        clearInterval(timer);

      },autoPlay

    );

    /*鼠标悬浮图片停止轮播代码结束*/

})

Copy after login

The above is the entire content of this article. You can combine the following two articles to study:

Article 1: jQuery manual click to achieve image carousel effects

Article 2: jquery implements scheduled automatic carousel effects

I hope this article will be helpful to everyone learning jQuery programming.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Detailed explanation of jQuery reference methods: Quick start guide Detailed explanation of jQuery reference methods: Quick start guide Feb 27, 2024 pm 06:45 PM

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to remove the height attribute of an element with jQuery? How to remove the height attribute of an element with jQuery? Feb 28, 2024 am 08:39 AM

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

In-depth analysis: jQuery's advantages and disadvantages In-depth analysis: jQuery's advantages and disadvantages Feb 27, 2024 pm 05:18 PM

jQuery is a fast, small, feature-rich JavaScript library widely used in front-end development. Since its release in 2006, jQuery has become one of the tools of choice for many developers, but in practical applications, it also has some advantages and disadvantages. This article will deeply analyze the advantages and disadvantages of jQuery and illustrate it with specific code examples. Advantages: 1. Concise syntax jQuery's syntax design is concise and clear, which can greatly improve the readability and writing efficiency of the code. for example,

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

See all articles