Home Web Front-end CSS Tutorial How to use pure CSS and JS to achieve image carousel effect

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

Apr 23, 2021 am 09:06 AM
css js

本篇文章给大家详细介绍一下使用纯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="https://s2.ax1x.com/2019/11/06/MCxe0O.jpg" alt="图片加载中" width="648" height="400"></a>
					<a href="#" class="inner-img"><img src="https://s2.ax1x.com/2019/11/06/MCxl9A.jpg" alt="图片加载中" width="648" height="400"></a>
					<a href="#" class="inner-img"><img src="https://s2.ax1x.com/2019/11/06/MCxJnf.jpg" alt="图片加载中" width="648" height="400"></a>
					<a href="#" class="inner-img"><img src="https://s2.ax1x.com/2019/11/06/MCxtHS.jpg" alt="图片加载中" width="648" height="400"></a>
					<a href="#" class="inner-img"><img src="https://s2.ax1x.com/2019/11/06/MCxaNQ.jpg" alt="图片加载中" width="648" height="400"></a>
					<a href="#" class="inner-img"><img src="https://s2.ax1x.com/2019/11/06/MCx6BT.jpg" alt="图片加载中" width="648" height="400"></a>
					<a href="#" class="inner-img"><img src="https://s2.ax1x.com/2019/11/06/MCxW4J.jpg" alt="图片加载中" width="648" height="400"></a>
					<a href="#" class="inner-img"><img src="https://s2.ax1x.com/2019/11/06/MCx43R.jpg" alt="图片加载中" width="648" height="400"></a>

					<p class="leftBtn">&lt;</p>
					<!--左按钮-->
					<p class="rightBtn">&gt;</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>
Copy after login

推荐学习: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 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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles