classic: slide-version 2.0,增加了新代码,代码进行部分重构,增加了新效果.example:
section :slideshow
/*slideShow幻灯片*/
.mySlides {display:none;}
/* 幻灯片容器 */
.slideshow-container {
max-width: 100%;
position: relative;
margin: auto;
}
/*css箭头*/
.arrowsright{
display: inline-block;
width:18px;
height:18px;
border-top: 6px solid gray;
border-right: 6px solid gray;
transform: rotate(45deg);
background:none;
}
.arrowsleft{
display: inline-block;
width:18px;
height:18px;
border-bottom: 6px solid gray;
border-left: 6px solid gray;
transform: rotate(45deg);
background:none;
}
/* 下一张 & 上一张 按钮 */
.prev, .next {
cursor: pointer;
position: absolute;
top: 40%;
width: 20px;
height:20px;
padding: 20px;
font-size: 18px;
transition:3s ease;/*此过渡属性结合rgba()函数使用背景色过渡变换效果*/
border-radius: 50%;
margin:15px;
}
/* 定位 "下一张" 按钮靠右 */
.next {
right: 0px;
}
/* 当鼠标悬停时修改背景色*/
.prev:hover, .next:hover {
background-color: rgba(0,0,555,0.5);
}
/* 标题文本 */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 28px;
width: 100%;
text-align: center;
}
/* 数字文本 (1/3 等) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* 长条形控制器 */
.square {
cursor:pointer;
height: 3px;
width: 80px;
margin: 0 2px;
display: inline-block;
/*background-image线性渐变效果*/
background-image:linear-gradient(to right, #6CC, #699);
}
/* 长条形控制器背景变换 */
.active, .square:hover {
background-color: #717171;
}
/*当前幻灯片容器current*/
.currentPage{
position:absolute;
bottom:12px;
left:42%;
}
/* 图片淡出动画 */
.fade {
-webkit-animation-name: fade;
-webkit-animation-duration: 1.5s;
animation-name: fade;
animation-duration: 1.5s;
}
/*图片透明度渐变函数*/
@-webkit-keyframes fade {
from {opacity: 0.5}
to {opacity: 1}
}
@keyframes fade {
from {opacity: 0.5}
to {opacity: 1}
}
<script>
/*slideShow JS*/
var slideIndex = 0;
showSlides(slideIndex);
function plusSlides(n) {//传递两个参数-1或者+1并对默认slideIndex = 1;索引值的重新计算赋值
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");//获取幻灯片图片对象
var square = document.getElementsByClassName("square");//获取最下边的长条.square控制器对象
if (n > slides.length) {slideIndex = 1} //判断传入的索引值并对slideIndex索引参数重新赋值
if (n <= 0) {slideIndex = slides.length}//判断传入的索引值并对默认的slideIndex变量重新赋值
for (i = 0; i < slides.length; i++) {//for循环遍历幻灯片对象的display属性值,设为“none”值,使图层不可见
slides[i].style.display = "none";
}
for (i = 0; i < square.length; i++) {//for循环遍历幻灯片控制器的class属性值,通过className属性修改class
square[i].className = square[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block"; //通过数组索引遍历幻灯片.mySlides所有对象
square[slideIndex-1].className += " active"; //通过数组索引遍历幻灯片控制器.dot所有对像
}
/**%js 幻灯片设计的核心事项是通过把幻灯片放入到数组中,并对数组的index进行操作计算进行换灯片的切换%**/
</script>
<div class="slideshow-container">
<div class="mySlides fade" style="display:block;">
<img src="image/Enb2.jpg" style="width:100%" height="600px">
</div>
<div class="mySlides fade">
<img src="image/ban04.jpg" style="width:100%" height="600px">
</div>
<div class="mySlides fade">
<img src="image/ban3.jpg" style="width:100%" height="600px">
</div>
<a class="prev" οnclick="plusSlides(-1)"><span class="arrowsleft"></span></a>
<a class="next" οnclick="plusSlides(1)"><span class="arrowsright"></span></a>
<!--幻灯片控制器 control-->
<div class="currentPage" style="text-align:center">
<span class="square" οnclick="currentSlide(1)"></span>
<span class="square" οnclick="currentSlide(2)"></span>
<span class="square" οnclick="currentSlide(3)"></span>
</div><!--/..end control-->
</div>