js實現無縫輪播
本文實例為大傢分享瞭js實現文字無縫輪播的具體代碼,供大傢參考,具體內容如下
所用到的知識點:
innerHTML 獲取或設置某個對象的內容
scrollTop 滾動條滾動的距離,此屬性是系統內置定義好的
offsetHeight 獲取或設置對象的高度
setInterval() 開啟定時器
clearInterval() 關閉定時器
html佈局結構如下:
其中con2是用來儲存復制con1內容的容器
<body> <div id="mooc"> <!-- 頭部 --> <h3 id="moocTitle">最新課程<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" target="_self">更多>></a> </h3> <!-- 頭部結束 --> <!-- 中間 --> <div id="moocBox"> <ul id="con1"> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1.學會html5 絕對的逆襲(案例)</a><span>2013-09-18</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >2.tab頁面切換效果(案例)</a><span>2013-10-09</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >3.圓角水晶按鈕制作(案例)</a><span>2013-10-21</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >4.HTML+CSS基礎課程(系列)</a><span>2013-11-01</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >5.分頁頁碼制作(案例)</a><span>2013-11-06</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >6.導航條菜單的制作(案例)</a><span>2013-11-08</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >7.信息列表制作(案例)</a><span>2013-11-15</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >8.下拉菜單制作(案例)</a><span>2013-11-22</span></li> <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >9.如何實現“新手引導”效果</a><span>2013-12-06</span></li> </ul> <ul id="con2"> </ul> </div> <!-- 中間結束 --> </div> </body>
css樣式如下:
<style> body { font-size: 12px; line-height: 24px; text-algin: center; /* 頁面內容居中 */ } * { margin: 0px; padding: 0px; /* 去掉所有標簽的marign和padding的值 */ } ul { list-style: none; /* 去掉ul標簽默認的點樣式 */ } a img { border: none; /* 超鏈接下,圖片的邊框 */ } a { color: #333; text-decoration: none; /* 超鏈接樣式 */ } a:hover { color: #ff0000; } #mooc { width: 399px; border: 5px solid #ababab; -moz-border-radius: 15px; /* Gecko browsers */ -webkit-border-radius: 15px; /* Webkit browsers */ border-radius: 15px; box-shadow: 2px 2px 10px #ababab; margin: 50px auto 0; text-align: left; /* 讓新聞內容靠左 */ } /* 頭部樣式 */ #moocTitle { height: 62px; overflow: hidden; /* 這個一定要加上,內容超出的部分要隱藏,免得撐高頭部 */ font-size: 26px; line-height: 62px; padding-left: 30px; background-image: -moz-linear-gradient(top, #f05e6f, #c9394a); /* Firefox */ background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #f05e6f), color-stop(1, #c9394a)); /* Saf4+, Chrome */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#8fa1ff', endColorstr='#f05e6f', GradientType='0'); /* IE*/ border: 1px solid ##f05e6f; -moz-border-radius: 8px 8px 0 0; /* Gecko browsers */ -webkit-border-radius: 8px 8px 0 0; /* Webkit browsers */ border-radius: 8px 8px 0 0; color: #fff; position: relative; } #moocTitle a { position: absolute; right: 10px; bottom: 10px; display: inline; color: #fff; font-size: 12px; line-height: 24px; } /* 底部樣式 */ #moocBot { width: 399px; height: 10px; overflow: hidden; /* 這個一定要加上,內容超出的部分要隱藏,免得撐高底部結構 */ } /* 中間樣式 */ #moocBox { height: 144px; width: 335px; margin-left: 25px; margin-top: 10px; overflow: hidden; /* 這個一定要加,超出的內容部分要隱藏,免得撐高中間部分 */ } #mooc ul li { height: 24px; } #mooc ul li a { width: 180px; float: left; display: block; overflow: hidden; text-indent: 15px; height: 24px; } #mooc ul li span { float: right; color: #999; } </style>
js代碼如下:
<script> var area=document.getElementById('moocBox'); area.scrollTop=0;//scrollTop初始化對象滾動條的距離,此屬性是系統標準化的 //獲取第一個ul對象 var con1=document.getElementById('con1'); //獲取第二個ul對象 var con2=document.getElementById('con2'); //將第一個ul對象裡的內容給第二個ul對象 con2.innerHTML=con1.innerHTML; //封裝函數 用來判斷滾動條的距離與盒子高度的關系 function scrollUp(){ if(area.scrollTop>=con1.offsetHeight){ area.scrollTop=0; }else{ area.scrollTop++; } } //聲明定時器 var timer=null; //解決暴力用戶 在開啟定時器之前最好先清除一下定時器 clearInterval(timer); //開始定時器 timer=setInterval(scrollUp,50); //為對象設置綁定鼠標滑過 area.onmouseover=function(){ clearInterval(timer); } //為對象綁定鼠標劃出 area.onmouseout=function(){ timer=setInterval(scrollUp,50); } </script>
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。