js 實現(xiàn)無縫滾動 兼容IE和FF
更新時間:2009年07月15日 00:26:27 作者:
js 實現(xiàn)無縫滾動 兼容IE FF,大家可以看看。
原理解析:
1、首先給容器設定高度或?qū)挾?,比如ul,設置ul高40px;overflow:hidden;
2、容器高度設定后,內(nèi)容的高度超出40px,超過部分溢出,被隱藏,scrollTop屬性可用,這一點可以用overflow:scroll來看效果;
3、改變?nèi)萜鞯膕crollTop(上下滾動)屬性的值,讓內(nèi)容上下移動一個節(jié)點的位置(滾動的原理);
4、到滾動的高度scrollTop大于或等于要滾動節(jié)點的高度時,設置scrollTop=0,并把把子節(jié)點樹中的第一個移動到最后,重新開始滾動,無間斷循環(huán)滾動效果就出現(xiàn)了。
html 源碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>滾動</title>
<script type="text/javascript" src="divCycle.js" src="divCycle.js"></script>
<style><!--
li{ height:20px; mar}
--></style><style bogus="1">li{ height:20px; mar}</style>
</head>
<body>
<div>
<ul id="list" style="border:1px #ccc solid; overflow:hidden; height:40px; width:100px; margin:0; padding:0; list-style-type:none;">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
</div>
<script type="text/javascript"><!--
new simpleScroll("list",20,40,1);
// --></script>
</body>
</html>
js源碼
// JavaScript Document
/*****
@author leaves chen (leaves615@gmail.com)
@copyright 2009
*****/
var pause=false;
var scrollTimeId=null;
var container=null;
var lineHeight=null;
var speed=0;
var delay=0;
simpleScroll=function(container1,lineHeight1,speed1,delay1){
container=document.getElementById(container1);
lineHeight=lineHeight1;
speed=speed1;
delay=delay1;
//滾動效果
scrollexc=function(){
if(pause) return ;
container.scrollTop+=2;
var lh=lineHeight||container.getElementsByTagName('li')[0].offsetHeight;
if(container.scrollTop%lh<=1){
clearInterval(scrollTimeId);
fire();
container.scrollTop=0;
setTimeout(start,delay*1000);
}
};
//開始滾動
start=function(){
var lh=lineHeight||container.getElementsByTagName('li')[0].offsetHeight;
if (container.scrollHeight - container.offsetHeight >= lh)
scrollTimeId = setInterval(scrollexc, speed);
};
//把子節(jié)點樹中的第一個移動到最后
fire=function(){
container.appendChild(container.getElementsByTagName('li')[0]);
};
container.onmouseover=function(){pause=true;};
container.onmouseout=function(){pause=false;};
setTimeout(start,delay*1000);
};
1、首先給容器設定高度或?qū)挾?,比如ul,設置ul高40px;overflow:hidden;
2、容器高度設定后,內(nèi)容的高度超出40px,超過部分溢出,被隱藏,scrollTop屬性可用,這一點可以用overflow:scroll來看效果;
3、改變?nèi)萜鞯膕crollTop(上下滾動)屬性的值,讓內(nèi)容上下移動一個節(jié)點的位置(滾動的原理);
4、到滾動的高度scrollTop大于或等于要滾動節(jié)點的高度時,設置scrollTop=0,并把把子節(jié)點樹中的第一個移動到最后,重新開始滾動,無間斷循環(huán)滾動效果就出現(xiàn)了。
html 源碼
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>滾動</title>
<script type="text/javascript" src="divCycle.js" src="divCycle.js"></script>
<style><!--
li{ height:20px; mar}
--></style><style bogus="1">li{ height:20px; mar}</style>
</head>
<body>
<div>
<ul id="list" style="border:1px #ccc solid; overflow:hidden; height:40px; width:100px; margin:0; padding:0; list-style-type:none;">
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
</div>
<script type="text/javascript"><!--
new simpleScroll("list",20,40,1);
// --></script>
</body>
</html>
js源碼
復制代碼 代碼如下:
// JavaScript Document
/*****
@author leaves chen (leaves615@gmail.com)
@copyright 2009
*****/
var pause=false;
var scrollTimeId=null;
var container=null;
var lineHeight=null;
var speed=0;
var delay=0;
simpleScroll=function(container1,lineHeight1,speed1,delay1){
container=document.getElementById(container1);
lineHeight=lineHeight1;
speed=speed1;
delay=delay1;
//滾動效果
scrollexc=function(){
if(pause) return ;
container.scrollTop+=2;
var lh=lineHeight||container.getElementsByTagName('li')[0].offsetHeight;
if(container.scrollTop%lh<=1){
clearInterval(scrollTimeId);
fire();
container.scrollTop=0;
setTimeout(start,delay*1000);
}
};
//開始滾動
start=function(){
var lh=lineHeight||container.getElementsByTagName('li')[0].offsetHeight;
if (container.scrollHeight - container.offsetHeight >= lh)
scrollTimeId = setInterval(scrollexc, speed);
};
//把子節(jié)點樹中的第一個移動到最后
fire=function(){
container.appendChild(container.getElementsByTagName('li')[0]);
};
container.onmouseover=function(){pause=true;};
container.onmouseout=function(){pause=false;};
setTimeout(start,delay*1000);
};
相關文章
js函數(shù)參數(shù)設置默認值的一種變通實現(xiàn)方法
js函數(shù)中有個儲存參數(shù)的數(shù)組arguments,因此js版支持參數(shù)默認值的函數(shù)可以通過另外一種變通的方法實現(xiàn)2014-05-05JS實現(xiàn)表單多文件上傳樣式美化支持選中文件后刪除相關項
在項目開發(fā)中我們經(jīng)常遇到文件上傳的功能,根據(jù)需求有多文件上傳和單文件上傳,今天小編給大家實例講解下表單多文件上傳樣式美化支持選中文件后刪除相關項,非常不錯,感興趣的朋友一起看看吧2016-09-09