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内购破解版
详情