2012/11/8 11:56:50作者:佚名來源:網(wǎng)絡(luò)
【實(shí)例名稱】
JS實(shí)現(xiàn)多幅圖片分頁滾動(dòng)顯示
【實(shí)例描述】
本例可用于圖片新聞的展示,通過兩個(gè)div控件循環(huán)顯示所有的圖片??尚薷谋纠械拇a調(diào)整圖片滾動(dòng)的速度。
【實(shí)現(xiàn)代碼】
<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>標(biāo)題頁-學(xué)無憂(www.wangbatian.cn)</title> <SCRIPT language="JavaScript"> var scrollerwidth=90 //指定div的高度 var scrollerheight=32 //指定div的寬度 var scrollerbgcolor='white' var pausebetweenimages=3000 //圖片的間隔時(shí)間,默認(rèn)為3秒。 //帶鏈接的圖片,存放在數(shù)組中 var slideimages=new Array() slideimages[0]='<A href="http://www.baidu.com" target=_blank> <IMG alt=百度搜索 border=0 height=40 src=http://www.baidu.com/img/logo.gif width=100></A>' slideimages[1]='<A href=http://www.google.cn target=_blank> <IMG alt=google搜索 border=0 height=40 src=http://www.google.cn/intl/zh-CN/images/logo_cn.gif width=100></A>' slideimages[2]='<A href=# target=_blank> <IMG alt=廣告位置為你準(zhǔn)備 border=0 height=40 src="" width=100></A>' slideimages[3]='<A href=http://www.google.cn target=_blank> <IMG alt=google搜索 border=0 height=40 src=http://www.google.cn/intl/zh-CN/images/logo_cn.gif width=100></A>' if (slideimages.length>1) i=2 //初始化一個(gè)變量,用來做圖片數(shù)組的索引 else i=0 function move1(mydiv) { tdiv=eval(mydiv) //獲取div對象 if (tdiv.style.pixelTop>0&&tdiv.style.pixelTop<=4){ //判斷div的y坐標(biāo) tdiv.style.pixelTop=0 //指定div的y坐標(biāo) setTimeout("move1(tdiv)",pausebetweenimages) //設(shè)置轉(zhuǎn)換的時(shí)間間隔 setTimeout("move2(secondDiv)",pausebetweenimages) return } if (tdiv.style.pixelTop>=tdiv.offsetHeight*-1){ tdiv.style.pixelTop-=5 //div開始往上滾動(dòng) setTimeout("move1(tdiv)",100) } else{ tdiv.style.pixelTop=scrollerheight //指定div的高度 tdiv.innerHTML=slideimages[i] //將圖片顯示在div中 if (i==slideimages.length-1 ) //如果已經(jīng)循環(huán)到底,再從第一張開始循環(huán) i=0 else i++ } } function move2(mydiv) { tdiv2=eval(mydiv) //獲取第二個(gè)div if (tdiv2.style.pixelTop>0&&tdiv2.style.pixelTop<=4){ //判斷div的y坐標(biāo) tdiv2.style.pixelTop=0 //指定div的y坐標(biāo) setTimeout("move2(tdiv2)",pausebetweenimages) //設(shè)置轉(zhuǎn)換的時(shí)間間隔 setTimeout("move1(firstDiv)",pausebetweenimages) return } if (tdiv2.style.pixelTop>=tdiv2.offsetHeight*-1){ tdiv2.style.pixelTop-=5 //第二個(gè)div開始向上滾動(dòng) setTimeout("move2(secondDiv)",100) } else{ tdiv2.style.pixelTop=scrollerheight //指定第二個(gè)div的高度 tdiv2.innerHTML=slideimages[i] //將圖片顯示在div中 if (i==slideimages.length -1) //如果已經(jīng)循環(huán)到底,再從第一張開始循環(huán) i=0 else i++ } } function startscroll() //調(diào)用實(shí)現(xiàn)div層移動(dòng)的方法 { if (document.all){ move1(firstDiv) secondDiv.style.top=scrollerheight //設(shè)置第二章圖片的位置 } } window.onload=startscroll //窗體一架載,便開始顯示圖片 </SCRIPT> </head> <body> <SCRIPT language="JavaScript"> if (document.all){ document.writeln('<span id="main2" style="position:relative; width:'+scrollerwidth+';height:'+scrollerheight+';overflow:hiden; background-color:'+scrollerbgcolor+'">') document.writeln('<div style="position:absolute; width:'+scrollerwidth+';height:'+scrollerheight+'; clip:rect(0 '+scrollerwidth+' '+scrollerheight+' 0);left:0;top:0">') document.writeln('<div id="firstDiv" style="position:absolute;width:'+scrollerwidth+';left:0;top:1;">') document.write(slideimages[0]) document.writeln('</div>') document.writeln('<div id="secondDiv" style="position:absolute;width:'+scrollerwidth+';left:0;top:0">') document.write(slideimages[1]) document.writeln('</div>') document.writeln('</div>') document.writeln('</span>') } </SCRIPT> </body> </html>
【運(yùn)行效果】
【難點(diǎn)剖析】
本例重點(diǎn)是使用兩個(gè)diV循環(huán)顯示所有的圖片。為了屏蔽diV的圖層,需要將窗體的背景色設(shè)置為“white”(白色)。然后將第一個(gè)div的Y坐標(biāo)設(shè)置為“1”,指定時(shí)間過后再讓此div的y坐標(biāo)自動(dòng)減小,實(shí)現(xiàn)圖層上移的效果。然后設(shè)置第二個(gè)div的y坐標(biāo),實(shí)現(xiàn)第二張圖片的顯示,依次循環(huán)顯示所有的圖片。
【源碼下載】
為了JS代碼的準(zhǔn)確性,請點(diǎn)擊:JS實(shí)現(xiàn)多幅圖片分頁滾動(dòng)顯示 進(jìn)行本實(shí)例源碼下載
標(biāo)簽: 圖片 顯示
相關(guān)文章
金山數(shù)據(jù)恢復(fù)大師官方版 v1.0.0.2
詳情南方測繪Cass10v10.1.6中文
詳情revit 2017
詳情KeyShot Pro 9中文(附安裝教程) v9.0.286
詳情網(wǎng)易新聞客戶端v105.3
詳情AIMP4v5.11.2421中文綠色美化版
詳情onekey一鍵還原v18.0.18.1008
詳情浩辰CAD2020綠色v20.0
詳情好圖網(wǎng)圖標(biāo)轉(zhuǎn)換工具v4.9.7
詳情Adobe indesign cs6
詳情aardiov35.0.0中文最新版
詳情Adobe Creative Cloud 2024簡體中文v5.3.0.48
詳情暴風(fēng)影音16 v9.04.1029去廣告精簡版
詳情ASP.NET Maker 2019(ASP.NET代碼生成工具)v12.0.4.0
詳情暴風(fēng)影音v5.92.0824.1111
詳情迅雷5穩(wěn)定版v5.8.14.706
詳情使命召喚17官方中文版 v1.0
詳情死亡之雨新的僵尸病毒中文v1.0綠色免安裝版
詳情輻射4v1.7.15.0整合版
詳情克莉2v1.0中文版
詳情冬日計(jì)劃v1.2中文版
詳情刺客信條英靈殿v1.0吾愛
詳情刺客信條英靈殿終極v1.0免安裝
詳情動(dòng)物森友會(huì)v1.10.0最新
詳情哈迪斯殺出地獄v1.37中文
詳情嗜血印中文豪華版v1.0豪華版 附游戲攻略秘籍
詳情城市戰(zhàn)斗v1.0中文
詳情尼爾人工生命v1.0steam免費(fèi)
詳情尼爾人工生命升級版v1.0PC
詳情層層恐懼2中文v1.0綠色免安裝版
詳情往日不再v1.0 steam
詳情往日不再v1.0pc
詳情小生活游戲內(nèi)置MOD版v2.0(57)安卓版
詳情使命召喚手游測試服最新版v1.9.41安卓版
詳情三國謀定天下官服v1.2.1安卓版
詳情熱血新手村高爆版v1.0.0安卓版
詳情我養(yǎng)你啊手機(jī)版v1.0.0安卓版
詳情看懂了就很恐怖的故事(細(xì)思極恐)中文版v1.0安卓版
詳情背包英雄中文手機(jī)版v1.1.1安卓版
詳情glow官方版v2.0.9安卓版
詳情三國大時(shí)代4霸王立志官方正版v1.9安卓版
詳情飛盧小說閱讀器手機(jī)版v7.0.7安卓版
詳情牛?;浾Z詞典軟件v20.4.4安卓版
詳情PrettyUp視頻美化瘦身軟件v2.3.0
詳情化學(xué)方程式app中文版v1.1.0.20安卓版
詳情地下城堡3魂之詩2024安卓最新版v1.2.3安卓版
詳情南方都市報(bào)v6.10.0安卓版
詳情阿修羅之眼正版v1.0.10安卓版
詳情