原生js實現(xiàn)中獎信息無間隙滾動效果
知識要點(diǎn)
1.實現(xiàn)原理:通過定時器不斷改變列表的top值。而達(dá)到無間隙滾動就要對信息列表復(fù)制一份,再判斷兩個列表的top臨界值初始化。最后注意的就是 防止動畫積存需要對定時器進(jìn)行清除。
2.用到的屬性方法:
setInterval() //每隔一定時間執(zhí)行一次函數(shù),可以無限執(zhí)行下去 clearInterval() //清除指定的setInterval setTimeout() //經(jīng)過一定時間執(zhí)行一次函數(shù),只能執(zhí)行一次,如果要無限下去需要在函數(shù)里自行設(shè)置 clearTimeout() //清除指定的setTimeout
剩下的就是一些基礎(chǔ)的dom操作
完整代碼
注:因為看到了天貓積分的抽獎頁面所以想自己寫試試,審查天貓代碼看到原理是改變列表top值,無縫滾動是自己瞎琢磨的,估計應(yīng)該有更高效的方法還請大神指教。。
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;}
h1,h2,h3,h4,h5,h6{font-size:100%;}
address,cite,dfn,em,var{font-style:normal;}
code,kbd,pre,samp{font-family:courier new,courier,monospace;}
ul,ol{list-style:none;}
a{text-decoration:none;}
a:hover{text-decoration:none;}
sup{vertical-align:text-top;}
sub{vertical-align:text-bottom;}
legend{color:#000;}
fieldset,img{border:0;}
button,input,select,textarea{font-size:100%;}
table{border-collapse:collapse;border-spacing:0;}
.clear{clear: both;float: none;height: 0;overflow: hidden;}
.title{background: #D20F25; width: 200px; height: 40px; color: #fff; line-height: 40px;}
.title p{margin-left: 30px;}
#vip{background: #D20F25; width: 200px; height: 105px; color: #FF92AD; overflow: hidden; position: relative; }
#list{position: absolute;}
#vip li{ height: 50px; line-height: 24px; font-size: 12px; margin-left: 30px; }
</style>
</head>
<body>
<div class="title"><p>會員中獎榜</p></div>
<div id="vip">
<ul id="list" style="top: 0px;">
<li>m**b<br/>抽中18積分</li>
<li>小**宮<br/>抽中28積分</li>
<li>金**告<br/>抽中8積分</li>
<li>真**生<br/>抽中88積分</li>
<li>鄭**9<br/>抽中18積分</li>
<li>l**美<br/>抽中8積分</li>
</ul>
</div>
<script type="text/javascript">
//在頁面加載完后立即執(zhí)行多個函數(shù)方案
function addloadEvent(func){
var oldonload=window.onload;
if(typeof window.onload !="function"){
window.onload=func;
}
else{
window.onload=function(){
if(oldonload){
oldonload();
}
func();
}
}
}
//在頁面加載完后立即執(zhí)行多個函數(shù)方案結(jié)束
addloadEvent(nes);
function nes(){
//獲取列表父容器
var vip=document.getElementById("vip");
//獲取信息列表
var list=document.getElementById("list");
//創(chuàng)建第二個列表設(shè)置一系列樣式id等
var list1=document.createElement("ul");
list1.setAttribute("id","list1");
//初始位置為300正好在第一個列表的下面
list1.style.top=300+"px";
list1.style.position="absolute";
//插入文檔流
vip.appendChild(list1);
//把第一個列表的結(jié)構(gòu)內(nèi)容復(fù)制給第二個
list1.innerHTML=list.innerHTML;
//第一個列表
function b(){
//top值為當(dāng)前的top減10
list.style.top=parseInt(list.style.top)-10+"px";
//如果top值為-300那么初始化top
if(parseInt(list.style.top)==-300){
list.style.top=0;
}
//這里是實現(xiàn)間隔滾動判斷
//當(dāng)top值整除50(每個li的高度)時候清除定時器
if(parseInt(list.style.top)%50==0){
clearInterval(time);
//然后兩秒后再次執(zhí)行time=setInterval
se=setTimeout(function(){time=setInterval(b,30);},2000);
}
};
//定時器
time=setInterval(b,30);
//第二個列表與第一個列表操作一樣,只是修改了高度
function c(){
list1.style.top=parseInt(list1.style.top)-10+"px";
if(parseInt(list1.style.top)==0){
list1.style.top=300+"px";
}
if(parseInt(list1.style.top)%50==0){
clearInterval(time1);
se1=setTimeout(function(){time1=setInterval(c,30);},2000);
}
};
time1=setInterval(c,30);
//鼠標(biāo)移入列表時 清除兩個定時器
vip.onmouseover=function(){
clearTimeout(se);
clearTimeout(se1);
clearInterval(time);
clearInterval(time1);
};
//鼠標(biāo)劃出時先判斷如果定時器在執(zhí)行則清除
vip.onmouseout=function(){
if(time&&time1) {
clearInterval(time);
clearInterval(time1)
}
if(se&&se1) {
clearTimeout(se);
clearTimeout(se1)
}
//再次執(zhí)行定時器
se=setTimeout(function(){time=setInterval(b,30);},2000);
se1=setTimeout(function(){time1=setInterval(c,30);},2000);
};
}
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關(guān)文章
javascript實現(xiàn)在某個元素上阻止鼠標(biāo)右鍵事件的方法和實例
這篇文章主要介紹了javascript實現(xiàn)在某個元素上阻止鼠標(biāo)右鍵事件的方法和實例,需要的朋友可以參考下2014-08-08
JS中showModalDialog關(guān)閉子窗口刷新主窗口用法詳解
這篇文章主要介紹了JS中showModalDialog關(guān)閉子窗口刷新主窗口用法,結(jié)合具體實例形式較為詳細(xì)的分析了showModalDialog常見用法與相關(guān)使用技巧,需要的朋友可以參考下2017-03-03
JavaScript 用cloneNode方法克隆節(jié)點(diǎn)的代碼
很多時候我們需要通過HTML DOM 的方式,用JavaScript 動態(tài)生成很多相同的節(jié)點(diǎn),包括其子節(jié)點(diǎn)2012-10-10
IE不出現(xiàn)Flash激活框的小發(fā)現(xiàn)的js實現(xiàn)方法
IE不出現(xiàn)Flash激活框的小發(fā)現(xiàn)的js實現(xiàn)方法...2007-09-09
JavaScript實現(xiàn)垂直向上無縫滾動特效代碼
下面小編就為大家?guī)硪黄狫avaScript實現(xiàn)垂直向上無縫滾動特效代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11

