js模拟marquee的滚动效果

本文介绍了一种通过JavaScript代码实现marquee标签功能的方法,以解决不同浏览器的兼容性问题。提供了HTML和JS代码示例,适用于特定场景需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一些场景下我们需要用到marquee标签制作滚动效果,但是marquee不是标准标签,在浏览器中使用会出现各种兼容性问题。我今天的任务就是要做一个滚动的提示,但是使用marquee的时候在firefox和ie下会出问题,只能自己用js模拟了╮(╯_╰)╭!看代码

这是js代码
     scroll($('#warnMsg')); function scroll(doc){ var parentWidth = doc.parent('div').width(); var docWidth = doc.width(); doc.css('left',parentWidth+'px'); var timer = setInterval(function(){ if(parseInt(doc.css('left'))>-docWidth){ doc.css('left',(parseInt(doc.css('left'))-1)+"px"); }else{ doc.css('left',parentWidth+'px'); } },10); doc.on('mouseover',function(){ clearInterval(timer); }); doc.on('mouseout',function(){ timer = setInterval(function(){ if(parseInt(doc.css('left'))>-docWidth){ doc.css('left',(parseInt(doc.css('left'))-1)+"px"); }else{ doc.css('left',parentWidth+'px'); } },10); }); }
这是html:
<div style="position: absolute;top:27px;left:500px;z-index:777;overflow: hidden;width:500px;" >
    <div style="position: relative;left:500px;white-space: nowrap;width:auto; display:inline-block !important; display:inline;" id="warnMsg">
        滚动内容
    </div>
</div>

当然,我的使用场景不一定和大家的使用场景相同,不过还是有借鉴价值的!

转载于:https://www.cnblogs.com/mjfmei/p/5089493.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值