jq实现发弹幕功能

效果如下:
在这里插入图片描述

html:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>弹幕</title>
	<style>
		.wrap{overflow: hidden;width: 100%;height: 100px;position:  relative;}
		.item{position:absolute;white-space: nowrap;}
	</style>
</head>
<body>
<div class="wrap" id="barrage">
	<div class="item">
		哈哈哈哈
	</div>
	<div class="item">
		下雨了。。
	</div>
</div>	
<div class="send">
	<input type="text" id="iptDm">
	<button>发送</button>
</div>
	
<script src="jquery.js"></script>
<script src="barrage_t.js"></script>
<script>
$('#barrage').barrage()
	
</script>
</body>
</html>

barrager.js文件代码如下:

(function(){
	var Barrage = function(ele,options){
		var defaults = {
			color: ['red','blue','green','black','purple'],
			wrap: ele
		}
		this.setting = $.extend({},defaults,options || {})
		this._init()
		this.bindEven()
	}
	Barrage.prototype = {
		_init:function(){
			var _this = this
			var item = $(this.setting.wrap).find('.item')
			item.css({left:$(window).width()+17})
			for(var i=0;i<item.length;i++){
				item.eq(i).css({
					 top: this.getRandomTop(i%2)+'px',
	                 right: -item.eq(i).width(),
	                 color: this.getRandomColor()
             	});
			}
			this.randomTime(0)
		},
		bindEven:function(){
			var _this = this
			$('.send button').on('click',function(){
				_this._click()
			})
			$('#iptDm').keyup(function(event){
	             if(event.keyCode ==13){
	                $('.send button').trigger('click')
	             }
            });

		},
		randomTime:function(n){
			var obj = $(this.setting.wrap).find('.item');
			var _this = this;
			var len = obj.length;
			if(n>=len){n=0;}
			setTimeout(function(){
				n++;
				_this.randomTime(n);
			},1000);

			var item = obj.eq(n), _w = item.outerWidth(!0);
			item.animate({
				left: -_w
			},_this.getRandomTime(),'linear',function(){
				item.css({left: $(window).width()+17});
				_this.randomTime(n);
			})
		},
		getRandomTop:function(x){
			switch(x){
				case 1:{
					return 4;
					break;
				}
				case 0:{
					return 30;
				}
			}
		},
		getRandomColor:function(){
			var random = Math.floor(Math.random() * this.setting.color.length);
			return this.setting.color[random];
		},
		getRandomTime:function(){
			var random = Math.floor(Math.random()*100000);
			return random;
		},
		_click:function(){
			var hh = $('#iptDm').val()
			if(hh === ''){
				alert('请输入弹幕内容');
				return false;
			}else{
				$('.wrap').append("<div class='item'>" + hh + "</div>");
				this._init();
				$('#iptDm').val('');
			}
		}

	}
		
	$.fn.barrage = function(opt){
		var barg = new Barrage(this,opt);
	}

})(jQuery);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值