jq彈幕

上代碼

<!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>
<div id="box">
    <div id="main">
        <video width="100%" height="605" mediaplayer="allow" src="123456.mp4" controls="controls"></video>
    </div>
    <div id="bottom">
        <input type="text" id="txt" placeholder="請輸入內容" />
        <input type="button" id="btn" value="發射" />
    </div>
</div>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript">
    var Room = {
        socket: null,
        uid: 0,
        users: [],
        pageH: parseInt($("#main").height()),
        colorArr : ["#cfaf12","#12af01","#981234","#adefsa","#db6be4","#f5264c","#d34a74"],
        init: function(){
            // 創建一個Socket實例
            this.socket = new WebSocket('ws://192.168.222.100:9502');
            // 打開Socket
            this.socket.onopen = function(event) {
                // 監聽消息
                this.socket.onmessage = function(event) {
                    if(event.data.indexOf('server:') === 0){return;}//不知道爲什麼,發送到服務器的時候,也會在這裏收到事件
                    console.log('Message: ', event.data);
                    var data = $.parseJSON(event.data);
                    this.sendMsg(data.msg);
                }.bind(this);

                // 監聽Socket的關閉
                this.socket.onclose = function(event) {
                    $('.alert').show();
                    console.log('Client notified socket has closed', event);
                };
            }.bind(this);


            this.bind();
        },
        sendMsg: function($value){
            $("#main").append("<p>" + $value + "</p>");
            $("#txt").val("");
            var _top = parseInt(this.pageH*(Math.random()));
            var num = parseInt(this.colorArr.length*(Math.random()));
            $("p:last-child").css({"top":_top,"color":this.colorArr[num],"font-size":"20px"});
            $("p:last-child").animate({"left":"-200px"},10000);
            $("p:last-child").stop().animate({"left":"-300px"},10000,"linear",function(){
                $(this).remove();
            });
        },
        bind: function(){
            $('#btn').click(function(){
                var $value = $("#txt").val();
                this.socket.send(JSON.stringify({
                    msg: $value
                }))
            }.bind(this));
        },
    };
    Room.init();
    /*$(function(){
        var pageH = parseInt($("#main").height());
        var colorArr = ["#cfaf12","#12af01","#981234","#adefsa","#db6be4","#f5264c","#d34a74"];
        $("#btn").bind("click",auto);
        function send() {
            var $value = $("#txt").val();

        }
        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();
            });
        };

    })*/
</script>
</body>
</html>

 

發佈了68 篇原創文章 · 獲贊 33 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章