search
HomeWeb Front-endCSS TutorialHow to use pure CSS and JS to achieve image carousel effect

本篇文章给大家详细介绍一下使用纯CSS、JS实现图片轮播效果的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

How to use pure CSS and JS to achieve image carousel effect

<!DOCTYPE html><html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<!--轮播-->
		<style>
			.carousel {
				width: 648px;
				height: 400px;
				margin: 0 auto;
				text-align: center;
				position: absolute;
				left: 24%;
				/*border: 1px solid red;*/
			}
			
			.inner {
				/*border: 12px solid blue;*/
				width: 648px;
				height: 400px;
				position: absolute;
			}
			
			.inner-img {
				width: 200px;
				height: 500px;
				display: none;
				/*position: absolute;*/
				position: relative;
				/*top:0;*/
				/*left:0;*/
				/*z-index:1000;*/
			}
			
			.inner-img.active {
				display: block;
			}
			
			.leftBtn,
			.rightBtn {
				position: absolute;
				width: 40px;
				height: 60px;
				background: rgba(0, 0, 0, 0.3);
				/*近乎透明色*/
				font-size: 30px;
				color: #fff;
				text-align: center;
				line-height: 60px;
				cursor: pointer;
				display: none;
			}
			
			.leftBtn {
				left: 5px;
				top: 170px;
			}
			
			.rightBtn {
				/*right:5px;*/
				left: 603px;
				top: 170px;
			}
			
			.carousel ul {
				position: absolute;
				/*left:20px;*/
				padding-left: 228px;
				bottom: 10px;
				/*z-index: 999;*/
				list-style: none;
				width: 200px;
				height: 20px;
			}
			
			.carousel ul .page {
				float: left;
				width: 18px;
				height: 18px;
				line-height: 18px;
				border-radius: 18px;
				/*变成圆形*/
				background: black;
				margin-right: 6px;
				/*距离6个px单位*/
				color: #fff;
				font-size: 14px;
				/*text-align: center;*/
				cursor: pointer;
			}
			
			.carousel ul .page.active {
				background-color: red;
			}
			
			.carousel p {
				/*float: left;*/
				margin-top: -360px;
				margin-right: -600%;
				color: black;
				text-decoration: none;
				list-style: none;
			}
		</style>

	</head>

	<body>
		<!--轮播-->
		<p class="carousel">
			<p class="inner">
				<a href="#" class="inner-img active"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCxe0O.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>
					<a href="#" class="inner-img"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCxl9A.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>
					<a href="#" class="inner-img"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCxJnf.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>
					<a href="#" class="inner-img"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCxtHS.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>
					<a href="#" class="inner-img"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCxaNQ.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>
					<a href="#" class="inner-img"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCx6BT.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>
					<a href="#" class="inner-img"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCxW4J.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>
					<a href="#" class="inner-img"><img src="/static/imghwm/default1.png"  data-src="https://s2.ax1x.com/2019/11/06/MCx43R.jpg"  class="lazy"   alt="图片加载中"    style="max-width:90%"  style="max-width:90%"></a>

					<p class="leftBtn"><</p>
					<!--左按钮-->
					<p class="rightBtn">></p>
					<!--右按钮-->
			</p>
			<ul>
				<li class="page active">1</li>
				<li class="page">2</li>
				<li class="page">3</li>
				<li class="page">4</li>
				<li class="page">5</li>
				<li class="page">6</li>
				<li class="page">7</li>
				<li class="page">8</li>
			</ul>
		</p>

	</body>

	<script>
		var carousel = document.getElementsByClassName(&#39;carousel&#39;)[0], //获取carousel的class类
			// 获取所有包含图片的链接
			innerImg = document.getElementsByClassName(&#39;inner-img&#39;),
			// 获取左右按钮
			btnL = document.getElementsByClassName(&#39;leftBtn&#39;)[0],
			btnR = document.getElementsByClassName(&#39;rightBtn&#39;)[0],
			// 获取分页器
			page = document.getElementsByClassName(&#39;page&#39;),
			num = 0; // 声明变量 初始图片为第一张0
		// 声明时间控制函数
		var timer = setInterval(moveR, 2500); //调用moveR 时间间隔2.5s 
		// 图片向右轮播
		function moveR() {
			num++; // 变量每3000毫秒递增一次,图片向右轮播
			// 如果是最后一张图片的时候从0开始显示
			if(num == innerImg.length) {
				num = 0;
			}
			move();
		};
		// 图片向左轮播
		function moveL() {
			num--; // 每调用一次moveL(),变量递减一次
			// 如果是第一张图片,则从最后一张图片开始显示
			if(num == -1) {
				num = innerImg.length - 1;
			};
			move();
		}     // 图片切换
		  		function move() {     // 把所有的innerImg隐藏和page背景全部变成黑色
			for(var i = 0; i < innerImg.length; i++) {
				innerImg[i].style.display = &#39;none&#39;;
				page[i].style.background = &#39;black&#39;;
			}
			// 把当前num下标的innerImg显示和page背景变成red
			innerImg[num].style.display = &#39;block&#39;;
			page[num].style.background = &#39;red&#39;;  		}

		// 分页器鼠标滑过时图片切换
		for(var i = 0; i < page.length; i++) {
			// 用来保存下标,page[i].index == 0 / 1 / 2 / 3 /4...
			page[i].index = i;
			// console.log(page[i].index);
			// 因为已经保存好的下边,这里的page[i], page[0], page[1], page[2], page[3]
			page[i].onmouseover = function() {
				for(var j = 0; j < page.length; j++) {
					page[j].style.background = &#39;black&#39;;
					innerImg[j].style.display = &#39;none&#39;;
				}
				this.style.background = &#39;red&#39;;        // console.log(this.index);可以查看是多少
				innerImg[this.index].style.display = &#39;block&#39;;
				num = this.index;
			}
		}
		btnL.onclick = function() {
			moveL();
		}
		btnR.onclick = function() {
			moveR();
		}

		// 鼠标划上carousel时让左右按钮显示 并清除时间函数 
		carousel.onmouseover = function() {
			// 清除时间函数
			clearInterval(timer); //暂停 图片不继续循环
			btnR.style.display = &#39;block&#39;;
			btnL.style.display = &#39;block&#39;;
		}

		// 鼠标离开carousel时让左右按钮隐藏
		carousel.onmouseout = function() {
			// 开启时间函数
			timer = setInterval(moveR, 2500);
			btnR.style.display = &#39;none&#39;;
			btnL.style.display = &#39;none&#39;;
		}
	</script></html>

推荐学习:css视频教程

The above is the detailed content of How to use pure CSS and JS to achieve image carousel effect. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
Two Images and an API: Everything We Need for Recoloring ProductsTwo Images and an API: Everything We Need for Recoloring ProductsApr 15, 2025 am 11:27 AM

I recently found a solution to dynamically update the color of any product image. So with just one of a product, we can colorize it in different ways to show

Weekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsWeekly Platform News: Impact of Third-Party Code, Passive Mixed Content, Countries with the Slowest ConnectionsApr 15, 2025 am 11:19 AM

In this week's roundup, Lighthouse sheds light on third-party scripts, insecure resources will get blocked on secure sites, and many country connection speeds

Options for Hosting Your Own Non-JavaScript-Based AnalyticsOptions for Hosting Your Own Non-JavaScript-Based AnalyticsApr 15, 2025 am 11:09 AM

There are loads of analytics platforms to help you track visitor and usage data on your sites. Perhaps most notably Google Analytics, which is widely used

It's All In the Head: Managing the Document Head of a React Powered Site With React HelmetIt's All In the Head: Managing the Document Head of a React Powered Site With React HelmetApr 15, 2025 am 11:01 AM

The document head might not be the most glamorous part of a website, but what goes into it is arguably just as important to the success of your website as its

What is super() in JavaScript?What is super() in JavaScript?Apr 15, 2025 am 10:59 AM

What's happening when you see some JavaScript that calls super()?.In a child class, you use super() to call its parent’s constructor and super. to access its

Comparing the Different Types of Native JavaScript PopupsComparing the Different Types of Native JavaScript PopupsApr 15, 2025 am 10:48 AM

JavaScript has a variety of built-in popup APIs that display special UI for user interaction. Famously:

Why Are Accessible Websites so Hard to Build?Why Are Accessible Websites so Hard to Build?Apr 15, 2025 am 10:45 AM

I was chatting with some front-end folks the other day about why so many companies struggle at making accessible websites. Why are accessible websites so hard

The `hidden` Attribute is Visibly WeakThe `hidden` Attribute is Visibly WeakApr 15, 2025 am 10:43 AM

There is an HTML attribute that does exactly what you think it should do:

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools