HTML+CSS实现简单的点击轮播图

在这里插入<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			* {
				padding: 0px;
				margin: 0px;
			}

			.nav {
				width: 100px;
				height: 100px;
				margin: 100px auto 0px;
			}

			ul {
				width: 200px;
				height: 100px;
				list-style-type: none;
			}

			ul li {
				width: 100px;
				height: 100px;
				float: left;
				//为了将每张li里的图片放到第一张的位置
				position: absolute;

			}

			ul li img {
				width: 100px;
				height: 100px;
			}

			#btn {}
		</style>
		
	</head>
	<body>
		<div class="nav">
			<ul>
				<li><img src="cupon4.png" /></li>
				<li><img src="cupon5.png" /></li>
			</ul>
		</div>
		<div align="center">
			<button id="btn" οnclick="next()">下一张</button>
		</div>
		
		<script>
			var flag = 0;
			var lis = document.getElementsByTagName("li");
			lis[0].style.opacity = "1";
			lis[1].style.opacity = "0";

			function next
			() {
				/* window.alert("测试代码块!!"); */
				/* 1.通过document对象 获取标签对象 */

				//lis 一组li {第1张,第2张}   默认
				//             0      1
				if (flag === 0) {
					var n = 0;
					while (n < lis.length) {
						if (n === 1) {
							lis[n].style.opacity = "1";
						} else {
							lis[n].style.opacity = "0";
						}

						n++;
					}
					flag = 1;
				} else {
					var n = 0;
					while (n < lis.length) {
						if (n === 0) {
							lis[n].style.opacity = "1";
						} else {
							lis[n].style.opacity = "0";
						}
						n++;
					}
					flag = 0;
				}




			}
		</script>
	</body>
</html>
代码片
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单HTML CSS JS轮播图示例: HTML代码: ``` <div class="slideshow-container"> <div class="slide"> <img src="image1.jpg"> </div> <div class="slide"> <img src="image2.jpg"> </div> <div class="slide"> <img src="image3.jpg"> </div> </div> <button class="prev">❮</button> <button class="next">❯</button> ``` CSS代码: ``` .slideshow-container { position: relative; width: 100%; height: 500px; overflow: hidden; } .slide { position: absolute; width: 100%; height: 100%; opacity: 0; transition: opacity 1s ease-in-out; } .slide.active { opacity: 1; } .prev, .next { position: absolute; top: 50%; transform: translateY(-50%); padding: 16px; font-size: 24px; font-weight: bold; background-color: #fff; border: none; cursor: pointer; } .prev { left: 0; } .next { right: 0; } ``` JS代码: ``` var slides = document.querySelectorAll('.slide'); var prevButton = document.querySelector('.prev'); var nextButton = document.querySelector('.next'); var slideIndex = 0; function showSlide(n) { if (n > slides.length - 1) { slideIndex = 0; } else if (n < 0) { slideIndex = slides.length - 1; } else { slideIndex = n; } for (var i = 0; i < slides.length; i++) { slides[i].classList.remove('active'); } slides[slideIndex].classList.add('active'); } function prevSlide() { showSlide(slideIndex - 1); } function nextSlide() { showSlide(slideIndex + 1); } prevButton.addEventListener('click', prevSlide); nextButton.addEventListener('click', nextSlide); showSlide(slideIndex); setInterval(nextSlide, 5000); ``` 解释说明: - HTML中,轮播图的图片通过`<img>`标签放置在`<div class="slide">`中; - CSS中,轮播图容器`.slideshow-container`使用`position: relative`以及`overflow: hidden`来隐藏溢出的图片,`.slide`使用`position: absolute`和`opacity`来实现淡入淡出效果,`.prev`和`.next`使用`position: absolute`、`top`、`left`或`right`来放置按钮; - JS中,使用`querySelectorAll`获取所有`.slide`元素,`querySelector`获取`.prev`和`.next`按钮,定义`slideIndex`变量表示当前图片的索引,`showSlide`函数根据传入的索引值显示对应的图片,`prevSlide`和`nextSlide`函数分别减少和增加`slideIndex`的值,最后,通过`addEventListener`为`prevButton`和`nextButton`添加点击事件,然后调用`showSlide`函数,实现轮播效果;最后,使用`setInterval`每隔5秒调用`nextSlide`函数,实现自动轮播。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值