Home  >  Article  >  Web Front-end  >  jquery implements mouse hover stop carousel special effect_jquery

jquery implements mouse hover stop carousel special effect_jquery

WBOY
WBOYOriginal
2016-05-16 15:26:333183browse

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

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

2. CSS style

*{
  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;
}

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:

$(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为轮播的时间
      }
    );
    /*鼠标悬浮图片停止轮播代码结束*/
})

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:

/*定时自动轮播图片代码开始*/
    function autoPlay(){
      timer=setInterval(function(){    //打开定时器
      iNow++;             //让图片的索引值次序加1,这样就可以实现顺序轮播图片
      if(iNow>showNumber.length-1){  //当到达最后一张图的时候,让iNow赋值为第一张图的索引值,轮播效果跳转到第一张图重新开始
        iNow=0;
      }
      showNumber.eq(iNow).trigger("click");  //模拟触发数字按钮的click
      },2000);  //2000为轮播的时间
    }
    autoPlay();
 /*定时自动轮播图片代码结束*/

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:

$(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
    );
    /*鼠标悬浮图片停止轮播代码结束*/
})

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