jQuery实现简单的弹幕效果

21 篇文章 2 订阅

jQuery实现简单的弹幕效果

效果演示

jQuery,HTML实现简单的弹幕效果

HTML代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>弹幕dome</title>
		<link rel="stylesheet" type="text/css" href="css/index.css"/>
		<script type="text/javascript" src="js/jquery-1.11.0.js" ></script>
		<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		
		<div class="bulletbox" id="bulletbox">
			<div class="bulletboxmin" id="bulletboxmin"></div>
			<div class="bulletbot">
				<div class="bulletbotmin">
					<p>吐槽:</p>
					<input type="text" class="text" id="text" placeholder="火力已加满..." />
					<button type="button" class="btn" id="btn" >发布</button>
				</div>
			</div>
			
		</div>
		
		
	</body>
</html>

css代码

html,body{margin: 0px; padding: 0px; width: 100%; height: 100%; font-family: "微软雅黑";font-size: 100%;}

/* 弹幕 */
.bulletbox{width: 100%;height: 100%;position: relative;background-color: #FFFFFF;overflow: hidden;}
.bulletboxmin{width: 100%;box-sizing: border-box;height: calc(100% - 130px);}
/* bulletbot */
.bulletbot{width: 100%;box-sizing: border-box;position: fixed;bottom: 0;left: 0;background-color: #404060;display: flex;display: -webkit-flex;-webkit-justify-content: center;justify-content: center;}
.bulletbotmin{display: flex;display: -webkit-flex;-webkit-align-items: center;align-items: center;height: 130px;}
.bulletbotmin p{font-size: 20px;color: #FFFFFF;margin-right: 10px;}
.bulletbotmin input{display: block;width: 400px;height: 50px;outline: none;background-color: #FFFFFF;border: none;border-top-left-radius: 10px;border-bottom-left-radius: 10px;box-sizing: border-box;padding: 0 20px;font-size: 16px;color: #333333;}
.bulletbotmin button{display: block;width: 140px;height: 50px;background-color: #0099CC;border: none;outline: none;border-top-right-radius: 10px;border-bottom-right-radius: 10px;font-size: 16px;color: #FFFFFF;cursor: pointer;}
.bulletbox span{width: 300px; height: 40px; position: absolute; overflow: hidden;font-size: 20px;color: #AFEEEE; line-height: 40px;white-space: nowrap; cursor: pointer;font-weight: bold;}

jQuery代码

$(function(){
	
	$("#btn").click(function(){
		//评论产生的随机高度
		var pageH=parseInt(Math.random()*300);
		//创建span元素(弹幕条)
		var newSpan=$("<span></span>");
		//获取用户输入的字符串
		var str=$("#text").val();
		//为新元素赋值
		newSpan.text(str);
		//每次发表后清空输入框
		$("#text").val("");
		//设置弹幕出现位置
		newSpan.css("left","1400px");
		newSpan.css("top",pageH);
		//设置弹幕颜色
		newSpan.css("color",randomColor());
		//弹幕动画
		newSpan.animate({"left":-500},10000,"linear",function(){
			$(this).remove();
		})
		//弹幕添加
		newSpan.appendTo("#bulletboxmin")
		
	})
	
	//定义一个随机生成颜色的方法
	function randomColor(){
		var colorArr=['pink','red','blue','puper','orange','black','DarkMagenta','DoderBlue','DeepSkyBlue','CadetBlue','DarkSlateGray','Yellow','Tomato','	RosyBrown'];
		var color=parseInt(Math.random()*colorArr.length);
		return colorArr[color];
	}
	
})
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
视频弹幕效果可以通过 JavaScriptjQuery 实现,具体实现步骤如下: 1. 创建弹幕元素 首先,在视频播放器下方创建一个 div 元素,用于显示弹幕,将其样式设置为绝对定位,宽度为视频播放器的宽度,高度为弹幕字体大小的两倍,背景色为透明。 ```html <div id="danmu-container"></div> ``` ```css #danmu-container { position: absolute; bottom: 0; width: 100%; height: 2em; background-color: transparent; overflow: hidden; } ``` 2. 发送弹幕 使用 jQuery 监听发送按钮的点击事件,获取文本框中的弹幕内容,并动态创建一个 span 元素,设置其样式和文本内容,将其添加到弹幕容器内,设置其初始位置为容器的右侧,使用 animate() 方法实现弹幕的滚动效果。 ```html <input type="text" id="danmu-input"> <button id="danmu-send">发送</button> ``` ```javascript $('#danmu-send').click(function() { var content = $('#danmu-input').val(); if (content) { var $danmu = $('<span>' + content + '</span>'); $danmu.css({ position: 'absolute', top: Math.random() * ($('#danmu-container').height() - 30) + 'px', right: 0, fontSize: '1em', whiteSpace: 'nowrap', color: '#fff' }); $('#danmu-container').append($danmu); $danmu.animate({ right: $('#danmu-container').width() }, 10000, function() { $(this).remove(); }); $('#danmu-input').val(''); } }); ``` 3. 暂停弹幕 使用 jQuery 监听视频播放器的暂停事件,遍历弹幕容器内的所有弹幕元素,使用 stop() 方法停止弹幕的滚动动画,即可实现暂停弹幕效果。 ```javascript $('#video-player').on('pause', function() { $('#danmu-container span').stop(); }); ``` 以上就是使用 JavaScriptjQuery 实现视频弹幕效果的基本步骤,可以根据需要进行功能扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值