弹幕功能的实现

弹幕功能的实现

在做项目的时候用到了弹幕功能

弹幕的字体大小和颜色也都是随机的,代码奉上

html:     <div class="clearfix tool-switch-container" id="btn-barrage">
                <input id="switch" type="checkbox" class="switch" />
                <label for="switch" id="switch_label">弹幕</label>
           </div>
css:  .tool-switch-container {
		    position: relative;
		    float: left;
		    height: 20px;
		    margin-left: 10px;
		    margin-top: 6px;
		}
		switch {
		    display: none;
		}
		[type="checkbox"], [type="radio"] {
		    box-sizing: border-box;
		    padding: 0;
		}
		.tool-switch-container label {
		    /* margin-left: 122px; */
		    display: block;
		    background-color: #b5b5b5;
		    height: 100%;
		    width: 50px;
		    cursor: pointer;
		    border-radius: 24px;
		    position: relative;
		    font-size: 12px;
		    line-height: 22px;
		    color: #fff;
		    text-indent: 18px;
		    /* border: 1px solid #b5926a; */
		    padding-left: 8px;
		    margin-right: 2px;
		}
		

js:/** switch 弹幕 */
    $("#switch_label").on('click', function () {
        if (qbarrage && qbarrage == '1') {
            $('#switch').removeAttr("disabled"); //去除input元素的disabled属性  
            var check = $('#switch').is(':checked');
            if (!check) {
                $(this).text('弹幕');
                $(this).css("text-indent", "-17px");
            } else {
                $(this).css("text-indent", "17px");
            }
        } else {
            $('#switch').attr("disabled", "disabled") //将input元素设置为disabled  
        }
    })
function randomFontsize(min, max) {//弹幕的字体大小
    var str = "",
        range = min,
        arr = [14, 16, 18, 20, 22, 24, 26, 28];

    // 随机产生 
    range = Math.round(Math.random() * (max - min)) + min;
    for (var i = 0; i < range; i++) {
        var pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
    }
    return str;
}


//获取随机颜色
var getRandomColor = function () {
    return '#' + ('00000' + (Math.random() * 0xffffff << 0).toString(16)).substr(-6);
}
var barrage = function (text) {
    var n = Math.floor(Math.random() * randomNum(1, 2) + 1) - 1;
    var textObj = $('<div class="barrage">' + text + '</div>');
    $("#container").append(textObj);
    moveObj(textObj);
    //setTimeout(barrage,3000);
}



//将传入的参数,也就是那obj,进行移动
var moveObj = function (obj) {
    var showscreen = $("#container"); //显示弹幕的div
    var showHeight = showscreen.height(); //此div的高度
    var showWidth = showscreen.width(); //此div的宽度
    var topMin = showscreen.offset().top; //68  此div距离文档的高度(header的高度)
    var topMax = topMin + showHeight; //整个文档的高度
    obj.css({  
        display: "inline",
        position: "absolute"
    });
    var begin = showWidth - obj.width(); //一开始的起点
    var num = randomNum(1, 1);
    var top = topMin * num;
    if (top > topMax - 120) {
        top = topMin * (1 + randomNum(1, 1) * 0.1);
    }
    var fontSize = randomFontsize(1, 1);  
    obj.css({ 
        left: begin,   
        top: top,
        'font-size': fontSize + 'px',
        color: getRandomColor(),    
        'font-size': 26 + 'px',
        color: '#fff',
        'font-weight': '600',
    });
    var time = 20800 + 10000 * Math.random();
    obj.animate({
        left: "-" + begin + "px"
    }, time, function () {
        obj.remove();
    });
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值