实现滚动弹幕

内容
1.页面上漂浮字体大小不一、颜色不一,从左向右滚动的弹幕;
2.底部中间有一个发送功能,可以发送新的弹幕;
3.底部的发送部分可以向下收起和弹出。
原理
用倒计时实现弹幕滚动
对标签添加颜色和字幕大小
代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
    <style>
    *{
        margin: 0;
        padding: 0;
        text-decoration: none;
    }
    .top{
        position: relative;
        height: 600px;
        background-color: white;
    }
    .bottom{
        display: block;
        width: 100%;
        height: 240px;
        background-color: rgb(210, 253, 255);
    }
    .one{
        display: block;
        position: absolute;
        right: 500px;
        bottom: 30px;
        width: 40px;
        height: 40px;
        font-size: 14px;
        background-color: rgb(59, 186, 167);
        text-align: center;
        color: black;
        line-height: 40px;
        border-radius: 20%;
    }
    .two{
        display: block;
        position: absolute;
        right: 550px;
        bottom: 30px;
        width: 40px;
        height: 40px;
        font-size: 14px;
        background-color: rgb(39, 117, 106);
        text-align: center;
        color: black;
        line-height: 40px;
        border-radius: 20%;
    }
    span{
        display: inline-block;
        position: absolute;
        left: 0;
    }
    input{
        margin-left: 530px;
        margin-top: 40px;
        width: 300px;
        height: 25px;
        border:1px solid black;
        background-color: rgba(255, 255, 255, 0.871);
        padding-left: 15px;
        border-radius: 10px;
    }
    button{
        width: 55px;
        height: 25px;
        border-radius: 10px;
        border:1px solid black;
        background-color: rgb(171, 235, 226);
    }
    </style>
</head>
<body>
    <div class="top"></div>
    <div class="bottom">
        <input type="text" placeholder="发送弹幕--">
        <button>发送</button>
    </div>
    <a href="#" class="one">收起</a>
    <a href="#" class="two">弹出</a>
    <script>
        window.onload=function(){
        // 创建文字颜色和大小
        var ocolor = ["black","red","orange","yellow","green","blue","purple"]
        var osize = ["14px","16px","18px","20px","22px","24px"]
        var btn = document.querySelector("button")
        var top = document.querySelector(".top")
        var input = document.querySelector("input")
        btn.onclick=function(){
            // 将上面box里放入input输入的内容,设置span标签
            var span = document.createElement("span")
            top.appendChild(span)

            span.innerHTML=input.value
            // 给这个标签一个随机高度和颜色还有大小
            span.style.fontSize = osize[Math.round(Math.random()*7)]
            span.style.color = ocolor[Math.round(Math.random()*11)]
            span.style.top = Math.round(Math.random()*500)+"px"

            // 实现文字从左向右滚动
            // 利用定时器
            var step = 0
            var timer = setInterval(function(){
                step++
                span.style.left =step+"px" 

            // 弹幕到可视区域以外消失
                var out = span.offsetLeft+span.clientWidth
                if(out-window.innerWidth==0){
                    clearInterval(timer)
                    top.removeChild(span)
                }
            }, 5);
        }
        // 设置弹出和收起
        var one = document.querySelector(".one")
        one.onclick=function(){
            var bottom = document.querySelector(".bottom")
            bottom.style.height=90+"px"
            top.style.height=750+"px"
        }
        var two = document.querySelector(".two")
        two.onclick=function(){
            var bottom = document.querySelector(".bottom")
            bottom.style.height=240+"px"
            top.style.height=600+"px"
        }
    }   
    </script>
</body>
</html>

效果

弹幕

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
滚动弹幕是一种在视频或画面上以固定速度连续滚动的文字或图像。对于以Python编写滚动弹幕功能,我们可以使用Pygame库来实现。 首先,我们需要安装Pygame库。可以使用pip install pygame 或者conda install -c cogsci pygame命令来安装。 然后,我们可以创建一个基本的滚动弹幕窗口。首先导入Pygame库,然后初始化Pygame和主窗口。 import pygame import random pygame.init() # 设置窗口大小 WIDTH = 800 HEIGHT = 600 WINDOW = pygame.display.set_mode((WIDTH, HEIGHT)) 接下来,我们可以定义一些弹幕的初始参数,例如文字内容、颜色、速度和位置。 # 定义弹幕参数 text = "这是一个滚动弹幕" color = (255, 255, 255) # 白色 speed = 2 # 弹幕滚动速度 x = WIDTH # 弹幕的起始位置为窗口的最右边 y = random.randint(0, HEIGHT) # 弹幕的起始位置在窗口内随机选择一个y坐标 然后,我们可以在主循环中实现弹幕滚动效果。在每一帧中,将弹幕的x坐标减少速度值,并绘制文本内容。 # 主循环 running = True while running: WINDOW.fill((0, 0, 0)) # 清空窗口 x -= speed # 弹幕左移 font = pygame.font.Font(None, 36) # 设置字体和大小 text_surface = font.render(text, True, color) # 渲染文本 WINDOW.blit(text_surface, (x, y)) # 绘制文本 pygame.display.update() # 更新窗口 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False 最后,我们需要在程序结束时关闭Pygame并退出。 pygame.quit() 这就是使用Python实现滚动弹幕的基本流程。可以根据需要对其进行扩展,例如添加更多的弹幕、改变文字颜色等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值