DIV CSS 佈局教程網

用 jQuery 操作 DOM 模擬微博發布效果
編輯:JQuery特效代碼     

這個示例完整還原了微博發布框的核心交互:輸入內容點擊發布後,新內容以動畫插入列表頂部;頭像懸浮顯示用戶信息卡;每條微博帶時間戳與刪除按鈕;空內容提交則給出閃爍提示。

完整代碼

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>微博發布jq版</title>
<script src="https://code.jquery.com/jquery-1.8.0.min.js" type="text/javascript"></script>
<style type="text/css">
html { color:#000; background:#fff; }
body,div,ul,li,h1,input,button,textarea { padding:0; margin:0; }
img { border:0; }
li { list-style:none; }
h1 { margin:20px auto 0; font-size:30px; width:200px; text-align:center; color:blue; }
#outer { position:relative; width:400px; margin:20px auto 10px; }
#test1 { display:block; width:400px; height:70px; }
.error { background:#f00; width:400px; height:50px; position:absolute; left:1px; top:10px;
         opacity:0.6; filter:alpha(opacity=60); }
#test2 { display:block; margin-left:800px; width:60px; height:30px; font-size:20px; }
.test3 { margin:10px auto; width:400px; }
#test3 { border:1px #444 solid; width:400px; min-height:300px; _height:300px;
         padding-bottom:10px; color:blue; float:left; }
.test { border-bottom:1px blue dotted; width:383px; padding:10px 5px 5px 10px; float:left; }
.inf { margin-top:15px; float:right; color:#555; }
.con { margin-left:16px; display:inline; width:304px; float:left; word-break:break-all; }
.bu { margin-left:6px; display:inline; }
.imgs { width:60px; height:60px; float:left; }
.imgInf { width:120px; background:#f0f; color:#fff; position:absolute; z-index:2;
          left:-65px; top:62px; opacity:0.5; filter:alpha(opacity=50); }
.finish { background:green; width:300px; height:50px; color:#ff0; font-size:30px;
          text-align:center; line-height:50px; position:absolute; left:50px; top:10px;
          opacity:0.6; filter:alpha(opacity=60); }
.imgOut { position:relative; }
</style>
</head>
<body>
<h1>微博發布</h1>
<div id="outer">
<textarea id="test1"></textarea>
</div>
<input type="button" id="test2" value="發布"/>
<div class="test3"><div id="test3"></div></div>

<script type="text/javascript">
$('#test2').click(function() {
    if ($('#test1')[0].value == "") {
        // 內容為空:紅色遮罩閃爍提示
        var finish = $('<div class="error"></div>').appendTo($('#outer'))
            .hide().fadeIn(200).fadeOut(200).fadeIn(200).fadeOut(200);
        return;
    } else {
        $('#test2')[0].disabled = true;
        var timer = new Date();
        // 構造一條新微博並插入列表頂部
        $('<div class="test"><span class="imgOut"><img src="images/wukong.gif" class="imgs"/></span>\
<div class="con"></div>\
<div class="inf">' + timer.getHours() + "時" + timer.getMinutes() + "分" + timer.getSeconds() + '秒\
<input type="button" value="刪除" class="bu"/></div></div>').prependTo($('#test3'));
        $('.con')[0].innerText = $('#test1')[0].value;

        // 頭像懸浮顯示用戶信息
        $('.imgs:eq(0)').hover(
            function() {
                $('<ul class="imgInf"><li>名字:悟空</li><li>稱號:戰鬥聖佛</li>\
<li>現居:花果山</li></ul>').appendTo($(this).parent());
            },
            function() { $('.imgInf').remove(); }
        );

        $('#test1')[0].value = "";

        // 發布成功動畫
        $('<div class="finish">發布成功</div>').appendTo($('#outer'))
            .hide().fadeIn(500).fadeOut(500, function() { $('#test2')[0].disabled = false; });

        // 新微博插入動畫
        $('.test:first').hide().slideDown("slow");

        // 刪除事件
        $('.bu:eq(0)').click(function() {
            if (confirm('確定刪除嗎?')) {
                $(this).parent().parent().hide(1000, function() { $(this).remove(); });
            }
        });
    }
});
</script>
</body>
</html>

關鍵技術點梳理

  • prependTo 實現倒序:新微博要出現在列表最上方,用 prependTo() 而不是 appendTo()。
  • 鏈式動畫組合:hide().fadeIn().fadeOut() 反覆交替,製作出「閃爍警示」的視覺效果,全程只寫一條語句。
  • hover 的雙參數形式:傳入兩個函數分別處理移入與移出,比綁定 mouseover / mouseout 更直觀。信息卡的定位依賴父容器 .imgOut { position:relative; }。
  • innerText 防注入:把用戶輸入寫入 .con 時用了 innerText 而非 html(),天然避免 XSS——這是在真實項目中必須堅持的做法。
  • 回調函數控制按鈕狀態:fadeOut(500, callback) 在動畫結束後才恢復按鈕可用,避免發布動畫期間重複點擊。
  • min-height + _height 雙寫:_height:300px 是 IE6 專屬的下劃線 hack,用來給不支持 min-height 的老 IE 兜底。

整體思路很樸素:所有交互都建立在 DOM 節點的動態增刪與 jQuery 動畫隊列之上,沒有依賴任何額外插件。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved