Javascript網頁滾動方法

來源:互聯網
上載者:User
Javascript網頁滾動方法
作者:不詳  來源於:源碼下載吧  發布時間:2007-6-6 16:24:26 [ 字型:大 中 小 ]
 
我想大家都看過各種各樣的滾動(最常用的就是用在新聞的更新上面),一般情況下只要不是圖片,都是用HTML本身就有提供一個很不錯的滾動標記——marquee來完成的(用JS也可以同樣實現這種效果);當然並不是說圖片就不能那樣做。
  以下為marquee的一些基本的參數:
  該標記文法格式如下: 
<marquee 
aligh=left|center|right|top|bottom
bgcolor=#n 
direction=left|right|up|down 
behavior=type 
height=n 
hspace=n 
scrollamount=n 
Scrolldelay=n 
width=n 
VSpace=n 
loop=n>內容</marquee>

  下面解釋一下各參數的含義: 
  align:是設定活動字幕的位置,除了居左、置中、居右三種位置外,又增加靠上(top)和靠下(bottom)兩種位置。 
  Bgcolor:用於設定活動字幕的背景顏色,可以使用英文的單詞也可以是十六進位數的。 
  Direction:用於設定活動字幕的滾動方向是向左(left)、向右(right)、向上(up)、向下(down)。 
  Behavior:用於設定滾動的方式,主要由三種方式:
behavior="scroll"表示由一端滾動到另一端;
behavior="slide":表示由一端快速滑動到另一端,且不再重複;
behavior="alternate"表示在兩端之間來復原動。 
  Height:用於設定滾動字幕的高度。 
  Width:則設定滾動字幕的寬度。 
  Hspace和vspace:分別用於設定滾動字幕的左右邊框和上下邊框的寬度。 
  Scrollamount:用於設定活動字幕的滾動距離。數值越小,滾動的速度就越快。 
  scrolldelay:用於設定滾動兩次之間的延遲時間,數值越小,間隔就越小。 
  Loop:用於設定滾動的次數,當loop=-1表示一直滾動下去,直到頁面更新。其中預設情況是向左滾動無限次,字幕高度是文本高度;滾動範圍:水平滾動的寬度是當前位置的寬度;垂直滾動的高度是當前位置的高度。

  現在要介紹的是一種新型的滾動,利用JS的方法來做字幕的滾動:向上,在中間停頓一到兩秒(就像經典的這種):
  將以下代碼貼到您要顯示的位置就可以了:
<script>
var marqueeContent=new Array(); //定義一個數組,用來存放顯示內容
marqueeContent[0]='<A href="#" onclick="reinitMarquee()">重新整理最新列表</FONT></A>';
marqueeContent[1]='<a href=http://www.blueidea.com/updatelist.asp target=_blank>網站最新更新六十條</a>';
marqueeContent[2]='<A href=http://www.blueidea.com/tech/graph/2003/875.asp target=_blank>羽毛效果製作教程</a>';
marqueeContent[3]='<A href=http://www.blueidea.com/tech/program/2003/831.asp target=_blank>MySQL&ASP</a>';
marqueeContent[4]='<A href=http://www.blueidea.com/tech/web/2003/874.asp target=_blank>初步瞭解CSS3</a>';
marqueeContent[5]='<A href=http://www.blueidea.com/tech/graph/2003/864.asp target=_blank>Fireworks MX 2004執行面版操作</a>';
marqueeContent[6]='<A href=http://www.blueidea.com/tech/graph/2003/872.asp target=_blank>Fireworks MX 2004等高漸層填充</a>';
marqueeContent[7]='<A href=http://www.blueidea.com/photo/gallery/2003/873.asp target=_blank>瓶子裡的花</a>';
marqueeContent[8]='<A href=http://www.blueidea.com/tech/graph/2003/871.asp target=_blank>Fireworks MX 實現選項卡式效果</a>';
marqueeContent[9]='<A href=http://www.blueidea.com/photo/gallery/2003/870.asp target=_blank>情侶:黑白的愛情空氣</a>';
marqueeContent[10]='<A href=http://www.blueidea.com/tech/graph/2003/866.asp target=_blank>製作 MAC 風格的蘋果標誌</a>';
marqueeContent[11]='<A href=http://www.blueidea.com/tech/graph/2003/868.asp target=_blank>蛋殼製作及破殼而出的人物合成</a>';

var marqueeInterval=new Array(); //定義一些常用而且要經常用到的變數
var marqueeId=0;
var marqueeDelay=4000;
var marqueeHeight=16;

//接下來的是定義一些要使用到的函數
Array.prototype.random=function() {
var a=this;
var l=a.length;
for(var i=0;i<l;i ) {
var r=Math.floor(Math.random()*(l-i));
a=a.slice(0,r).concat(a.slice(r 1)).concat(a[r]);
}
return a;
}
function initMarquee() {
marqueeContent=marqueeContent.random();
var str='';
for(var i=0;i<Math.min(3,marqueeContent.length);i ) str =(i>0?'  ':'') marqueeContent;
document.write('<div id=marqueeBox style="overflow:hidden;height:' marqueeHeight 'px" onmouseover="clearInterval(marqueeInterval[0])" onmouseout="marqueeInterval[0]=setInterval(\'startMarquee()\',marqueeDelay)"><div>' str '</div></div>');
marqueeId =2;
if(marqueeContent.length>3)marqueeInterval[0]=setInterval("startMarquee()",marqueeDelay);
}
function reinitMarquee() {
js_scroll_content.src='scroll_content2.js';
marqueeContent=marqueeContent.random();
var str='';
for(var i=0;i<Math.min(3,marqueeContent.length);i ) str =(i>0?'  ':'') marqueeContent;
marqueeBox.childNodes[(marqueeBox.childNodes.length==1?0:1)].innerHTML=str;
marqueeId=2;
}
function startMarquee() {
var str='';
for(var i=0;(i<3)&&(marqueeId i<marqueeContent.length);i ) {
str =(i>0?'  ':'') marqueeContent[marqueeId i];
}
marqueeId =3;
if(marqueeId>marqueeContent.length)marqueeId=0;

if(marqueeBox.childNodes.length==1) {
var nextLine=document.createElement('DIV');
nextLine.innerHTML=str;
marqueeBox.appendChild(nextLine);
}
else {
marqueeBox.childNodes[0].innerHTML=str;
marqueeBox.appendChild(marqueeBox.childNodes[0]);
marqueeBox.scrollTop=0;
}
clearInterval(marqueeInterval[1]);
marqueeInterval[1]=setInterval("scrollMarquee()",20);
}
function scrollMarquee() {
marqueeBox.scrollTop ;
if(marqueeBox.scrollTop%marqueeHeight==(marqueeHeight-1)){
clearInterval(marqueeInterval[1]);
}
}
initMarquee();
</script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.