使用javascript實現圖片上下轉場效果並且實現順序迴圈播放

來源:互聯網
上載者:User

標籤:一個   cal   分享   根據   javascrip   顯示   上下   閱讀   tco   

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>圖片切換</title>
<style>
.content{
height: 500px;
width: 500px;
text-align: center;
margin:0 auto;
}
.playBtn button,.showPages span,.upDownBtn button{
margin: 20px;
}
.img{
position: relative;
border:1px solid #430d06;
height: 350px;
width: 500px;
}
.img img{
width: 100%;
height: 100%;
}
.img p{
z-index: 1;
margin: 0;
position: absolute;
bottom:0;
height: 40px;
width: 100%;
font-size: 23px;
line-height: 40px;
color: #fff;
background-color: #000;
opacity: 0.2;
}

</style>
</head>
<body>
<div class="content">
<div class="playBtn">
<button id="order">順序播放</button>
<button id="loop">迴圈播放</button>
</div>
<div class="img">
<img src="img/3.jpg" id="oImg">
<p id="imgP"></p>
</div>
<div class="showPages">
<span id="showPages"></span>
</div>
<div class="upDownBtn">
<button id="up">上一張</button>
<button id="down">下一張</button>
</div>
</div>
<script>
//首先一組圖片放在數組裡
var imgAry=[‘img/1.jpg‘,‘img/2.jpg‘,‘img/3.jpg‘,‘img/4.jpg‘,‘img/5.jpg‘,‘img/6.jpg‘];

//用原生js擷取元素(當然這些都是基礎,看你自己喜歡用原生還是JQ了)
var up=document.getElementById("up");
var down=document.getElementById("down");
var oImg=document.getElementById("oImg");
var showPages=document.getElementById("showPages");
var imgP=document.getElementById("imgP");
var order=document.getElementById("order");
var loop=document.getElementById("loop");

//設定順序還是迴圈播放的開關,這裡預設是順序播放(true),那迴圈播放就是false了
var onOff=true;
//點擊進入順序播放
order.onclick=function () {
onOff=true;
};
//點擊進入迴圈播放
loop.onclick=function () {
onOff=false
}

//設定一個初始值作用相當於前邊那個imgAry數組的index值一樣
var n=0;
//點擊跳轉前一張圖片
up.onclick=function () {
//找對應的索引值,向上所以就是索引-1,n--跟n-1一樣的
n--;

//第一張臨界點判斷處理
if(n<0){
// 判斷是順序還是迴圈播放
if(onOff){
//這裡邊走的是順序播放,順序的時候在臨界點時讓其索引等於臨界點的值就行了,順便給個提示
n=0;
alert("已經是第一張了")
}else {
//這裡是迴圈播放,將臨界點的索引設定為最後一站的索引即可,即 倒敘
n=imgAry.length-1;
}
}
//再將公用組件執行賦值渲染即可
commontComponent();
};

//向下和向上的邏輯是一樣的,就顛倒一下就好,這裡就不再詳細解釋了
down.onclick=function () {
n++;
if(n>=imgAry.length){
if(onOff){
n=imgAry.length-1;
alert("已經是最後一張了")
}else {
n=0;
}
}
commontComponent();
};

//渲染的頁面公用組件
function commontComponent() {
//這裡是根據索引尋找對應的圖片並賦值
oImg.src=imgAry[n];

//這是圖片上的提示文字
/*強調一下為什麼是n+1:因為n是從0開始的,直接顯示0不符合人們閱讀時的正常邏輯,所以+1好一點*/
imgP.innerHTML=‘這是第‘+(n+1)+‘張圖片‘;

//這裡是圖片下邊分頁的顯示,n+1同理
showPages.innerHTML=n+1+‘/‘+imgAry.length;
}
commontComponent();
</script>
</body>
</html>

下邊這個是一個靜態顯示,具體功能可以自己實驗

 


使用javascript實現圖片上下轉場效果並且實現順序迴圈播放

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.