html網頁內容自動滾動實現跑馬燈效果

1、效果1

超出div高度,文本自動滾動(像跑馬燈一樣從下往上滾動)

一個div裏面有文字
1、如果文字沒超出div高度:文字不用滾動
2、如果文字太多超出了div高度,那些文字就自動滾動
ps:滾動的效果就像我這個設置一樣

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>文字內容超長,自動滾屏(跑馬燈效果)</title>
		<script src="js/jquery-1.9.1.min.js"></script>
		<style>
		div.marquee{
			overflow:hidden;
			background:#ccc;
			width:400px;
			height:140px;
			line-height:30px;
		}
		</style>
		
	</head>
	<body>
	<script>
		var s = '<div class="marquee">';
		for(var i = 0; i < 10; i++){
			s += i + '該系統於2017-06-21進行維護,望相互告知!!!<br />';
			
		} 
		s += '</div>';
		document.write(s);
	</script><br />
    <div class="marquee">該系統於2017-06-21進行維護,望相互告知!!!</div>
	<script>
	    $('div.marquee').each(function () {
	        if (this.scrollHeight > this.offsetHeight) $(this).html('<marquee behavior="scroll" direction="up" scrollamount="1" height="140" οnmοuseοver="this.stop()" οnmοuseοut="this.start()">'+this.innerHTML+'</marquee>');
	    });
	</script>
	</body>
</html>

 

2、效果2

js文字跑馬燈

實現文字跑馬燈效果,主要控制scrollLeft.

效果圖如下

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>文字內容超長,自動滾屏(跑馬燈效果)</title>
		<script src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var position=$("div").scrollLeft();
var t;
var flag=true;
$(document).ready(function(){
  $("button").click(function(){
      if(flag){
        position=0;
        fun();
        $(".btn1").text("重置跑馬燈");
      }else{
        $(".btn1").text("開始跑馬燈")
        clearTimeout(t); 
        $("div").scrollLeft(0);
      }
      flag=!flag;
      
  });
});
function fun(){
    if(position>400){
        position=0;
    }
    $("div").scrollLeft(position++);
    t=setTimeout("fun()",30);
}
</script>
</head>
<body>
<div id="testDiv" style="width:300px;overflow:hidden;white-space:nowrap;position:relative;">
The longest word in the english dictionary is: pneumonoultramicroscopicsilicovolcanoconiosis.
</div>
<button class="btn1">開始跑馬燈</button>
</body>
</html>

 

3、HTML之marquee(文字滾動)詳解

語法:

<marquee></marquee>

以下是一個最簡單的例子:

代碼如下:

<marquee><font size=+3 color=red>Hello, World</font></marquee>

下面這兩個事件經常用到:

onMouseOut="this.start()" :用來設置鼠標移出該區域時繼續滾動

onMouseOver="this.stop()":用來設置鼠標移入該區域時停止滾動

代碼如下:

<marquee onMouseOut="this.start()" onMouseOver="this.stop()">onMouseOut="this.start()" :用來設置鼠標移出該區域時繼續滾動 onMouseOver="this.stop()":用來設置鼠標移入該區域時停止滾動</marquee>

這是一個完整的例子:

代碼如下:

<marquee id="affiche" align="left" behavior="scroll" bgcolor="#FF0000" direction="up" height="300" width="200" hspace="50" vspace="20" loop="-1" scrollamount="10" scrolldelay="100" onMouseOut="this.start()" onMouseOver="this.stop()">

這是一個完整的例子

</marquee>

該標籤支持的屬性多達11個:

align

設定<marquee>標籤內容的對齊方式

absbottom:絕對底部對齊(與g、p等字母的最下端對齊)

absmiddle:絕對中央對齊

baseline:底線對齊

bottom:底部對齊(默認)

left:左對齊

middle:中間對齊

right:右對齊

texttop:頂線對齊

top:頂部對齊

代碼如下:

<marquee align="absbottom">align="absbottom":絕對底部對齊(與g、p等字母的最下端對齊)。 </marquee>

<marquee align="absmiddle">align="absmiddle": 絕對中央對齊。 </marquee>

<marquee align="baseline">align="baseline": 底線對齊。 </marquee>

<marquee align="bottom">align="bottom": 底部對齊(默認)。 </marquee>

<marquee align="left">align="left": 左對齊。 </marquee>

<marquee align="middle">align="middle": 中間對齊。 </marquee>

<marquee align="right">align="right": 右對齊。 </marquee>

<marquee align="texttop">align="texttop": 頂線對齊。 </marquee>

<marquee align="top">align="top": 頂部對齊。 </marquee>

behavior

設定滾動的方式:

alternate: 表示在兩端之間來回滾動。

scroll: 表示由一端滾動到另一端,會重複。

slide:      表示由一端滾動到另一端,不會重複。

代碼如下:

<marquee behavior="alternate">alternate:表示在兩端之間來回滾動。 </marquee>

<marquee behavior="scroll">scroll:表示由一端滾動到另一端,會重複。</marquee>

<marquee behavior="slide">slide:      表示由一端滾動到另一端,不會重複。</marquee>

bgcolor

設定活動字幕的背景顏色,背景顏色可用RGB、16進制值的格式或顏色名稱來設定。

代碼如下:

<marquee bgcolor="#006699">設定活動字幕的背景顏色 bgcolor="#006699"</marquee>

<marquee bgcolor="RGB(10%,50%,100%,)">設定活動字幕的背景顏色 bgcolor="rgb(10%,50%,100%,)"</marquee>

<marquee bgcolor="red">設定活動字幕的背景顏色 bgcolor="red"</marquee>

direction

設定活動字幕的滾動方向

代碼如下:

<marquee direction="down">設定活動字幕的滾動方向direction="down":向下</marquee>

<marquee direction="left">設定活動字幕的滾動方向direction="left":向左</marquee>

<marquee direction="right">設定活動字幕的滾動方向direction="right":向右</marquee>

<marquee direction="up">設定活動字幕的滾動方向direction="up":向上</marquee>

height

設定活動字幕的高度

代碼如下:

<marquee height="500" direction="down" bgcolor="#CCCCCC">設定活動字幕的高度height="500"</marquee>

width

設定活動字幕的寬度

代碼如下:

<marquee width="500" bgcolor="#CCCCCC">設定活動字幕的寬度width="500"</marquee>

hspace

設定活動字幕裏所在的位置距離父容器水平邊框的距離

This controls the horizontal(水平)space around the display box.

代碼如下:

      <table width="500" border="1" align="center" cellpadding="0" cellspacing="0">

        <tr>

          <td><marquee hspace="100" bgcolor="#CCCCCC">hspace="100"</marquee></td>

        </tr>

      </table>

vspace

設定活動字幕裏所在的位置距離父容器垂直邊框的距離

This controls the vertical(垂直) space around the display box.

代碼如下:

<marquee vspace="100" bgcolor="#CCCCCC">hspace="100"</marquee>

loop

設定滾動的次數,當loop=-1表示一直滾動下去,默認爲-1

代碼如下:

<marquee loop="-1" bgcolor="#CCCCCC">我會不停地走。</marquee>

<p>&nbsp;</p>

<marquee loop="2" bgcolor="#CCCCCC">我只走兩次哦</marquee>

scrollamount

設定活動字幕的滾動速度,單位pixels

代碼如下:

<marquee scrollamount="10" >scrollamount="10" </marquee>

<marquee scrollamount="20" >scrollamount="20" </marquee>

<marquee scrollamount="30" >scrollamount="30" </marquee>

scrolldelay

設定活動字幕滾動兩次之間的延遲時間,單位millisecond(毫秒)

值大了會有一步一停頓的效果

代碼如下:

<marquee scrolldelay="10" >scrolldelay="10" </marquee>

<marquee scrolldelay="100" > scrolldelay="100"</marquee>

<marquee scrolldelay="1000">scrolldelay="1000" </marquee>

 

<marquee> ... </marquee>

移動屬性的設置 ,這種移動不僅僅侷限於文字,也可以應用於圖片,表格等等

鼠標屬性

onMouseOut=this.start() ........鼠標移出狀態滾動

onMouseOver=this.stop() .........鼠標經過時停止滾動

方向

<direction=#> #=left, right ,up ,down <marquee direction=left>從右向左移!</marquee>

方式

<bihavior=#> #=scroll, slide, alternate <marquee behavior=scroll>一圈一圈繞着走!</marquee>

<marquee behavior=slide>只走一次就歇了!</marquee>

<marquee behavior=alternate>來回走</marquee>

循環

<loop=#> #=次數;若未指定則循環不止(infinite) <marquee loop=3 width=50% behavior=scroll>只走 3 趟</marquee>

<marquee loop=3 width=50% behavior=slide>只走 3 趟</marquee>

<marquee loop=3 width=50% behavior=alternate>只走 3 趟!</marquee>

速度

<scrollamount=#> <marquee scrollamount=20>啦啦啦,我走得好快喲!</marquee>

延時

<scrolldelay=#> <marquee scrolldelay=500 scrollamount=100>啦啦啦,我走一步,停一停!</marquee>

外觀(Layout)設置

對齊方式(Align)

<align=#> #=top, middle, bottom <font size=6>

<marquee align=# width=400>啦啦啦,我會移動耶!</marquee>

</font>

底色

<bgcolor=#> #=rrggbb 16 進制數碼,或者是下列預定義色彩:

Black, Olive, Teal, Red, Blue, Maroon, Navy, Gray, Lime,

Fuchsia, White, Green, Purple, Silver, Yellow, Aqua <marquee bgcolor=aaaaee>顏色!</marquee>

面積

<height=# width=#> <marquee height=40 width=50% bgcolor=aaeeaa>面積!</marquee>

空白

(Margins)<hspace=# vspace=#>

<marquee hspace=20 vspace=20 width=150 bgcolor=ffaaaa align=middle>面積!</marquee>

</P>

<marquee id="iescroller" direction=left height=10 onMouseOut=start(); onMouseOver=stop(); scrollamount=2 scrolldelay=10 scrollleft="0" scrolltop="0" behavior="alternate" bgcolor="#999999" style="color: #ffffff; font-size: 14; font-family: '宋體', 'Arial','Helvetica', 'sans-serif'"title=文字內容> 這是放文字或需要移動的圖片(光標放在這裏時用INSERT命令就可以插入圖片)</marquee> </P>

 

 

marquee的滾動屬性參數 

 

     從<marquee>開始到</marquee>結束,其中有很多參數,其實,朋友們還是應用得很多了,讓圖片滾動起來,也是經常經常應用的,下面對這個網頁參數的屬性做一些簡單的描述;

滾動參數:

1:方向:DIRECTION

left---左(默認)

right---右

up------上

down----下

2:方式:BEHAVIOR

SCROLL -------環繞滾動(默認)

SLIDE---------滾動一次

ALTERNATE-----來回滾動

3:次數:LOOP

當LOOP=-1或LOOP=INFINITE時,則表明文字滾動是無限循環(默認)

4:速度:SCROLLDELAY

任意自然整數

5:對齊:ALIGN

TOP---------對齊上方

MIDDLE------對齊中部

BOTTOM------對齊下方

6:鼠標的劃過與離開

onMouseOver=this.stop(); onMouseOut=this.start();

劃過停止滾動。離開,繼續滾動

有了以上參數。我們就很容易製作出一個logo圖片的任意滾動方式,例如:

<marquee width=120 height=200 DIRECTION=up BEHAVIOR=ALTERNATE SCROLLDELAY=120 ALIGN=MIDDLE onMouseOver=this.stop(); onMouseOut=this.start();><a href=http://qjpz.com><img src=http://qjpz.com/bbs/images/logo.gif border=0><marquee>

很容易對照出。這是一個滾動速度爲120MM,從下到上碰壁即返回並對齊中間,鼠標劃過圖標即停止,點擊圖標進入〈千嬌論壇〉的一個來回滾動的代碼。

在背景圖片上做滾動字幕

<TABLE width="500" border=0>

<TBODY>

<TR>

<TD background=背景圖片地址 height=250>

<P><MARQUEE scrollAmount=2 scrollDelay=50 direction=up width=200 height=200 behavior=scroll>要滾動的文字</MARQUEE></P></TD></TR></TBODY></TABLE></DIV>

參數設置:

a)scrollAmount表示速度,值越大速度越快。如果沒有它,默認爲6,建議設爲1~3比較好

b)scrollDelay也是用來控制速度的,表示走走停停,默認爲90,值越大,速度越慢。通常scrollDelay是不需要設置的。

c)direction很明顯是表示滾動的方向,默認爲從右向左:←,因此如果是向左滾動的話不需要次參數。其他可選的值還有right,down,up。滾動方向分別爲:right表示→,up表示↑,down表示↓。

d)width和height用來表示滾動區域的大小,width是寬度,height是高度。特別是在做垂直滾動的時候,一定要設height的值。

e)behavior是來控制滾動屬性的,默認爲循環滾動(scroll),同樣,如果是循環滾動的話可以不需要此參數。其他可選的值還有alternate(交替滾動),slide(幻燈片效果,指的是滾動一次,然後停止滾動)。

f)每行字的前後<FONT color=#990066 size=4 face=隸書>和</FONT>用定義每行字的顏色,大小和字體,如果哪項不需要的話,把代碼去掉就行。

圖片滾動
用<img src=相對路徑/文件名>的語句。並且要注意圖片名不要中文,要注意區分英文大小寫。

圖片做超鏈接
用<a href=>和</a>把<img>包圍,並且img必須設border=0,否則圖片會出現藍框。

正確的例子如:

<a href=http://www.sina.com.cn/><img src=../../j/01.jpg border=0></a>
其中a href=表示超鏈接,這是最常用的。練習的方法也很簡單,就是平時用FP或DW做網頁的時候,要多查看源代碼。

多張圖片排列滾動
通常圖片和圖片之間用<br>(回行)或<p style=margin-top:9>(精確調整圖片間的距離)來鏈接。也可以把你的圖片先用表格排版,然後把這個表格的所有語句也加入到marquee中,讓這個表格來滾動。
代碼如:

<script>document.write('<marquee scrollAmount=2 width=340 height=160 direction=up οnmοuseοver=stop() οnmοuseοut=start()><a href=http://www.sina.com.cn/><img src=../../j/01.jpg border=0></a></marquee>')</script> 

 

 

參考資料

https://ask.csdn.net/questions/391201?sort=id
https://www.cnblogs.com/lidedong/p/9667512.html

https://www.cnblogs.com/smiler/p/4892918.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章