javascript文字滾動顯示實現代碼

來源:互聯網
上載者:User

1.html

 代碼如下 複製代碼

<div class="box" id="marqueebox0">
<ul>
<li style="background:#f8e2ac;">第一行</li>
<li style="background:#f5f5f5;">第二行</li>
<li style="background:#ffe6ec;">第三行</li>
</ul>
</div>


2.css

 代碼如下 複製代碼

.box{
width:150px;
height:25px;
line-height:25px;
border:#bbb 1px solid;
overflow:hidden;
}
.box ul{margin:0; padding:0}
.box li{
height:25px;
line-height:25px;
font-size:12px;
text-align:center;
list-style-type:none;
}

3.JS

 代碼如下 複製代碼

function startmarquee(lh,speed,delay,index){
/*
函數startmarquee的參數:
lh:文字一次向上滾動的距離或高度;
speed:捲動速度;
delay:滾動停頓的時間間隔;
index:可以使封裝後的函數應用於頁面當中不同的元素;
*/
var t;
var p=false;
var o=document.getElementById("marqueebox"+index);
//擷取文檔中的捲動區域對象,賦值給變數o;
o.innerHTML+=o.innerHTML; //對象中的實際內容被複製了一份,包含了兩個ul,當然li標籤也
由原來的3行,變為6行;複製的目的在於給文字不間斷向上滾動提供過渡。
o.onmouseover=function(){p=true}
//滑鼠滑過,停止滾動;
o.onmouseout=function(){p=false}
//滑鼠離開,開始滾動;p是true還是false直接影響到下面start()函數的執行;
o.scrollTop = 0;
//文字內容頂端與捲動區域頂端的距離,初始值為0;
function start(){
t=setInterval(scrolling,speed); //每隔一段時間,setInterval便會執行一次
scrolling函數;speed越大,滾動時間間隔越大,捲動速度越慢;
if(!p){ o.scrollTop += 1;}
//滾動停止或開始,取決於p傳來的布爾值;
}
function scrolling(){
if(o.scrollTop%lh!=0){
//如果不被整除,即一次上移的高度達不到lh,則內容會繼續往上滾動;
o.scrollTop += 1;
if(o.scrollTop>=o.scrollHeight/2) o.scrollTop = 0;
//對象o中的內容之前被複製了一次,所以它的滾動高度,其實是原來內容的兩倍高度;
當內容向上滾動到scrollHeight/2的高度時,全部3行文字已經顯示了一遍,至此整塊內容
scrollTop歸0;再等待下一輪的滾動,從而達到文字不間斷向上滾動的效果;
}else{
clearInterval(t);
//否則清除t,暫停滾動
setTimeout(start,delay);
//經過delay間隔後,啟動start() 再連續滾動
}
}
setTimeout(start,delay);
//第一次啟動滾動;setTimeout會在一定的時間後執行函數start(),且只執行一次
}
//傳遞參數
startmarquee(25,30,3000,0);
//帶停頓效果
startmarquee(25,40,0,1);
//不間斷連續

以下為全部代碼:

 代碼如下 複製代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>文字向上滾動</title>
<style type="text/css">
.box{width:150px; height:25px;line-height:25px; border:#bbb 1px solid; overflow:hidden;}
.box ul{margin:0; padding:0}
.box li{height:25px; line-height:25px; font-size:12px; text-align:center; list-style-type:none;}
</style>
</head>
<body>
<div class="box" id="marqueebox0">
<ul>
<li style="background:#f8e2ac;">第一行</li>
<li style="background:#f5f5f5;">第二行</li>
<li style="background:#ffe6ec;">第三行</li>
</ul>
</div>
 
<div class="box" id="marqueebox1">
<ul>
<li style="background:#f8e2ac;">第一行</li>
<li style="background:#f5f5f5;">第二行</li>
<li style="background:#ffe6ec;">第三行</li>
</ul>
</div>
<script type="text/javascript">
function startmarquee(lh,speed,delay,index){
var t;
var p=false;
var o=document.getElementById("marqueebox"+index);
o.innerHTML+=o.innerHTML;
o.onmouseover=function(){p=true}
o.onmouseout=function(){p=false}
o.scrollTop =0;
function start(){
t=setInterval(scrolling,speed);
if(!p){ o.scrollTop +=1;}
}
function scrolling(){
if(o.scrollTop%lh!=0){
o.scrollTop +=1;
if(o.scrollTop>=o.scrollHeight/2) o.scrollTop = 0;
}else{
clearInterval(t);
setTimeout(start,delay);
}
}
setTimeout(start,delay);
}
startmarquee(25,30,3000,0);
startmarquee(25,40,0,1);
</script>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.