HTML实现公告文字滚动效果

效果图:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>滚动文字</title>
</head>
<body style="background: black;padding: 20px;">
<marquee><span style="font-weight: bolder;font-size: 40px;color: white;">你好,山月剧</span></marquee>
</body>
</html>

1、direction 滚动方向属性

        默认情况,文字从右向左滚动,实际应用中,我们可能需要改变方向,就可以通过该属性来设置,该属性可用值有:updownleftright,分别表示向上、向下、向左和向右滚动。
效果图:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>滚动文字</title>
    <style>
        body {
            background: black;
            padding: 20px;
        }

        marquee {
            font-weight: bolder;
            font-size: 40px;
            color: white;
        }
    </style>
</head>
<body style="background: black;padding: 20px;">
<marquee direction="up">嗨</marquee>
<marquee direction="down">你好</marquee>
<marquee direction="left">山月剧</marquee>
<marquee direction="right">明哥</marquee>
</body>
</html>

 

2、behavior 滚动方式属性

        通过behavior 可以设置滚动方式,如往复运动等。behavior可用值有:scrollslidealternate,分别表示循环滚动、只滚动一次就停止和来回交替滚动。
效果图:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>滚动文字</title>
    <style>
        body {
            background: black;
            padding: 20px;
        }

        marquee {
            font-weight: bolder;
            font-size: 40px;
            color: white;
        }
    </style>
</head>
<body style="background: black;padding: 20px;">
<marquee behavior="scroll">嗨</marquee>
<marquee behavior="slide">山月剧</marquee>
<marquee behavior="alternate">明哥</marquee>
</body>
</html>

3、scrolldelay 滚动延迟属性与scrollamount 滚动速度属性
        通过scrolldelay属性可以设置文字滚动的时间间隔。scrolldelay 的时间间隔单位是毫秒,这一时间间隔设置为滚动两步之间的时间间隔,如果时间过长,则会出现走走停停的效果。
scrollamount 用于设置滚动的步长。
效果图:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>滚动文字</title>
    <style>
        body {
            background: black;
            padding: 20px;
        }

        marquee {
            font-weight: bolder;
            font-size: 40px;
            color: white;
        }
    </style>
</head>
<body style="background: black;padding: 20px;">
<marquee scrolldelay="800" scrollamount="100">山月剧</marquee>
</body>
</html>

4、loop 滚动循环属性

        如果我们希望文字滚动几次后停止,就可以使用loop属性

效果图:

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>滚动文字</title>
    <style>
        body {
            background: black;
            padding: 20px;
        }

        marquee {
            font-weight: bolder;
            font-size: 40px;
            color: white;
        }
    </style>
</head>
<body style="background: black;padding: 20px;">
<marquee loop="4">山月剧</marquee>
</body>
</html>

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
可以使用jQuery和CSS来实现公告文字左右滚动。这里提供一个简单的示例代码: HTML: ```html <div class="notice"> <ul> <li>公告1</li> <li>公告2</li> <li>公告3</li> <li>公告4</li> <li>公告5</li> </ul> </div> ``` CSS: ```css .notice { width: 200px; height: 20px; overflow: hidden; } .notice ul { list-style: none; margin: 0; padding: 0; } .notice li { float: left; margin-right: 20px; } ``` JavaScript: ```javascript $(function() { var noticeWidth = $(".notice").width(); var ulWidth = 0; $(".notice li").each(function() { ulWidth += $(this).outerWidth(); }); $(".notice ul").width(ulWidth); function scrollNotice() { var scrollLeft = $(".notice ul").position().left - 1; if (scrollLeft < -ulWidth + noticeWidth) { scrollLeft += ulWidth; } $(".notice ul").css("left", scrollLeft); requestAnimationFrame(scrollNotice); } scrollNotice(); }); ``` 解释一下代码的实现过程: - 首先设置公告容器 `.notice` 的宽度和高度,并设置 `overflow: hidden`,这样公告文字就不会超出容器范围而显示滚动条。 - 然后设置公告列表 `.notice ul` 的样式,将列表项的样式设置为 `float: left`,这样列表项就可以水平排列。 - 在 JavaScript 中,首先计算出公告列表的总宽度,然后将公告列表的宽度设置为总宽度。这样公告列表的宽度就可以自适应文字长度。 - 接着使用 `requestAnimationFrame` 方法循环执行 `scrollNotice` 函数,实现公告文字的左右滚动效果。在 `scrollNotice` 函数中,先获取公告列表的左侧位置,然后将其减去 1,实现向左滚动效果。当公告列表左侧位置小于容器宽度减去列表总宽度时,将其重置为 0,实现循环滚动效果。最后使用 `css` 方法将公告列表的左侧位置设置为滚动后的位置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈老说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值