用jq实现“弹幕”效果

嘛,先上代码,html部分:

<div class="boxDom" id="boxDom">
    <div class="idDom" id="idDom">
        <div class="content">
            <p class="title">吐槽:</p>
            <input type="text" class="text" id="text"/>
            <button type="button" class="btn" id="btn">发射</button>
            <button type="button" class="btn2" id="btn2">清空弹幕</button>
        </div>
    </div>
    <div class="list" id="list">
        <div class="ammo" id="ammo"></div>
    </div>
</div>

样式:

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

        .boxDom {
            width: 100%;
            height: 100%;
            position: relative;
            overflow: hidden;
        }

        .idDom {
            width: 100%;
            height: 100px;
            background: #666;
            position: fixed;
            bottom: 0px;
        }

        .list {
            width: 300px;
            height: 100%;
            background: #888;
            position: fixed;
            right: 0px;
            z-index: 100;
        }

        .ammo {
            position: absolute;
            width: 270px;
            background-color: white;
            left: 0px;
            right: 0px;
            top: 0px;
            bottom: 0px;
            margin: auto;
            border-radius: 5px;
            padding: 5px;
            z-index: 101;
        }

        .content {
            display: inline-block;
            width: 560px;
            height: 40px;
            position: absolute;
            left: 0px;
            right: 0px;
            top: 0px;
            bottom: 0px;
            margin: auto;
        }

        .title {
            display: inline;
            font-size: 4em;
            vertical-align: bottom;
            color: #fff;
        }

        .text {
            border: none;
            width: 300px;
            height: 30px;
            border-radius: 5px;
            font-size: 2.4em;
        }

        .btn {
            width: 60px;
            height: 30px;
            background: #f90000;
            border: none;
            color: #fff;
            font-size: 2.4em;
            border-radius: 5px;
        }

        .btn2 {
            width: 100px;
            height: 30px;
            background: #f90000;
            border: none;
            color: #fff;
            font-size: 2.4em;
            border-radius: 5px;
        }

        span {
            width: 300px;
            height: 40px;
            position: absolute;
            overflow: hidden;
            color: #000;
            font-size: 4em;
            line-height: 1.5em;
            cursor: pointer;
            white-space: nowrap;
        }

功能代码:

    $('#idDom').css('width', $('body').width() - $('#list').width());
    $('#ammo').css('height', $('body').height() - 30);
    var colors = ['red', 'orange', 'cyan', 'blue', 'greenyellow', 'yellow', 'pink', 'purple'];
    $('#btn').click(function () {
        //点击随机生成一个颜色的索引
        var colorIndex = parseInt(Math.random() * colors.length);
        //点击获取随机的高度
        var randomTop = parseInt(Math.random() * 300)
        //获取文本框的值
        //$('#text').val();
        //创建一个span,设置内容
        var $span = $('<span></span>');
        var $div = $('<div></div>');
        $div.text($('#text').val());
        $div.appendTo($('#ammo'));
        //添加到盒子里
        $span.appendTo($('#boxDom'));
        $span.text($('#text').val())
            .css('color', colors[colorIndex])
            .css('top', randomTop)
            .css('left', $('body').width() - 200)
            .css('zIndex', 1)
            .animate({'left': -$span.width()}, (parseInt(Math.random() * 3) + 3) * 1000, function () {
                //到了终点的时候删除
                $(this).remove();
            });
        //清空文本框
        $('#text').val('');
    })
    $('#btn2').click(function () {
        $('#ammo').empty();
    })
    $('#text').keyup(function (e) {
        console.log(e.keyCode);
        if (e.keyCode == 13) {
            $('#btn').click();
        }
    })

简单地说就是单击回车或者按钮时,获取文本框的内容,然后在屏幕外生成,再通过动画移动到另一边的屏幕外,最后清除掉这一条,以节省空间。
用到的知识主要是创建、设置、移除节点,通过keyCode设置点击事件等等,以上。

猜你喜欢

转载自blog.csdn.net/qq_42926749/article/details/81948942
今日推荐