search
HomeWeb Front-endJS TutorialHow to achieve seamless carousel effect (automatic carousel) in jq? (code example)

本篇文章给大家介绍jq实现无缝轮播图效果的方法,可以自动轮播,也可以手动点击切换轮播。有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

我们在很多的网站上都可以看到轮播图,例如在淘宝、京东这些网站都有轮播图的存在;轮播图的使用范围非常广,banner和animation很容易抓住用户的眼球,所以做好这个也就是一个网页一个app的关键之一。

轮播图有多种实现方式,可以用css实现、用jQuery实现、甚至是用其他框架实现,下面我们就以jQuery方法举例,用jQuery代码做一个自动+手动轮播图片的轮播图效果。

jq实现无缝轮播图效果(自动轮播)的代码示例:

html代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>jQuery实现无缝轮播图</title>
		<link href="css/style.css" rel="stylesheet" type="text/css"/>
	</head>
	<body>
		<div class="banner">
			<ul class="img">
				<li>
					<a href="#"><img  src="/static/imghwm/default1.png"  data-src="img/2.png"  class="lazy"   alt="How to achieve seamless carousel effect (automatic carousel) in jq? (code example)" ></a>
				</li>
				<li>
					<a href="#"><img  src="/static/imghwm/default1.png"  data-src="img/2.png"  class="lazy"   alt="How to achieve seamless carousel effect (automatic carousel) in jq? (code example)" ></a>
				</li>
				<li>
					<a href="#"><img  src="/static/imghwm/default1.png"  data-src="img/2.png"  class="lazy"   alt="How to achieve seamless carousel effect (automatic carousel) in jq? (code example)" ></a>
				</li>
				<li>
					<a href="#"><img  src="/static/imghwm/default1.png"  data-src="img/2.png"  class="lazy"   alt="How to achieve seamless carousel effect (automatic carousel) in jq? (code example)" ></a>
				</li>
			</ul>

			<ul class="num">
			</ul>
			<div class="btn btn_l"><</div>
			<div class="btn btn_r">></div>
		</div>

	</body>
</html>

上面是html代码,代码的结构比较简单,这里解释一下,ul.img标签是要左右移动的,ul.img标签里就是轮播图的主体内容了;然后ul.num标签是提供索引和下标控制符,绑定动画的,会通过jq代码来控制数量与图片一致。btn_l和tn_r类名的div标签是提供左右按钮,这样轮播图就可以点击实现手动的左右滑动。

css代码:

* {
	padding: 0;
	margin: 0;
	list-style: none;
}

.banner {
	margin: 100px auto;
	border: 5px solid #000;
	width: 1000px;
	height: 640px;
	position: relative;
	overflow: hidden;
}

.banner .img {
	width: 5000px;
	position: absolute;
	left: 0px;
	top: 0px;
}

.banner .img li {
	float: left;
}

.banner .img li img {
	width: 1000px;
}


/*小圆点的样式*/

.banner .num {
	position: absolute;
	right: 40px;
	bottom: 30px;
}

.banner .num li {
	width: 20px;
	height: 20px;
	float: left;
	background: #333;
	margin-left: 20px;
	border-radius: 50px;
	border: 2px solid white;
}

.banner .num li.on {
	border: 2px solid #333;
	background: white
}


/*两边耳朵的样式*/

.banner .btn {
	position: absolute;
	width: 80px;
	height: 80px;
	background: rgba(0, 0, 0, 0.7);
	font-size: 60px;
	color: white;
	text-align: center;
	line-height: 80px;
	top: 50%;
	margin-top: -80px;
	cursor: pointer;
	display: none;
}

.banner:hover .btn {
	display: block;
}

.banner .btn_l {
	left: 10px;
	border-radius: 50%
}

.banner .btn_r {
	right: 10px;
	border-radius: 50%
}

我们链接的外部css文件,然后请注意一下各种属性和值。

jquery代码:

首先要引用jquery.js文件

<script type="text/javascript" src="s/jquery-1.7.2.min.js"></script>

注:想要使用jquery来实现各种效果,就必须要先引用jquery.js文件,在使用jquery代码来实现各种效果。

在使用jquery语句来实现轮播效果:

<script>
		$(function() {
			var i = 0;
			var clone = $(".banner .img li").first().clone();
			$(".banner .img").append(clone);
			var size = $(".banner .img li").size();
			for(var j = 0; j < size - 1; j++) {
				$(".banner .num").append("<li></li>");
			}
			$(".banner .num li").first().addClass(&#39;on&#39;);
			//鼠标划入圆点
			$(".banner .num li").hover(function() {
				var index = $(this).index();
				i = index;
				$(".banner .img").stop().animate({
					left: -index * 1000
				}, 500);
				$(this).addClass(&#39;on&#39;).siblings().removeClass(&#39;on&#39;);
			})

			/*轮播图自动轮播*/
			var t = setInterval(function() {
				i++;
				move();
			}, 2000);

			//对banner定时器的操作
			$(".banner").hover(function() {
				clearInterval(t);
			}, function() {
				t = setInterval(move, 2000);
			})

			/*向左按钮*/
			$(".banner .btn_l").click(function() {
				i--;
				move();
			})
			/*向右按钮*/
			$(".banner .btn_r").click(function() {
				i++;
				move();
			})
                        
                        /*封装函数*/
			function move() {
				if(i == size) {
					$(".banner .img").css({
						left: 0
					});
					i = 1;
				}
				if(i == -1) {
					$(".banner .img").css({
						left: -(size - 1) * 1000
					});
					i = size - 2;
				}

				$(".banner .img").stop().animate({
					left: -i * 1000
				}, 500);

				if(i == size - 1) {
					$(".banner .num li").eq(0).addClass(&#39;on&#39;).siblings().removeClass(&#39;on&#39;);
				} else {
					$(".banner .num li").eq(i).addClass(&#39;on&#39;).siblings().removeClass(&#39;on&#39;);
				}
			}
		})
	</script>

我们来看看效果(静态):

How to achieve seamless carousel effect (automatic carousel) in jq? (code example)

总结:以上就是jQuery实现无缝轮播图效果的全部代码,大家可以自己动手尝试编译,看看效果,加深理解,制作属于自己的轮播图。希望能对大家的学习有所帮助,更多相关教程请访问JavaScript视频教程jQuery视频教程bootstrap教程

The above is the detailed content of How to achieve seamless carousel effect (automatic carousel) in jq? (code example). 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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

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.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

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

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

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.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

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.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

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.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

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

See all articles

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment