更新時間:2021-03-22 17:45:56 來源:動力節點 瀏覽1522次
在前端開發的頁面設計當中,由于四象限中可能會有很多任務,可能會超出象限大小,所以需要加上滾動條。HTML滾動條一般是:overflow:auto這個屬性;HTML滾動條標簽是marquee,它是成對出現的標簽,首標簽<marquee>和尾標簽</marquee>之間的內容就是滾動內容。<marquee>標簽的屬性主要有behavior、bgcolor、direction、width、height、hspace、vspace、loop、scrollamount、scrolldelay等,它們都是可選的。
html marquee標簽的direction屬性 :
文字滾動的方向,屬性的參數值有down、left、right、up共四個單一可選值,分別代表滾動方向向下、向左、向右、向上。如下所示:
<marquee direction="right">我向右滾動</marquee>
<marquee direction="right">我向下滾動</marquee>
下面我們來簡單介紹幾種HTML滾動條代碼:
1、向右滾動代碼:
<div?id="colee_right" style="overflow:hidden;width:760px;">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td id="colee_right1" valign="top" align="center">
<table?width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
</tr>
</table>
</td>
<td id="colee_right2" valign="top"></td>
</tr>
</table>
</div>
<script>
var speed=30//速度數值越大速度越慢
//var colee_right2=document.getElementByIdx_x_x("colee_right2");
//var colee_right1=document.getElementByIdx_x_x("colee_right1");
//var colee_right=document.getElementByIdx_x_x("colee_right");
colee_right2.innerHTML=colee_right1.innerHTML
function Marquee4(){
if(colee_right.scrollLeft<=0)
colee_right.scrollLeft+=colee_right2.offsetWidth
else{
colee_right.scrollLeft--
}
}
var MyMar4=setInterval(Marquee4,speed)
colee_right.οnmοuseοver=function() {clearInterval(MyMar4)}
colee_right.οnmοuseοut=function() {MyMar4=setInterval(Marquee4,speed)}
</script>
<!--向右滾動代碼結束-->
2、向左滾動代碼:
<!--下面是向左滾動代碼-->
<div?id="colee_left" style="overflow:hidden; width:760px;">
<table cellpadding="0" cellspacing="0" border="0">
<tr><td id="colee_left1" valign="top" align="center">
<table cellpadding="2" cellspacing="0" border="0">
<tr align="center">
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td id="colee_left2" valign="top"></td>
</tr>
</table>
</div>
<script>
//使用div時,請保證colee_left2與colee_left1是在同一行上.
var speed=30//速度數值越大速度越慢
//var colee_left2=document.getElementByIdx_x("colee_left2");
//var colee_left1=document.getElementByIdx_x("colee_left1");
//var colee_left=document.getElementByIdx_x("colee_left");
colee_left2.innerHTML=colee_left1.innerHTML
function Marquee3(){
if(colee_left2.offsetWidth-colee_left.scrollLeft<=0)//offsetWidth 是對象的可見寬度
colee_left.scrollLeft-=colee_left1.offsetWidth//scrollWidth 是對象的實際內容的寬,不包邊線寬度
else{
colee_left.scrollLeft++
}
}
var MyMar3=setInterval(Marquee3,speed)
colee_left.οnmοuseοver=function() {clearInterval(MyMar3)}
colee_left.οnmοuseοut=function() {MyMar3=setInterval(Marquee3,speed)}
</script>
<!--向左滾動代碼結束-->
3、向上滾動代碼:
<!--向上滾動代碼開始-->
<div id="colee" style="overflow:hidden;height:400px;width:550px;">
<div id="colee1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
</tr>
</table>
</div>
<div id="colee2"></div>
</div>
<script>
var speed=30;
//var colee2=document.getElementByIdx_x("colee2");
//var colee1=document.getElementByIdx_x("colee1");
//var colee=document.getElementByIdx_x("colee");
colee2.innerHTML=colee1.innerHTML; //克隆colee1為colee2
function Marquee1(){
//當滾動至colee1與colee2交界時
if(colee2.offsetTop-colee.scrollTop<=0){
colee.scrollTop-=colee1.offsetHeight; //colee跳到最頂端
}else{
colee.scrollTop++
}
}
var MyMar1=setInterval(Marquee1,speed)//設置定時器
//鼠標移上時清除定時器達到滾動停止的目的
colee.οnmοuseοver=function() {clearInterval(MyMar1)}
//鼠標移開時重設定時器
colee.οnmοuseοut=function(){MyMar1=setInterval(Marquee1,speed)}
</script>
<!--向上滾動代碼結束-->
4、向下滾動代碼:
<div id="colee_bottom" style="overflow:hidden;height:253px;width:410px;">
<div id="colee_bottom1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
<td style="padding-left:10px;"><img src="aa.jpg" /></td>
</tr>
</table>
</div>
<div id="colee_bottom2"></div>
</div>
<script>
var speed=30
var colee_bottom2=document.getElementByIdx_x("colee_bottom2");
var colee_bottom1=document.getElementByIdx_x("colee_bottom1");
var colee_bottom=document.getElementByIdx_x("colee_bottom");
colee_bottom2.innerHTML=colee_bottom1.innerHTML
colee_bottom.scrollTop=colee_bottom.scrollHeight
function Marquee2(){
if(colee_bottom1.offsetTop-colee_bottom.scrollTop>=0)
colee_bottom.scrollTop+=colee_bottom2.offsetHeight
else{
colee_bottom.scrollTop--
}
}
var MyMar2=setInterval(Marquee2,speed)
colee_bottom.οnmοuseοver=function() {clearInterval(MyMar2)}
colee_bottom.οnmοuseοut=function() {MyMar2=setInterval(Marquee2,speed)}
</script>
最后,我們需要注意的是marquee元素的默認寬度與其父元素的寬度相等。如果marquee位于沒有指定寬度的td內,你就需要明確設置marquee的寬度。如果marquee和td的寬度都沒有指定,那么滾動字幕就將限定于1個像素寬。
HTML滾動條算是在使用HTML進行前端開發網站頁面中的基本知識,在本站的HTML教程中也很頻繁的講到,我們如何將HTML滾動條設計的更加合理和規范,從而使網站頁面更加流暢將是我們學習的重點。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習