jquery实现弹幕效果

这篇博客介绍了如何利用HTML、CSS和jQuery实现弹幕效果。在初次尝试时,作者遇到了弹幕文字始终显示在左侧且无动画的问题。通过对比他人代码,作者发现缺少对span标签的样式设置,修正后成功实现了弹幕随着时间推移从左侧滑动消失的效果。
摘要由CSDN通过智能技术生成

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标签没有给样式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值