2012/11/8 15:16:16作者:佚名來源:網絡
【實例名稱】
JS實現圖片新聞切換效果
【實例描述】
新聞切換技術想必大家都很熟悉,本例研究其切換效果如何實現。
【實例代碼】
<html> <head> <style type="text/css"> body { text-align: center; margin:0; padding:0; background: #FFF; font-size:12px; color:#000;} div,form,img,ul,ol,li,dl,dt,dd {margin: 0; padding: 0; border: 0;} h1,h2,h3,h4,h5,h6 { margin:0; padding:0;} table,td,tr,th{font-size:12px;} a:link {color: #000; text-decoration:none;} a:visited {color: #83006f;text-decoration:none;} a:hover {color: #c00; text-decoration:underline;} a:active {color: #000;} .focusPic{margin:0 auto; width:244px;} .focusPic .pic{margin:0 auto; width:240px; height:180px; padding:2px 0 0;} .focusPic .adPic{margin:0 auto 5px; width:240px; height:29px; overflow:hidden;background:url(http://tech.163.com/newimg/adpic.gif);} .focusPic .adPic .text{float:right; padding:9px 4px 0 0; width:140px;} .focusPic .adPic .text a{color:#1f3a87;} .focusPic .adPic .text a:hover{color:#bc2931;} .focusPic h2{ float:left; width:232px;padding:4px 0 3px 12px; font-size:14px; text-align:left;} .focusPic p{float:left; width:226px;line-height:160%; margin:0; text-align:left;padding:0 0 10px 12px;} .focusPic p img {margin:0px 0 2px;} .focusPic .more{ margin:0 auto; width:240px; } .focusPic .more .textNum{float:right; margin:0 8px 0 0;padding:0 0 4px;} .focusPic .more .textNum .text{float:left; font-weight:bold; padding:7px 6px 0 0; color:#666;} .focusPic .more .textNum .num{float:left; width:113px; height:19px;} .focusPic .more .textNum .bg1{ background:url(http://tech.163.com/newimg/num1.gif);} .focusPic .more .textNum .bg2{ background:url(http://tech.163.com//num2.gif);} .focusPic .more .textNum .bg3{ background:url(http://tech.163.com/newimg/num3.gif);} .focusPic .more .textNum .bg4{ background:url(http://tech.163.com/newimg/num4.gif);} .focusPic .more .textNum .num ul{ float:left; width:113px;} .focusPic .more .textNum .num li{float:left; width:28px; font-weight:bold;display:block; color:#fff; list-style-type:none; padding:6px 0 0;} .focusPic .more .textNum .num li a{color:#fff; padding:0 5px; } .focusPic .more .textNum .num li a:visited{color:#fff;} .focusPic .more .textNum .num li a:hover{color:#ff0;} </style> <script language="JavaScript" type="text/javascript"> var nn; nn=1; setTimeout('change_img()',6000); function change_img() { if(nn>4) nn=1 setTimeout('setFocus1('+nn+')',6000); nn++; tt=setTimeout('change_img()',6000); } function setFocus1(i) { selectLayer1(i); } function selectLayer1(i) { switch(i) { case 1: document.getElementById("focusPic1").style.display="block"; document.getElementById("focusPic2").style.display="none"; document.getElementById("focusPic3").style.display="none"; document.getElementById("focusPic4").style.display="none"; document.getElementById("focusPic1nav").style.display="block"; document.getElementById("focusPic2nav").style.display="none"; document.getElementById("focusPic3nav").style.display="none"; document.getElementById("focusPic4nav").style.display="none"; break; case 2: document.getElementById("focusPic1").style.display="none"; document.getElementById("focusPic2").style.display="block"; document.getElementById("focusPic3").style.display="none"; document.getElementById("focusPic4").style.display="none"; document.getElementById("focusPic1nav").style.display="none"; document.getElementById("focusPic2nav").style.display="block"; document.getElementById("focusPic3nav").style.display="none"; document.getElementById("focusPic4nav").style.display="none"; break; case 3: document.getElementById("focusPic1").style.display="none"; document.getElementById("focusPic2").style.display="none"; document.getElementById("focusPic3").style.display="block"; document.getElementById("focusPic4").style.display="none"; document.getElementById("focusPic1nav").style.display="none"; document.getElementById("focusPic2nav").style.display="none"; document.getElementById("focusPic3nav").style.display="block"; document.getElementById("focusPic4nav").style.display="none"; break; case 4: document.getElementById("focusPic1").style.display="none"; document.getElementById("focusPic2").style.display="none"; document.getElementById("focusPic3").style.display="none"; document.getElementById("focusPic4").style.display="block"; document.getElementById("focusPic1nav").style.display="none"; document.getElementById("focusPic2nav").style.display="none"; document.getElementById("focusPic3nav").style.display="none"; document.getElementById("focusPic4nav").style.display="block"; break; } } </script> </head>
<body> <div class="focusPic"> <div id="focusPic1" style="display:block ;"> <div class="pic"> <a href="http://tech.163.com/11/0411/05/71BB3NO0000915BF.html"> <img src=http://img3.cache.netease.com/tech/2011/4/11/2011041105573667bd5.jpg alt="Facebook或年內曲線入華" width="240" height="180" border="0" /></a> </div> <h2><a href="http://tech.163.com/11/0411/05/71BB3NO0000915BF.html"> Facebook或年內曲線入華</a></h2> <p>有分析稱,Facebook入華不外乎兩條途徑:一是成立合資公司,二是合作、收購。點擊進入查看…… <img src="/newimg/i2.gif" alt="詳細" width="3" height="5" /> <a href=http://tech.163.com/11/0411/05/71BB3NO0000915BF.html class="cDRed">詳細</a></p> </div> <div id="focusPic2" style="display: none ;"> <div class="pic"> <a href="http://tech.163.com/discover/"> <img src=http://cimg.163.com/tech/2006/1/17/200601171557008cee7.jpg alt="顛覆叢林動物生存法則" width="240" height="180" border="0" /></a> </div> <h2><a href="http://tech.163.com/discover/">顛覆叢林動物生存法則</a></h2> <p>群居動物的這種行為顛覆了我們通常認為的,在動物世界通行的 “叢林法則”, 動物不都自私,不都是弱肉強食的。<img src="/newimg/i2.gif" alt="詳細" width="3" height="5" /> <a href="http://tech.163.com/discover/" class="cDRed"> 詳細</a></p> </div> <div id="focusPic3" style="display: none ;"> <div class="pic"> <a href="http://tech.163.com/special/00091MNJ/itobserve015.html"> <img src="http://cimg.163.com/tech/2006/1/17/2006011711483290a60.jpg" alt="WAPI并非貿易陰謀" width="240" height="180" border="0" /></a> </div> <h2><a href="http://tech.163.com/special/00091MNJ/itobserve015.html">WAPI并非貿易陰謀</a> </h2> <p>近日國家做出決定:“將向其他的國內及國外企業公布該算法”。事實證明中國WAPI標準并非是貿易陰謀。 <img src="/newimg/i2.gif" alt="詳細" width="3" height="5" /> <a href="http://tech.163.com/special/00091MNJ/itobserve015.html" class="cDRed"> 詳細</a></p> </div> <div id="focusPic4" style="display: none ;"> <div class="pic"> <a href="http://tech.163.com/special/00091OSI/horizons.html"> <img src=http://cimg.163.com/tech/2006/1/17/200601171002503f251.jpg alt="新視野號探測冥王星特別專題" width="240" height="180" border="0" /></a> </div> <h2><a href="http://tech.163.com/special/00091OSI/horizons.html"> 新視野號探測冥王星特別專題</a></h2> <p>美國宇航局將于北京時間18日凌晨2時24分發射新視野號探測器, 造訪這顆人類唯一尚未探測過的行星-冥王星。<img src="/newimg/i2.gif" alt="詳細" width="3" height="5" /> <a href=http://tech.163.com/special/00091OSI/horizons.html class="cDRed">詳細</a></p> </div> <div class="more"> <div class="textNum"> <div class="text">> 更多頭圖新聞</div> <div class="num bg1" id="focusPic1nav" style="display: block;"> <ul> <li>1</li> <li><a href="javascript:setFocus1(2);" target="_self">2</a></li> <li><a href="javascript:setFocus1(3);" target="_self">3</a></li> <li><a href="javascript:setFocus1(4);" target="_self">4</a></li> </ul> </div> <div class="num bg2" id="focusPic2nav" style="display: none;"> <ul> <li><a href="javascript:setFocus1(1);" target="_self">1</a></li> <li>2</li> <li><a href="javascript:setFocus1(3);" target="_self">3</a></li> <li><a href="javascript:setFocus1(4);" target="_self">4</a></li> </ul> </div> <div class="num bg3" id="focusPic3nav" style="display: none;"> <ul> <li><a href="javascript:setFocus1(1);" target="_self">1</a></li> <li><a href="javascript:setFocus1(2);" target="_self">2</a></li> <li>3</li> <li><a href="javascript:setFocus1(4);" target="_self">4</a></li> </ul> </div> <div class="num bg4" id="focusPic4nav" style="display: none;"> <ul> <li><a href="javascript:setFocus1(1);" target="_self">1</a></li> <li><a href="javascript:setFocus1(2);" target="_self">2</a></li> <li><a href="javascript:setFocus1(3);" target="_self">3</a></li> <li>4</li> </ul> </div> </div> </div> </div>
</body> </html> 需要在bodyr中添加一些新聞,注意這些新聞的布局。
【運行效果】
【難點剖析】
本例的重點是頁面的布局和控件的樣式。其中要注意獲取頁面元素使用的是“document.getElementById()”方法,其中的參數就是要獲取的元素id。修改元素的樣式使用“style”屬性,其中“style.display”用來控制元素的顯示和隱藏。
【源碼下載】
為了JS代碼的準確性,請點擊:JS實現圖片新聞切換效果 進行本實例源碼下載
標簽: 圖片
相關文章
ip雷達 v5.0.2綠色版
詳情WinSCPv5.15.1綠色中文版
詳情filezilla client v5.5
詳情wireshark中文版 v3.4.9
詳情Boson Netsimv10.8破解版
詳情風行播放器 v3.0.6.106
詳情愛奇藝客戶端 v10.1.0.6636官方版
詳情搜狐影音 v7.0.19.0官方版
詳情樂視視頻 v7.3.2.192
詳情華數tv客戶端PC版 v1.1.2.0
詳情cbox央視影音 v5.1.2.1經典版
詳情軟媒收音機(魔方) v1.13
詳情傾聽網絡收音機 v3.0.0.0
詳情魅影流聲全球網絡收音機 v1.0.0.1
詳情酷狗電臺 v8.2.75
詳情暴龍網絡收音機 v2.5.1
詳情克莉2v1.0中文版
詳情冬日計劃v1.2中文版
詳情刺客信條英靈殿v1.0吾愛破解版
詳情刺客信條英靈殿終極v1.0免安裝破解版
詳情動物森友會v1.10.0最新破解版
詳情哈迪斯殺出地獄v1.37中文破解版
詳情嗜血印中文豪華版v1.0豪華版破解版 附游戲攻略秘籍
詳情城市戰斗v1.0中文破解版
詳情尼爾人工生命v1.0steam免費破解版
詳情尼爾人工生命升級版v1.0PC破解版
詳情尼爾人工生命升級版v1.2247中文破解版
詳情層層恐懼2中文破解版v1.0綠色免安裝版
詳情往日不再v1.0 steam破解版
詳情往日不再v1.0pc破解版
詳情救贖之路v1.0中文破解版
詳情斯蓋爾女仆v1.0中文破解版
詳情新盜墓筆記官方版v1.215.589730
詳情無盡的拉格朗日無限金幣鉆石版v1.2.267172破解版
詳情明日之后無限小太陽版v1.0.246安卓版
詳情雙生幻想vivo版v6.6.83安卓版
詳情劍網1歸來內購免費版v1.1.116安卓版
詳情模擬城市我是市長國際版v0.69.21342.22832安卓版
詳情迷你世界999999迷你幣版v1.20.5破解版
詳情王牌戰爭無限內購版v9.7安卓版
詳情云上城之歌不氪金版v10.16安卓版
詳情王鏟鏟的致富之路破解版v1.1.8安卓版
詳情創造與魔法無限點券版v1.0.0505安卓版
詳情狩獵使命九游版v1.6.6.000
詳情魔鏡物語變態版v2.1.4無限金幣版
詳情黑月手游免費版v2.2.0安卓版
詳情曙光英雄v1.0.12.0.20無限鉆石版
詳情原始傳奇無限元寶版v1.6.806內購破解版
詳情