JS實現模仿微博發布效果執行個體代碼_javascript技巧

來源:互聯網
上載者:User

效果:

思路:

利用多功能浮動運動架構實現微博效果,首先,將textarea中的屬性添加到新建立的li裡面然後,再將li添加到ul裡面,再利用浮動運動架構將資料動態顯示出來。

代碼:

複製代碼 代碼如下:

<head runat="server">
    <title></title>
    <style type="text/css">
        *
        {
            margin: 0;
            padding: 0;
        }
        #ul1
        {
            width: 300px;
            height: 300px;
            border: 1px solid black;
            margin: 10px auto;
            overflow: hidden;
        }
        #ul1 li
        {
            list-style: none;
            padding: 4px;
            border-bottom: 1px #999 dashed;
            overflow: hidden;
            opacity: 0;
        }
    </style>
    <script type="text/javascript">
        window.onload = function () {
            var btn = document.getElementById('btn');
            var txt = document.getElementById('t1');
            var oUl = document.getElementById('ul1');
            btn.onclick = function () {
                var cLi = document.createElement('li');
                cLi.innerHTML = txt.value;      //將資料添加到li裡面
                txt.value = '';
                if (oUl.children.length > 0) {      //判斷是否已經有li,如果有那麼就插入,如果沒有那麼就建立
                    oUl.insertBefore(cLi, oUl.children[0]);
                } else {
                    oUl.appendChild(cLi);
                }
                var iHeight = cLi.offsetHeight;     //獲得li的高度
                cLi.style.height = '0';
                move(cLi, { height: iHeight }, function () {        //然後利用浮動運動將資料顯示出來
                    move(cLi, { opacity: 100 });
                });
            }
        }
        //------------------------------------------------------------------------------------
        //擷取非行間樣式
        function getStyle(ojb, name) {
            if (ojb.currentStyle) {
                return ojb.currentStyle[name];
            }
            else {
                return getComputedStyle(ojb, false)[name];
            }
        }
        //緩衝運動json的應用
        //json{attr,finsh}
        //json{width:200,height:200}
        function move(obj, json, fnName) {      //obj是對象,Json是對象的屬性, fnName是函數
            clearInterval(obj.timer);           //關閉之前的計時器
            obj.timer = setInterval(function () {
                var timeStop = true;        //記錄屬性是否都已經執行完成
                for (var attr in json) {        //遍曆json中的資料
                    var oGetStyle = 0;
                    if (attr == 'opacity') {  //判斷透明度
                        oGetStyle = Math.round(parseFloat(getStyle(obj, attr)) * 100);      //透明度取整,然後轉換完後賦值
                    }
                    else {
                        oGetStyle = parseInt(getStyle(obj, attr));
                    }
                    var speed = (json[attr] - oGetStyle) / 5;       //求速度
                    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);  //進位取整
                    if (oGetStyle != json[attr])
                        timeStop = false;
                    if (attr == 'opacity') {    //透明度
                        obj.style.filter = 'alpha(opacity:' + (oGetStyle + speed) + ')';    //給透明度賦值
                        obj.style.opacity = (oGetStyle + speed) / 100;
                    }
                    else {
                        obj.style[attr] = oGetStyle + speed + 'px';     //移動div
                    }
                }
                if (timeStop) {     //如果所有屬性都已經執行完成,那麼就關閉計時器
                    clearInterval(obj.timer);
                    if (fnName) {       //當關閉計時器後要執行的函數
                        fnName();
                    }
                }
            }, 30)
        }
        //------------------------------------------------------------------------------------
    </script>
</head>
<body>
    <textarea id="t1"></textarea>
    <input type="button" id="btn" value="發布" />
    <ul id="ul1">
        <li style="display: none;"></li>
    </ul>
</body>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.