亲宝软件园·资讯

展开

Javascript幻灯片 Javascript幻灯片播放功能实现过程解析

凌轹VF 人气:0

javascript实现幻灯片播放

实现原理

代码

html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引入css样式 -->
<link rel="stylesheet" href="style.css" rel="external nofollow" type="text/css">

</head>
<body>
	<!-- 先设置轮播的整体容器 -->
	<div class="slideshow-container">
		<!-- 在设置轮播图片、翻页键、数字文本的容器 -->
		<!-- fade为滑动动画特效 -->
		<div class="mySlides fade">
			<div class="numbertext">1 / 3</div>
			<img src="img/1.jpg" style="width: 100%">
			<div class="text">文本 1</div>
		</div>

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

		<div class="mySlides fade">
			<div class="numbertext">3 / 3</div>
			<img src="img/3.jpg" style="width: 100%">
			<div class="text">文本 3</div>
		</div>
		
		<a class="prev" onclick="plusSlides(-1)">❮</a> 
		<a class="next" onclick="plusSlides(1)">❯</a>
	</div>
	<br>
	<!-- 设置下方按钮 -->
	<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>
  <!-- 引入js文件,注意js文件应该在下方,否则将无法出现错误,个人觉得错误原因应该是元素未加载而去执行js文件导致错误,具体原因还需探究-->
	<script src="show.js" type="text/javascript"></script>
</body>

</html>

css

@charset "UTF-8";
* {box-sizing:border-box}
body {font-family: Verdana,sans-serif;}
.mySlides {
	display:none;
  /*display 为none的含义是不展示元素,但是且不占用空间*/
}
/* 幻灯片容器 */
.slideshow-container {
 max-width: 1000px;
 position: relative;
 margin: auto;
}

/* 下一张 & 上一张 按钮 */
.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;
}

/* 定位 "下一张" 按钮靠右 */
.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);
}

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

/* 数字文本 (1/3 等) */
.numbertext {
 color: #f2f2f2;
 font-size: 12px;
 padding: 8px 12px;
 position: absolute;
 top: 0;
}

/* 标记符号 */
.dot {
 cursor:pointer;
 height: 13px;
 width: 13px;
 margin: 0 2px;
 background-color: #bbb;
 border-radius: 50%;
 display: inline-block;
 transition: background-color 0.6s ease;
}

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

/* 淡出动画 */
.fade {
 -webkit-animation-name: fade;
 -webkit-animation-duration: 1.5s;
 animation-name: fade;
 animation-duration: 1.5s;
}

@-webkit-keyframes fade {
 from {opacity: .4} 
 to {opacity: 1}
}

@keyframes fade {
 from {opacity: .4} 
 to {opacity: 1}
}

js

var slideIndex = 1;

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} 
 if (n < 1) {slideIndex = slides.length}
 for (i = 0; i < slides.length; i++) {
   slides[i].style.display = "none"; 
 }//class为mySlides下的元素,即不展示项目的图片元素、数字元素和文本元素
 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";//轮播图片对应下标样式激活
}
showSlides(slideIndex);

加载全部内容

相关教程
猜你喜欢
用户评论