Home  >  Article  >  Web Front-end  >  jquery implements mouse operation suspension carousel effect

jquery implements mouse operation suspension carousel effect

php中世界最好的语言
php中世界最好的语言Original
2018-04-24 11:27:212218browse

This time I will bring you jquery to implement the mouse operation suspension carousel effect. What are the precautions for jquery to realize the mouse operation suspension carousel effect. The following is a practical case, let's take a look.

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>
  <p 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>
   <!--图片布局结束-->
   
   <!--按钮布局开始-->
   <p class="showNav">
    <span class="active">1</span>
    <span>2</span>
    <span>3</span>
    <span>4</span>
   </p>
   <!--按钮布局结束-->
  </p>
  <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 Let’s first talk about the principle of stopping the mouse hover image carousel:

  • 1. When the mouse hovers over the frame When the mouse is above 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) is a simulated hover event (the mouse moves to an object and method to remove this 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 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 you can see from the above picture, the code for starting the timer is repeated, so here you can define an automatic playback function autoPlay() to streamline the code. The streamlined 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();
 /*定时自动轮播图片代码结束*/

After the definition is completed, don’t forget to call this function, that is, 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
  );
  /*鼠标悬浮图片停止轮播代码结束*/
})

I believe you have read this article You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

jQuery realizes the picture carousel slide effect

jquery drag effect implementation method

The above is the detailed content of jquery implements mouse operation suspension carousel effect. For more information, please follow other related articles on the PHP Chinese website!

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