This article mainly introduces the simple image carousel effect implemented by jQuery, and analyzes the related implementation techniques of jQuery combining time function and random number operation to dynamically transform page elements in the form of a complete example, with features such as fixed direction sliding, fade-in and fade-out, etc. Switching effect, the code is very simple and practical, friends in need can refer to
This article describes the simple image carousel effect implemented by jQuery. Share it with everyone for your reference, the details are as follows:
Let’s take a look at the running effect first:
The specific code is as follows :
<!DOCTYPE html> <html> <head> <title>jquery实现图片轮播效果</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style> #lunbo{width: 600px;height: 300px;margin: 0 auto;overflow: hidden;} #pics{width: 600px;height: 300px;cursor: pointer;position: relative;} ul li{width: 600px;height: 300px;list-style: none;position: absolute;top: 0;left: 0;display: none;} img{width: 600px;height: 300px;} </style> </head> <body> <p id="lunbo"> <ul id="pics"> <li style="display: inline;"><img src="/static/imghwm/default1.png" data-src="http://pic.qiantucdn.com/58pic/14/60/54/32n58PICxE6_1024.jpg" class="lazy" alt="How to implement image carousel with jQuery" ></li> <li><img src="/static/imghwm/default1.png" data-src="http://pic.qiantucdn.com/58pic/19/09/94/5678b76f75315_1024.jpg" class="lazy" alt="How to implement image carousel with jQuery" ></li> <li><img src="/static/imghwm/default1.png" data-src="http://pic.qiantucdn.com/58pic/19/39/22/01D58PICFx7_1024.jpg" class="lazy" alt="How to implement image carousel with jQuery" ></li> <li><img src="/static/imghwm/default1.png" data-src="http://pic.qiantucdn.com/58pic/19/11/08/5679490f4892d_1024.jpg" class="lazy" alt="How to implement image carousel with jQuery" ></li> <li><img src="/static/imghwm/default1.png" data-src="http://pic.qiantucdn.com/58pic/18/44/26/5620690acb188_1024.jpg" class="lazy" alt="How to implement image carousel with jQuery" ></li> </ul> </p> <script type="text/javascript"> $(document).ready(function(){ var oLi = $("li"); var liWidth = oLi.eq(0).width(); var liHeight = oLi.eq(0).height(); //每隔3秒进行轮播 var timer = setInterval(change,3000); //鼠标放置在图片上时停止轮播,移开时继续轮播 $("p").mouseover ( function(){ clearInterval(timer); }) $("p").mouseout (function(){ timer = setInterval(change,3000); }) //轮播函数 var prevIndex = 0,nextIndex = 1; function change(){ if (prevIndex == oLi.length-1 ) {//若当前图片是最后一张图片,下一张出现首张图片 nextIndex = 0; } var n = Math.floor(Math.random()*3); if (n == 0) { fade(prevIndex,nextIndex); } else if (n== 2) { slide(prevIndex,nextIndex); } else{ grap(prevIndex,nextIndex); } prevIndex = nextIndex; nextIndex ++; } //淡入淡出效果, function fade(prevIndex,nextIndex){ //传入当前显示图片以及下一张图片的索引 oLi.eq(prevIndex).fadeOut(1000); oLi.eq(nextIndex).fadeIn(1000); } //向左右滑动效果 function slide(prevIndex,nextIndex){ var rand = Math.floor(Math.random()*2); oLi.eq(nextIndex).show(); oLi.eq(nextIndex).css("z-index","-1"); if (rand) { //向左滑动 oLi.eq(prevIndex).animate({left: -liWidth},1000,fun); } else{ oLi.eq(prevIndex).animate({left: liWidth},1000,fun); } function fun(){ oLi.eq(prevIndex).css({"left":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); } } //收缩效果 function grap(prevIndex,nextIndex){ var rand = Math.floor(Math.random()*4); oLi.eq(nextIndex).show(); oLi.eq(nextIndex).css("z-index","-1"); switch (rand){ case 0://向左上角滑动 oLi.eq(prevIndex).animate({left: -liWidth,top: -liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":"0","top": "0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 1://向右上角滑动 oLi.eq(prevIndex).animate({left: liWidth,top: -liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 2://向右下角滑动 oLi.eq(prevIndex).animate({left: liWidth,top: liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 3://向左下角滑动 oLi.eq(prevIndex).animate({left: -liWidth,top: liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; default:break; } } }); </script> </body> </html>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
Nuxt.js Vue server-side rendering exploration
What are the differences between let and var defined variables in js?
veloticy-ui realizes text animation effect
The above is the detailed content of How to implement image carousel with jQuery. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

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