Javascript幻燈片播放功能實(shí)現(xiàn)過程解析
更新時(shí)間:2020年05月07日 10:51:48 作者:凌轢VF
這篇文章主要介紹了Javascript幻燈片播放功能實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
javascript實(shí)現(xiàn)幻燈片播放
實(shí)現(xiàn)原理
- step1 設(shè)置容器,包含圖片、翻頁、下標(biāo)等元素,通過相對(duì)定位來布局。
- step2 將幻燈片變化,需要改變的元素放在一個(gè)字容器內(nèi),display設(shè)為none,并且采取動(dòng)畫來變化。
- step3設(shè)置js函數(shù),將應(yīng)該播放的元素樣式激活,其他的元素仍為未激活狀態(tài)或者不展示類的隱藏。
代碼
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> <!-- 先設(shè)置輪播的整體容器 --> <div class="slideshow-container"> <!-- 在設(shè)置輪播圖片、翻頁鍵、數(shù)字文本的容器 --> <!-- fade為滑動(dòng)動(dòng)畫特效 --> <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> <!-- 設(shè)置下方按鈕 --> <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文件應(yīng)該在下方,否則將無法出現(xiàn)錯(cuò)誤,個(gè)人覺得錯(cuò)誤原因應(yīng)該是元素未加載而去執(zhí)行js文件導(dǎo)致錯(cuò)誤,具體原因還需探究--> <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);
}
/* 標(biāo)題文本 */
.text {
color: #f2f2f2;
font-size: 15px;
padding: 8px 12px;
position: absolute;
bottom: 8px;
width: 100%;
text-align: center;
}
/* 數(shù)字文本 (1/3 等) */
.numbertext {
color: #f2f2f2;
font-size: 12px;
padding: 8px 12px;
position: absolute;
top: 0;
}
/* 標(biāo)記符號(hào) */
.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;
}
/* 淡出動(dòng)畫 */
.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下的元素,即不展示項(xiàng)目的圖片元素、數(shù)字元素和文本元素
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");//將激活的下標(biāo)元素,改為不再展示
}
slides[slideIndex-1].style.display = "block";//輪播的元素展示
dots[slideIndex-1].className += " active";//輪播圖片對(duì)應(yīng)下標(biāo)樣式激活
}
showSlides(slideIndex);
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
情人節(jié)單身的我是如何在敲完代碼之后收到12束玫瑰的(javascript)
這篇文章主要介紹了情人節(jié)單身的我是如何在敲完代碼之后收到12束玫瑰的,感興趣的朋友一起來學(xué)習(xí)下2015-08-08
JavaScript深度復(fù)制(deep clone)的實(shí)現(xiàn)方法
本文給大家介紹JavaScript深度復(fù)制(deep clone)的實(shí)現(xiàn)方法,涉及到j(luò)s深度復(fù)制相關(guān)知識(shí),本文介紹的非常詳細(xì),特此分享腳本之家平臺(tái)供大家參考2016-02-02
微信小程序使用wx.navigateTo路由跳轉(zhuǎn)層級(jí)限制問題小結(jié)
在微信小程序開發(fā)中,wx.navigateTo和wx.redirectTo是兩種頁面跳轉(zhuǎn)方式,wx.navigateTo允許跳轉(zhuǎn)到新頁面并保留當(dāng)前頁面,適合需要返回的場景,但受頁面棧10層限制,wx.redirectTo則關(guān)閉當(dāng)前頁面后跳轉(zhuǎn),本文介紹微信小程序使用wx.navigateTo路由跳轉(zhuǎn)層級(jí)限制問題2024-10-10
利用threejs實(shí)現(xiàn)一個(gè)簡易的泊車功能
這篇文章主要為大家詳細(xì)介紹了如何利用threejs實(shí)現(xiàn)一個(gè)簡易的泊車功能,文中的示例代碼講解詳細(xì),對(duì)大家的學(xué)習(xí)和工作有一定的幫助,感興趣的小伙伴可以動(dòng)手嘗試一下2024-01-01

