更新時間:2021-03-22 17:45:56 來源:動力節(jié)點(diǎn) 瀏覽1526次
在前端開發(fā)的頁面設(shè)計(jì)當(dāng)中,由于四象限中可能會有很多任務(wù),可能會超出象限大小,所以需要加上滾動條。HTML滾動條一般是:overflow:auto這個屬性;HTML滾動條標(biāo)簽是marquee,它是成對出現(xiàn)的標(biāo)簽,首標(biāo)簽<marquee>和尾標(biāo)簽</marquee>之間的內(nèi)容就是滾動內(nèi)容。<marquee>標(biāo)簽的屬性主要有behavior、bgcolor、direction、width、height、hspace、vspace、loop、scrollamount、scrolldelay等,它們都是可選的。
html marquee標(biāo)簽的direction屬性 :
文字滾動的方向,屬性的參數(shù)值有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//速度數(shù)值越大速度越慢
//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>
<!--向右滾動代碼結(jié)束-->
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//速度數(shù)值越大速度越慢
//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 是對象的實(shí)際內(nèi)容的寬,不包邊線寬度
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>
<!--向左滾動代碼結(jié)束-->
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(){
//當(dāng)滾動至colee1與colee2交界時
if(colee2.offsetTop-colee.scrollTop<=0){
colee.scrollTop-=colee1.offsetHeight; //colee跳到最頂端
}else{
colee.scrollTop++
}
}
var MyMar1=setInterval(Marquee1,speed)//設(shè)置定時器
//鼠標(biāo)移上時清除定時器達(dá)到滾動停止的目的
colee.οnmοuseοver=function() {clearInterval(MyMar1)}
//鼠標(biāo)移開時重設(shè)定時器
colee.οnmοuseοut=function(){MyMar1=setInterval(Marquee1,speed)}
</script>
<!--向上滾動代碼結(jié)束-->
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元素的默認(rèn)寬度與其父元素的寬度相等。如果marquee位于沒有指定寬度的td內(nèi),你就需要明確設(shè)置marquee的寬度。如果marquee和td的寬度都沒有指定,那么滾動字幕就將限定于1個像素寬。
HTML滾動條算是在使用HTML進(jìn)行前端開發(fā)網(wǎng)站頁面中的基本知識,在本站的HTML教程中也很頻繁的講到,我們?nèi)绾螌TML滾動條設(shè)計(jì)的更加合理和規(guī)范,從而使網(wǎng)站頁面更加流暢將是我們學(xué)習(xí)的重點(diǎn)。
初級 202925
初級 203221
初級 202629
初級 203743