jquery实现弹幕效果

3 篇文章 0 订阅
用js写的一个弹幕

效果图:



源码:

<pre name="code" class="javascript"><!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<!--  
    <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />  
    <link href="favicon.ico" rel="Bookmark" type="image/x-icon" />  
    -->
		<meta name="Generator" content="EditPlus®">
		<meta name="Author" content="">
		<meta name="Keywords" content="">
		<meta name="Description" content="">
		<title>JQuery弹幕</title>
		<link href="" rel="stylesheet" />
		
		</script>
		<style type="text/css">
			body {
				overflow: hidden;
			}
			
			.content {
				overflow: hidden;
			}
			
			.ctxt {
				background: burlywood;
				width: 100%;
				overflow: hidden;
				margin: 0 auto;
				z-index: 9999;
			}
			
			.ctxt p {
				
				left: 95%;
				margin: 0;
				padding: 0;
				z-index: 99;
				overflow: hidden;
			}
			
			#msg{
				height: 24px;
				width: 200px;
			}
			
			#barrage {
				color: gainsboro;
				border: 1px solid aqua;
				font-size: 12px;
				border-radius: 10px;
				float: right;
			}
			
			#style {
				margin-top: 10px;
			}
			
			#publish {
				display: none;
			}
			
			video {
			
				width: 100%;
				overflow: hidden;
				z-index: -99999;
			}
			
			#danmu {
				position: absolute;
				overflow: hidden;
				font-size:20px;
			}
		</style>
	</head>

	<body>
		<div class="content">

			<div id="" class="ctxt">
				<video id="vodio" autoplay="autoplay">
					<source src="video/1429411761ed3dc100c73251.mp4" type="video/mp4">
					</source>

				</video>
			</div>

			<div id="style">
				<button id="barrage"> <font style="color: white;">开始弹幕</font></button>
				<div id="publish">
					<form method="post" align="center">
						<input type="text" id="msg" />
						<button type="button" id="submitBut">发布</button>
					</form>
				</div>

			</div>
		</div>
			<script type="text/javascript" src="js/jquery-2.1.1.min.js" ></script>
		<script type="text/javascript">
			$(document).ready(function() {
				$("#barrage").click(function() {
					$("#publish").toggle();
				});
					
				$("#submitBut").click(function() {

					var msgtxt = $("#msg").val();
				
					var colortxt = getReandomColor();
					var topPos = generateMixed(3);
					
					if(topPos > 500) {
						topPos = 30;
					}
					var newtxt = '<p id="danmu" style="top:' + topPos + 'px; color:' + colortxt + '">' + $("#msg").val() + '</p>';
					$(".ctxt").prepend(newtxt);
					var addTextW = $(".ctxt").find("p").width();
					$(".ctxt p").animate({
						left: '-' + addTextW + 20 + "px"
					}, 30000, function() {
						$(this).hide();
					});
					$("#msg").val(" ");	
				});

			});
			//随机获取颜色值  
			function getReandomColor() {
				return '#' + (function(h) {
					return new Array(7 - h.length).join("0") + h
				})((Math.random() * 0x1000000 << 0).toString(16))
			}

			//生成随机数据。n表示位数    
			var jschars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

			function generateMixed(n) {
				var res = "";
				for(var i = 0; i < n; i++) {
					var id = Math.ceil(Math.random() * 9);
					res += jschars[id];
				}
				return res;
			}
		</script>

	</body>

</html>

 

赶快试试吧
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用jQuery实现DIV弹出效果可以通过以下步骤进行: 1. HTML结构:首先,在HTML中创建一个DIV元素,用于弹出内容的容器。例如: ```html <div id="popup" style="display: none;"> <h2>弹出内容</h2> <p>这是一个弹出窗口的内容。</p> <button id="closeBtn">关闭</button> </div> ``` 2. CSS样式:为弹出容器DIV添加样式,使其以弹出的形式显示在页面上。例如: ```css #popup { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 200px; background-color: #fff; border: 1px solid #ccc; padding: 20px; z-index: 9999; } ``` 3. jQuery代码:使用jQuery实现弹出效果。例如: ```javascript $(document).ready(function() { // 点击触发弹出 $("#openBtn").click(function() { $("#popup").fadeIn(); }); // 点击关闭按钮或弹出外部区域时关闭弹出 $("#closeBtn, #popup").click(function() { $("#popup").fadeOut(); }); // 阻止点击弹出内容区域时冒泡关闭 $("#popup").click(function(event) { event.stopPropagation(); }); }); ``` 在上述代码中,首先使用`$("#openBtn")`来选择一个按钮元素,当点击该按钮时,使用`fadeIn()`方法来使弹出容器淡入显示。使用`$("#closeBtn, #popup")`来选择关闭按钮和弹出容器本身,当点击它们时,使用`fadeOut()`方法来使弹出容器淡出隐藏。 同时,还添加了一个事件处理程序来阻止点击弹出内容区域时冒泡关闭,以确保在弹出内容区域内的点击操作不会关闭弹出容器。 通过以上步骤,你可以使用jQuery实现一个简单的DIV弹出效果。记得在页面中引入jQuery库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值