jquery实现弹幕效果

版权声明:转载本文,请附录原文地址,如果错误,请联系我。 https://blog.csdn.net/qq_19880197/article/details/86689136

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>
        </div>
    </div>
</div>

CSS

html,
body{
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    font-size: 62.5%;
    font-family: "微软雅黑";
}
#boxDom{
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}
.idDom{
    width: 100%;
    height: 100px;
    background-color: #666;
    position: fixed;
    bottom: 0;
}
.content{
    display: inline-block;
    width: 430px;
    height: 40px;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto;
}
.title{
    display: inline;
    font-size: 4em;
    color: #fff;
    font-weight: 700;
    vertical-align: bottom;
}
.text{
    border: none;
    width: 300px;
    height: 30px;
    border-radius: 5px;
    font-size: 16px;
}
.btn{
    border: none;
    height: 30px;
    color: red;

}
/*span标签要给样式*/
span {
    width:300px;
    height:40px;
    position:absolute;
    overflow:hidden;
    color:#000;
    font-size:4em;
    line-height:1.5em;
    cursor:pointer;
    white-space:nowrap;
}

jquery

<script src="jQuery/jquery-1.12.3.js"></script>
    <script>
        $(function () {
            // 注册事件
            var colors = ["red", "green", "pink", "deeppink", "yellow", "cyan"];
            $("#btn").click(function () {
                var randomColor = parseInt(Math.random() * colors.length);
                var randomY = parseInt(Math.random() * 400);
                $("<span></span>")//创建span
                    .text($("#text").val())//设置内容
                    .css("color", colors[randomColor])//设置字体颜色
                    .css("left", "2000px")//设置left值
                    .css("top", randomY)//设置Y轴值
                    .animate({left:-500},3000,function () {
                        // 到终端 要删除
                        $(this).remove();
                    }).appendTo("#boxDom");
            });
        });
    </script>

第一次做的时候,没有添加span标签的样式,出现的效果,都是字在最左侧,颜色都可以切换,就是没有动画的效果,等到设定的时间以后,字从左侧消失,检查了很多次代码都没发现是哪的问题,后来在查看别人代码的时候才发现创建的span标签没有给样式。

猜你喜欢

转载自blog.csdn.net/qq_19880197/article/details/86689136