简单的实现弹幕功能

开发工具与关键技术:Visual Studio 2015  JavaScript
作者:宁佐潮
撰写时间:2019.3.31

在日常追剧中的,许多人都喜欢打开弹幕,因为评论往往才具有灵魂,接下来让我们一起制作一个简单弹幕功能
页面而已和样式十分简洁,不多说,直接上图片
在这里插入图片描述
在这里插入图片描述
主要部分,input用来输入文字,span标签当作按钮来点击发送弹幕,动画danmuon用来实现弹幕从左至右的滑动
然后便是点击发送弹幕触发的事件
在这里插入图片描述
获取到ID,click里一个function,dangmuval是获取到的文字,i是随机的高度,然后cont是将动态添加的弹幕,设置style样式的高度为上面的随机高度,,再加上从左至右的动画效果,内容便是上面获取到用户输入的内容,最后再用append来添加到ul标签的尾部部分,这样便可以实现弹幕的功能了


<body>
    <style>
        li {list-style: none; }
        #dang { height: 40px;text-align: center;padding-top: 650px; }
        #dang input { width: 200px;height: 30px; text-align: left;line-height: 30px;padding-left: 5px; }
        #fan {width: 80px;height: 35px;text-align: center;line-height: 35px;color: #000;display: inline-block;cursor: pointer;}
        .shanpe li { position: absolute;color: #000; height: 30px;line-height: 30px; right: 0px;}
        @keyframes danmuone {
            100% {
                right: 1600px;
            }
        }
    </style>
    <div class="content" id="content">
        <ul class="shanpe" id="shanpe"></ul>
        <form action="/" method="post" id="dang">
            <input type="text" name="name" value="¥ㄟ(´・ᴗ・`)ノ$暴富" id="dangmu" />
            <span id="fan">发送弹幕</span>
        </form>
    </div>
    <script src="jquery-1.12.4/jquery-1.12.4.js"></script>
    <script>
        $("#fan").click(function () {
            var dangmuval = $("#dangmu").val();
            var i = Math.floor(Math.random() * 300);//随机高度
            var cont = '<li style="' + 'top: ' + i + 'px;' + 'animation: danmuone 10s 1 forwards linear;' + '">' + dangmuval + '</li>';
            $("#shanpe").append(cont);
        });
    </script>
</body>
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WebSocket技术可以用来实现实时弹幕功能。以下是一个简单的WebSocket弹幕示例: 1. 前端代码 ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Websocket弹幕</title> <style> #barrage { position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden; pointer-events: none; } .barrage-item { position: absolute; top: 0; white-space: nowrap; font-size: 20px; color: white; text-shadow: 1px 1px 2px black; } </style> </head> <body> <canvas id="barrage"></canvas> <script> const canvas = document.getElementById('barrage'); const ctx = canvas.getContext('2d'); const width = canvas.width = window.innerWidth; const height = canvas.height = window.innerHeight; let barrageList = []; // 连接websocket服务器 const ws = new WebSocket('ws://localhost:8080'); ws.onmessage = (event) => { const data = JSON.parse(event.data); barrageList.push({ text: data.text, x: width, y: Math.random() * height, color: data.color }); }; function draw() { ctx.clearRect(0, 0, width, height); for (let i = 0; i < barrageList.length; i++) { const item = barrageList[i]; ctx.fillStyle = item.color; ctx.fillText(item.text, item.x, item.y); item.x -= 3; } barrageList = barrageList.filter(item => item.x > -ctx.measureText(item.text).width); requestAnimationFrame(draw); } draw(); </script> </body> </html> ``` 2. 后端代码 ```javascript const WebSocket = require('ws'); const wss = new WebSocket.Server({ port: 8080 }); wss.on('connection', (ws) => { console.log('connected'); ws.on('message', (message) => { console.log(`received: ${message}`); wss.clients.forEach(client => { if (client.readyState === WebSocket.OPEN) { client.send(message); } }); }); }); ``` 3. 使用方法 用户在前端页面上输入文本和颜色,点击发送按钮后,将文本和颜色发送给后端WebSocket服务器,服务器接收到消息后将消息广播给所有连接的客户端,客户端接收到消息后将消息添加到弹幕列表中,并在画布上绘制弹幕。 这是一个最基本的弹幕实现,实际中还需要考虑弹幕的速度、字体大小、弹幕屏蔽等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值