javascript中DOM寫的類似微博發布的效果

來源:互聯網
上載者:User
 代碼如下 複製代碼
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>微博發布</title>
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
h1{margin:20px auto;font-size:30px;width:200px;text-align:center;color:blue;}
#test1{display:block;width:400px;height:70px;margin:20px auto 10px;}
#test2{display:block;margin-left:800px;width:60px;height:30px;font-size:20px;}
#test3{margin:10px auto;border:1px #444 solid;width:400px;min-height:300px;padding-bottom:10px;overflow:hidden;color:blue;}
.test{border:1px blue solid;width:400px;overflow:hidden;}
.time{margin-left:240px;color:#666;font-size:16px;}
.inf{margin:10px 10px;}
.con{margin:10px;min-height:20px;}
</style>
</head>
<body>
    <h1>微博發布</h1>
    <input type="text" id="test1" value="" />
    <input type="button" id="test2" value="發布"/>
    <div id="test3"></div>
<script type="text/javascript">
<!--
    var test1=document.getElementById("test1");
    var test2=document.getElementById("test2");
    var test3=document.getElementById("test3");
    var t,r,i=0;
   
    ///////////////點擊發布的方法
    test2.onclick=move;
    function move(){
        var test=document.createElement('div');                            //建立一個微博框節點
        if(test1.value==""){                                            //如果輸入的內容為空白,重新輸入
            alert("親,請輸入內容哦!");
            return;
        }else{
            test.setAttribute("class","test");                            //test的css樣式
            test.innerHTML ='<p class="con">'+test1.value+'</p>';        //給節點新增內容
            test3.insertBefore(test,test3.firstChild);                    //把建立的節點插入到temp3文檔中,最新發行的放在第一個
            test1.value="";                                                //當發布微博後,輸入框裡的內容消失
        ///////////////微博框運動
            var child=document.getElementById("test3").childNodes;        //擷取所有test3的所有子節點
            var n=-test.offsetHeight;
            function run(){                                                //點擊按鈕時,微博框運動方法
                n++;
                test.style.marginTop=n+"px";
                if(n>=0){n=0;return;clearTimeout(t);}                    ////此處return?????
                t=setTimeout(run,20);
            }run();   
        }
        ////////建立微博中時間和刪除按鈕節點
        var inf=document.createElement('div');                            //建立一個div節點,此div的目的是包含時間和刪除按鈕事件
            inf.setAttribute("class","inf");                            //inf的css樣式
            var d=new Date();                                            //設定時間
            inf.innerHTML ='<span class="time">'+d.getHours()+"時"+d.getMinutes()+"分"+d.getSeconds()+"秒"+'</span>';//綁定時間
            test.appendChild(inf);                                        //把inf節點插入到test中       
        var del=document.createElement('input');                        //建立刪除按鈕節點
            del.type="button";                                            //input的類型為button
            del.value="刪除";
            del.style.float="right";
            inf.appendChild(del);                                        //del節點插入到test中
        /////刪除按鈕事件,刪除的動畫效果
        del.onclick=dele;
        function dele(){   
            //點擊按鈕,微博消失
            var m=test.offsetHeight;                                    //高度的值不能直接賦給變數,所以此處不用test.style.height
            function run2(){
                m--;
                r=setTimeout(run2,20);
                test.style.height=m+"px";
                if(m<=0)
                {m=0;clearTimeout(r);test3.removeChild(test);}            //test3.removeChild(test);//刪除test的內容   
            }run2();
        }
    }
//-->
</script>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.