Home Web Front-end JS Tutorial jquery implements mouse operation suspension carousel effect

jquery implements mouse operation suspension carousel effect

Apr 24, 2018 am 11:27 AM
jquery levitate operate

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>
Copy after login

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;
}
Copy after login

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

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();
 /*定时自动轮播图片代码结束*/
Copy after login

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

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

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

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:

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

Forgot your Win8 computer startup password? This operation will restore it immediately! Forgot your Win8 computer startup password? This operation will restore it immediately! Mar 27, 2024 pm 10:12 PM

Forgetting the Win8 computer startup password is a problem that many people encounter when using computers on a daily basis. When we forget the login password, we will be unable to enter the system normally, causing inconvenience to our daily use. If you happen to encounter this problem, don’t worry. Below I will introduce some simple operations to help you quickly restore the power-on password of your Win8 computer. Method 1: Use Microsoft account password. If you use a Microsoft account to log in to your Win8 computer, you can try using the password of that account.

See all articles