JavaScript學習---自己動手寫的圖片輪播

來源:互聯網
上載者:User

標籤:擷取   obj   cti   mouse   tle   add   映像   href   http   

效果如下:

 

圖片定時輪播

點擊左右控制顯示下一張或上一張圖片

 

index.html檔案

<html><head><title>js編寫實現投影片效果</title><meta content="text/html; charset=utf-8" /> <link rel="stylesheet" href="css/style.css" type="text/css" /></head><body>js編寫實現投影片效果<div class="content"><div id="cimg1" > <img src="images/1.jpg" id="imgs"/> <div id="cimg4"><img src="images/left.png"  /></div> <div id="cimg5"><img src="images/right.png"  /></div> <div id="text"> <p id="textfont"> </p> </div>  </div></div></body><script src=‘js/index.js‘></script></html>

 css樣式代碼:(css/style.css)

.content{width:450px;height:300px;margin: 120px auto;}#cimg1 img{width:450px;height:300px;}#cimg4{  width:30px;  height:25px;  position:relative;left:0px;top:-150px;  }#cimg5{  width:30px;  height:25px;  position:relative;left:420px;top:-175px;}#cimg4 img{width:20px;height:25px;}#cimg5 img{width:20px;height:25px;}#text{height:50px;width:100%;background:#222222;position:relative;left:0px;top:-100px;}#text p{color:#fff;text-align:center;}

 JavaScript代碼(js/idnex.js)

//擷取放置圖片div的對象cimg1obj=document.getElementById("cimg1");imgsobj=document.getElementById("imgs");textobj=document.getElementById("textfont");//左右翻轉的圖片cimg4obj=document.getElementById("cimg4");cimg5obj=document.getElementById("cimg5");cimg4obj.onmouseover=function () {cimg4obj.style.background="#b3b3b3";cimg4obj.style.borderRadius="20px"}  cimg4obj.onmouseout=function () {cimg4obj.style.background="";}  cimg5obj.onmouseover=function () {cimg5obj.style.background="#b3b3b3";cimg5obj.style.borderRadius="20px"}cimg5obj.onmouseout=function () {cimg5obj.style.background="";}//向左的表徵圖被點擊時//變數i用於記錄當前的圖片//可以控製圖片輪播的數量當前i的最大值只能為3,最小為0i=1;imgsobj.src="images/"+i+".jpg";//預設投影片出來的圖片是第一張textadd();//向左的表徵圖被點擊時cimg4obj.onclick=function () {i--;if(i==0){i=3;}imgsobj.src="images/"+i+".jpg";//重寫圖片路徑textadd();}//向右的表徵圖被點擊時cimg5obj.onclick=function () {i++;if(i==4){i=1;}imgsobj.src="images/"+i+".jpg";//重寫圖片路徑textadd();}//建立定時器實現自動圖片輪播每隔5秒換一張圖setInterval("jishi()",5000);//1000為1秒鐘       function jishi()       {           i++;if(i==4){i=1;}imgsobj.src="images/"+i+".jpg";//重寫圖片路徑 textadd();        }        //為相應的映像添加文字        function textadd(){        switch (i) {case 1: textobj.innerHTML="《霸王別姬》<br/>說的是一輩子!差一年,一個月,一天,一個時辰,都不算一輩子! ";break;case 2:  textobj.innerHTML="《春光乍泄》<br/>試問誰不想從頭來過,但世間又有多少愛可以重來呢?";break;case 3:  textobj.innerHTML="《東邪西毒》<br/>你不能再擁有的時候。你唯一可以做的,就是讓自己不要忘記。";break;}        }        

 

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.