博主信息
博文 56
粉丝 3
评论 1
访问量 60910
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
js轮播图——2018年4月12日
沈斌的博客
原创
845人浏览过

参考W3C实现

实例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>slideshow</title>
	<style type="text/css">
		* {box-sizing:border-box}

		/* Slideshow container */
		.slideshow-container {
		  max-width: 1000px;
		  position: relative;
		  margin: auto;
		}

		/* Hide the images by default */
		.mySlides {
		    display: none;
		}

		/* Next & previous buttons */
		.prev, .next {
		  cursor: pointer;
		  position: absolute;
		  top: 50%;
		  width: auto;
		  margin-top: -22px;
		  padding: 16px;
		  color: white;
		  font-weight: bold;
		  font-size: 18px;
		  transition: 0.6s ease;
		  border-radius: 0 3px 3px 0;
		}

		/* Position the "next button" to the right */
		.next {
		  right: 0;
		  border-radius: 3px 0 0 3px;
		}

		/* On hover, add a black background color with a little bit see-through */
		.prev:hover, .next:hover {
		  background-color: rgba(0,0,0,0.8);
		}

		/* Caption text */
		.text {
		  color: #f2f2f2;
		  font-size: 15px;
		  padding: 8px 12px;
		  position: absolute;
		  bottom: 8px;
		  width: 100%;
		  text-align: center;
		}

		/* Number text (1/3 etc) */
		.numbertext {
		  color: #f2f2f2;
		  font-size: 12px;
		  padding: 8px 12px;
		  position: absolute;
		  top: 0;
		}

		/* The dots/bullets/indicators */
		.dot {
		  cursor: pointer;
		  height: 15px;
		  width: 15px;
		  margin: 0 2px;
		  background-color: #bbb;
		  border-radius: 50%;
		  display: inline-block;
		  transition: background-color 0.6s ease;
		}

		.active, .dot:hover {
		  background-color: #717171;
		}

	</style>
</head>
<body>
	<div class="slideshow-container">

		<!-- Full-width images with number and caption text -->
		<div class="mySlides fade">
			<div class="numbertext">1 / 3</div>
			<img src="img/1.jpg" style="width:100%">
			<div class="text">Caption One</div>
		</div>

		<div class="mySlides fade">
			<div class="numbertext">2 / 3</div>
			<img src="img/2.jpg" style="width:100%">
			<div class="text">Caption Two</div>
		</div>

		<div class="mySlides fade">
			<div class="numbertext">3 / 3</div>
			<img src="img/3.jpg" style="width:100%">
			<div class="text">Caption Three</div>
		</div>

		<!-- Next and previous buttons -->
		<a class="prev" onclick="plusSlides(-1)">❮</a>
		<a class="next" onclick="plusSlides(1)">❯</a>
	</div>
	<br>

	<!-- The dots/circles -->
	<div style="text-align:center">
		<span class="dot" onclick="currentSlide(1)"></span> 
		<span class="dot" onclick="currentSlide(2)"></span> 
		<span class="dot" onclick="currentSlide(3)"></span> 
	</div>	

	<script type="text/javascript">
		var slideIndex=1;
		showSlides(slideIndex);

		function plusSlides(n){
			showSlides(slideIndex+=n);
		}

		function currentSlide(n){
			showSlides(slideIndex=n);
		}

		function showSlides(n){
			var i;
			var slides=document.getElementsByClassName("mySlides");
			var dots=document.getElementsByClassName("dot");
			if(n>slides.length) {
				slideIndex=1;
				console.log(slideIndex);
			}

			if(n<1) {
				slideIndex=slideIndex.length;
			}

			for (i=0;i<slides.length;i++){
				slides[i].style.display="none";
			}

			for (i=0;i<dots.length;i++){
				dots[i].className=dots[i].className.replace(" active","")
			}
			slides[slideIndex-1].style.display="block";
			dots[slideIndex-1].className +=" active";
		}
	</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

slide.png

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学