


Implementing left and right image carousels based on jQuery (general principle)_jquery
This article shares the jQuery code for implementing left and right image carousels for your reference. The specific implementation content is as follows
Operation rendering:
Point! ! !
Implementation principle:
Determine whether the image is moved left or right by judging the change in the index value. By controlling the left value of the image, a carousel effect is achieved.
Specific code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <script src="js/jquery.min.js"></script> <style> .banner{ margin:0 auto; border: 4px dashed black; width:400px; height:200px; position: relative; overflow:hidden; } .banner a{ z-index: 100; display: block; width:100%; height: 100%; position: absolute; left:100%; top:0; } .banner .first{ left:0; } .banner a img{ width:100%; height: 100%; } .choose{ z-index: 1000; position: absolute; left:150px; top:180px; width:100px; height: 10px; } .choose span{ margin-right: 15px; float: left; display:block; background: blue; width:10px; height: 10px; border-radius: 10px; } .choose span:hover{ background: red; } .choose .red{ background: red; } .banner .pre,.next{ cursor:pointer; text-align:center; border-radius:20px; display:block; background:#cccccc; opacity:0.4; text-decoration: none; z-index: 200; display:block; width:40px; height: 40px; font-size: 40px; color:red; position: absolute; top:80px; } .banner .pre{ left:0px } .banner .next{ right: 0px; } </style> <body> <div class="banner"> <!-- 这里为上一页下一页点击按钮 --> <span class="pre">-</span> <span class="next">+</span> <!-- 此处为轮播主体,颜色块代替。图片自加 --> <a href="###" class="first" style="background: pink;"></a> <a href="###" style="background: blue;"><img src="images/banner1.jpg"/></a> <a href="###" style="background: greenyellow;"><img src="images/banner2.jpg"/></a> <a href="###" style="background: deepskyblue;"><img src="images/banner3.jpg"/></a> <!-- 此处为轮播部分下方小点选择 --> <div class="choose"> <span class="red"></span> <span></span> <span></span> <span></span> </div> </div> <script> /*定义两个变量,保存当前页码和上一页页码*/ var $index=0; var $exdex=0; /*小点的鼠标移入事件,触发图片左移还是右移*/ $(".choose span").mouseover(function(){ //获取当前移入的index值 $index=$(this).index(); //首先让点的颜色变化,表示选中 $(".choose span").eq($index).addClass("red").siblings(). removeClass("red"); //判断如果index变小,证明图片要往左移动。变大则为右移 if($index>$exdex){ next(); }else if($index<$exdex){ pre(); } //移动完毕将当前index值替换了前页index return $exdex=$index; }); //下一页的点击事件。在右移基础上加了最大index判断 $(".next").click(function(){ $index++; if($index>3){ $index=0 } $(".choose span").eq($index).addClass("red").siblings(). removeClass("red"); next(); return $exdex=$index; }); //上一页的点击事件 $(".pre").click(function(){ $index--; if($index<0){ $index=3 }; $(".choose span").eq($index).addClass("red").siblings(). removeClass("red"); pre(); return $exdex=$index; }); //加个定时器,正常轮播 var atime=setInterval(function(){ $(".next").click(); },1000); //这里为右移和左移的事件函数。 //右移基本原理就是先让exdex定位的left左移百分百,而选中的当前页从屏幕右边移入,left变为0 function next(){ $(".banner a").eq($index).stop(true,true). css("left","100%").animate({"left":"0"}); $(".banner a").eq($exdex).stop(true,true). css("left","0").animate({"left":"-100%"}); } function pre(){ $(".banner a").eq($index).stop(true,true). css("left","-100%").animate({"left":"0"}); $(".banner a").eq($exdex).stop(true,true). css("left","0").animate({"left":"100%"}); } </script> </body> </html>
The above is the entire content of this article to help you achieve a simple and elegant image carousel effect.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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,

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

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

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:

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

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
