html中如何制作随着屏幕滚动的文字(就是会跟着屏幕走的文字)

html中如何制作随着屏幕滚动的文字(就是会跟着屏幕走的文字),第1张

滚动代码 (从右向左滚动)
<marquee scrollamount=1 scrolldelay=3 valign=middle behavior="scroll">
<img border="0" src=" >

一般是:overflow:auto这个属性;

同时简单介绍几种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="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

</tr>

</table>

</td>

<td id="colee_right2" valign="top"></td>

</tr>

</table>

</div>

<script>

var speed=30//速度数值越大速度越慢

//var colee_right2=documentgetElementByIdx_x_x("colee_right2");

//var colee_right1=documentgetElementByIdx_x_x("colee_right1");

//var colee_right=documentgetElementByIdx_x_x("colee_right");

colee_right2innerHTML=colee_right1innerHTML

function Marquee4(){

if(colee_rightscrollLeft<=0)

colee_rightscrollLeft+=colee_right2offsetWidth

else{

colee_rightscrollLeft--

}

}

var MyMar4=setInterval(Marquee4,speed)

colee_rightonmouseover=function() {clearInterval(MyMar4)}

colee_rightonmouseout=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="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></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=documentgetElementByIdx_x("colee_left2");

//var colee_left1=documentgetElementByIdx_x("colee_left1");

//var colee_left=documentgetElementByIdx_x("colee_left");

colee_left2innerHTML=colee_left1innerHTML

function Marquee3(){

if(colee_left2offsetWidth-colee_leftscrollLeft<=0)//offsetWidth 是对象的可见宽度

colee_leftscrollLeft-=colee_left1offsetWidth//scrollWidth 是对象的实际内容的宽,不包边线宽度

else{

colee_leftscrollLeft++

}

}

var MyMar3=setInterval(Marquee3,speed)

colee_leftonmouseover=function() {clearInterval(MyMar3)}

colee_leftonmouseout=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="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

</tr>

</table>

</div>

<div id="colee2"></div>

</div>

<script>

var speed=30;

//var colee2=documentgetElementByIdx_x("colee2");

//var colee1=documentgetElementByIdx_x("colee1");

//var colee=documentgetElementByIdx_x("colee");

colee2innerHTML=colee1innerHTML; //克隆colee1为colee2

function Marquee1(){

//当滚动至colee1与colee2交界时

if(colee2offsetTop-coleescrollTop<=0){

coleescrollTop-=colee1offsetHeight; //colee跳到最顶端

}else{

coleescrollTop++

}

}

var MyMar1=setInterval(Marquee1,speed)//设置定时器

//鼠标移上时清除定时器达到滚动停止的目的

coleeonmouseover=function() {clearInterval(MyMar1)}

//鼠标移开时重设定时器

coleeonmouseout=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="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

<td style="padding-left:10px;"><img src="aajpg" /></td>

</tr>

</table>

</div>

<div id="colee_bottom2"></div>

</div>

<script>

var speed=30

var colee_bottom2=documentgetElementByIdx_x("colee_bottom2");

var colee_bottom1=documentgetElementByIdx_x("colee_bottom1");

var colee_bottom=documentgetElementByIdx_x("colee_bottom");

colee_bottom2innerHTML=colee_bottom1innerHTML

colee_bottomscrollTop=colee_bottomscrollHeight

function Marquee2(){

if(colee_bottom1offsetTop-colee_bottomscrollTop>=0)

colee_bottomscrollTop+=colee_bottom2offsetHeight

else{

colee_bottomscrollTop--

}

}

var MyMar2=setInterval(Marquee2,speed)

colee_bottomonmouseover=function() {clearInterval(MyMar2)}

colee_bottomonmouseout=function() {MyMar2=setInterval(Marquee2,speed)}

</script>

同时注意还有其他滚动代码的分享:

一、向左滚动:<marquee scrollAmount="2" direction="left" width="滚动区域宽度" height="滚动区域高度" onMouseOver="stop()" onMouseOut="start()">这里放滚动的内容</marquee>

二、向上滚动:<marquee scrollAmount="2" direction="up" width="滚动区域宽度" height="滚动区域高度" onMouseOver="stop()" onMouseOut="start()">这里放滚动的内容</marquee>。

在mounted中创建并执行定时器,然后在beforeDestroy或者destroyed中清除定时器

如果用到了 缓存组件,组件是不会被销毁的,可以使用beforeRouteLeave ,离开路由之前执行的函数

mounted()里面的timer在创建之后就被销毁了,而mothods()里面的thistimer正常情况下应该是undefined,正确写法mounted()改成thistimer=setInterval(thisshow,1000)

JS定时器有两种,分别是setTimeout和setInterval,区别是前者是一次执行,后者是循环执行,具体什么意思呢,直接看代码。
//指定时间为6000毫秒,即6s,到达6s后,执行一次,之后不再执行。
setTimeout(function(){
alert("执行一次定时器");
},6000);
//指定时间为6s,每过6s执行一次
setInterval(function(){
alert("循环执行定时器");
},6000);
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
要关闭定时器,首先我们要先获取到定时器,即如果页面中设置了多个定时器,我们需要知道要关闭那个定时器,直接看代码:
//定义一个变量获取到定时器
var timer = setInterval(function(){
alert("timer是一个数字,即代表了当前定时器");
},6000);
var timer2 = setTimeout(function(){
alert("第二个定时器");
},6000);
//调用方法,关闭指定的定时器
clearInterval(timer);
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
注:定时器启动后,刷新浏览器会清空定时器。有时我们在页面中定义了定时器,在关闭页面时,定时器并不会关闭,我们可以获取定时器,然后在页面的关闭事件中清除定时器。有时我们需要在其他页面对定时器进行关闭,这时可以在对应的页面定义变量,然后在定义定时器页面获取定时器,并赋值给指定页面的变量,这样就可以在指定页面对定时器进行关闭了,需要注意的是,如果我们定时器放到函数中时,在页面初始化时并不能获取到定时器,只有在调用了函数后才会获取到定时器,所以如果要在页面初始化时就获取定时器,不要将定时器放到函数中。这样在页面加载时可以直接加载定时器启动定时器,这样就能拿到定时器了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13379443.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-24
下一篇 2023-07-24

发表评论

登录后才能评论

评论列表(0条)

保存