首頁 >web前端 >js教程 >jQuery如何實現彈幕? jQuery實作彈幕效果的程式碼

jQuery如何實現彈幕? jQuery實作彈幕效果的程式碼

不言
不言原創
2018-08-07 11:50:481941瀏覽

本篇文章帶給大家的內容是關於jQuery如何實現彈幕? jQuery實現彈幕效果的程式碼,有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。

 jQuery實作彈幕效果的程式碼如下:

<!DOCTYPE html><html>
    <head>
        <meta charset="utf-8" />
        <title>JQ实现弹幕效果</title>
        <style type="text/css">
            *{
                padding: 0;
                margin: 0;
            }
            #box{
                height:700px;
                width:1000px;
                margin: 0 auto;
                border:1px solid #000000;
                position: relative;
            }
            #main{
                width:100%;
                height:605px;
                position: relative;
                overflow: hidden;
            }
            p{
                position: absolute;
                left:1000px;
                width:200px;
                top:0;
            }
            #bottom{
                width:100%;
                height:80px;
                background: #ABCDEF;
                text-align: center;
                padding-top: 15px;
                position: absolute;
                left: 0;
                bottom: 0;
            }
            #txt{
                width:300px;
                height:50px;    
            }
            #btn{
                width:100px;
                height:50px;                
            }
        </style>
    </head>
    <body>
        <p id="box">
            <p id="main">
                
            </p>
            <p id="bottom">
                <input type="text" id="txt" placeholder="请输入内容" />
                <input type="button" id="btn" value="发射" />
            </p>
        </p>
        <script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
        <script type="text/javascript">
            $(function(){                var pageH=parseInt($("#main").height());                var colorArr=["#cfaf12","#12af01","#981234","#adefsa","#db6be4","#f5264c","#d34a74"];                
                $("#btn").bind("click",auto);
                document.onkeydown=function(e){                    if(e.keyCode == 13){
                        auto();
                    }
                };                function auto(){                    var $value = $("#txt").val();
                    $("#main").append("<p>" + $value + "</p>");
                    $("#txt").val("");                    var _top=parseInt(pageH*(Math.random()));                    var num=parseInt(colorArr.length*(Math.random()));
                    $("p:last-child").css({"top":_top,"color":colorArr[num],"font-size":"20px"});
                    $("p:last-child").animate({"left":"-200px"},10000);
                    $("p:last-child").stop().animate({"left":"-300px"},10000,"linear",function(){
                        $(this).remove();
                   });                
                    //console.log($value);                };
                
            })        </script>
    </body></html>

相關文章推薦:

利用JS實作一個可精確到10ms的碼錶的製作(附程式碼)

如何使用原生JavaScript實作輪播圖?程式碼詳解

以上是jQuery如何實現彈幕? jQuery實作彈幕效果的程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn