js控住DOM實現發佈微博簡單效果

来源:http://www.cnblogs.com/duenyang/archive/2016/08/29/5819468.html
-Advertisement-
Play Games

這段代碼的效果具體是輸入標題和內容,點擊發佈把消息發佈出去,並使最新的消息始終在內容的最上面,代碼為: 這段代碼主要運用了一些DOM節點操作的知識,純屬學習之餘練手作品,大家可以參考參考。 ...


這段代碼的效果具體是輸入標題和內容,點擊發佈把消息發佈出去,並使最新的消息始終在內容的最上面,代碼為:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博消息效果</title>
    <style>
    *{margin:0;padding: 0;}
    .warp{
        width: 600px;
        margin:50px auto 0;
        background-color: #ccc;
    }
    #box{
        width: 600px;
        height: 340px;
        /*background-color: #ccc;*/
        position: relative;
        /*overflow: hidden;*/
        /*margin:50px auto 0;*/
        font-family: '微軟雅黑';
    }
    #box .span1{
        position: absolute;
        font-size: 16px;
        line-height: 16px;
        top: 10px;
        left: 5px;
    }
    #box .span2{
        position: absolute;
        font-size: 16px;
        line-height: 16px;
        top: 50px;
        left: 5px;
    }
    #title{
        position: absolute;
        width: 460px;
        height: 20px;
        line-height: 20px;
        font-size: 16px;
        text-indent: 5px;
        left: 70px;
        top: 6px;
    }
    #text{
        position: absolute;
        width: 460px;
        height: 250px;
        resize: none;
        top: 50px;
        left: 70px;
        text-indent: 5px;
        font-size: 16px;
    }
    #box #prompt{
        position: absolute;
        top: 312px;
        left: 340px;
    }
    #prompt1{
        position: absolute;
        top: 312px;
        left: 340px;
        display: none;
    }
    #send{
        position: absolute;
        height: 25px;
        width: 60px;
        line-height: 20px;
        font-size: 16px;
        top: 310px;
        right: 68px;
    }
    #news{
        list-style: none;
        width: 490px;
        margin:10px auto 0px;
        padding-bottom: 5px;
    }
    #news li{
        width: 490px;
        font-size: 14px;
        overflow: hidden;
        background-color: #fff;
        margin-bottom: 5px;
        position: relative;
    }
    #news li h1{
        font-size: 16px;
        line-height: 20px;
    }
    #news li p{
        text-indent: 5px;
        clear: left;
    }
    #news li span{
        position: absolute;
        top: 0px;
        right: 0px;
        cursor: pointer;
    }
    #news span:hover{
        color: red;
    }
    </style>
</head>
<body>
    <div class="warp">
        <div id="box">
            <span class='span1'>標題:</span>
            <input id="title" type="text">
            <span class="span2">內容:</span>
            <textarea id="text"></textarea>
            <em id="prompt">還可以輸入<var id="textnum">200</var></em>
            <em id="prompt1">你已超出<var id="textnum1"></var></em>
            <button id="send">發送</button>
        </div>
        <ul id="news">
                <li><h1></h1><span></span>
                        <p></p>
                </li>
            </ul>
    </div>
    <script>
        var title=document.getElementById('title');
        var text=document.getElementById('text');
        var send=document.getElementById('send');
        var ul=document.getElementById('news');
        var lis=ul.getElementsByTagName('li');
        var prompt=document.getElementById('prompt');
        var prompt1=document.getElementById('prompt1');
        var textnum=document.getElementById('textnum');
        var textnum1=document.getElementById('textnum1');
        var timer1=null,timer2=null;
        send.onclick=function(){
            if (text.value==''||title.value=='') {
                alert('親~標題或內容不能為空');return false;
            }
            lis[0].innerHTML='<h1>'+title.value+'</h1><span>×</span><p>'+text.value+'</p>';
            lis[0].children[1].setAttribute('id','close');
            var newLi=document.createElement('li');
            ul.insertBefore(newLi,lis[0]);
            maxheight=lis[1].clientHeight;
            lis[1].style.height=0+'px';
            var x=0;
            var minstep=0;
            var maxstep=20;
            var change=maxheight/maxstep;
            clearInterval(timer1);
            timer1=setInterval(function(){
                minstep++;
                if (minstep>=maxstep) {
                    clearInterval(timer1);
                }
                x+=change;
                lis[1].style.height=x+'px';
            },10)
            title.value='';
            text.value='';
            var close=document.getElementById('close');
             for (var i = 0; i < lis.length; i++) {
                 close.onclick=function(){
                     var isme=this.parentNode;
                     var x=this.parentNode.clientHeight;
                    var minstep=0;
                    var maxstep=20;
                    var change=x/maxstep;
                    clearInterval(timer1);
                    timer1=setInterval(function(){
                        minstep++;
                        if (minstep>=maxstep) {
                            clearInterval(timer1);
                            ul.removeChild(isme);
                        }
                        x-=change;  
                        isme.style.height=x+'px';
                    },10)
                   // ul.removeChild(lis[i]);//不可以,不知道綁定的是第幾個。
                 }
             }
         }
        text.onfocus=function(){
            // console.log(prompt.children[0].innerHTML);//children是指帶有標簽的子節點;
            timer2=setInterval(function(){
                if(text.value.length<190){
                    var num=200-text.value.length;
                    textnum.style.color='black';
                    // prompt.style.color='black';
                    textnum.innerHTML=num;//
                    // prompt.innerHTML='還可以輸入<var id="textnum">'+num+'</var>字</em>';
                }
                if (text.value.length>=190&&text.value.length<=200){
                    var num=200-text.value.length;
                    // prompt.style.color='black';
                    textnum.style.color='red';//為什麼不變紅呢?因為這他妹的也是一個未來事件!
                    // prompt.innerHTML='還可以輸入<var id="textnum">'+num+'</var>字</em>';
                    textnum.innerHTML=num;
                }
                if (text.value.length>200){
                    var num=text.value.length-200;
                    // prompt.style.color='red';
                    prompt.style.display='none';
                    prompt1.style.display='block';
                    textnum1.style.color='red';
                    textnum1.innerHTML=num;
                   
                }
                // console.log(text.value.length);
            },50)    

        }
        text.onblur=function(){
            clearInterval(timer2);
        }
    </script>
</body>
</html>

這段代碼主要運用了一些DOM節點操作的知識,純屬學習之餘練手作品,大家可以參考參考。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • (原文來自:http://www.jb51.net/shouce/perl.htm) ...
  • URL中參數的值為中文時,servlet接收後顯示為亂碼,如下圖: 這時候需要修改tomcat的中的server.xml文件。該文件路徑為 tomcat安裝目錄下的conf文件夾。 為修改前的server.xml文件。 修改後的server.xml文件。增加了一行: URIEncoding="UTF ...
  • 一、CSS DIV網頁佈局中當解析度小於等於1024px(像素)時,DIV佈局對象顯示1000px寬度,當解析度大於1024px時候顯示1200px寬度等需求。使用CSS實現改變瀏覽器顯示寬度從而實現佈局的網頁寬度動態改變變化(網頁寬度自動隨瀏覽器顯示寬度而變寬變窄)。隨著發展,越來越多的電腦用戶顯 ...
  • github地址:https://github.com/lily1010/sass/tree/master/course03 用到的sass語法是: sass --watch test.scss:test.css --style expanded 如下圖: 1 導入外部文件,預設文件尾碼預設是sas ...
  • ...
  • 可以使用js來實現簡單的碰壁反彈效果,具體的內容請大家參考代碼部分。 其中movex和movey兩個變數是判斷運動的方向。 ...
  • 一,定義和用法 所有瀏覽器都支持 <a> 標簽。 <a> 標簽定義超鏈接,用於從一張頁面鏈接到另一張頁面。 <a> 元素最重要的屬性是 href 屬性,它指示鏈接的目標。 在所有瀏覽器中,鏈接的預設外觀是: 未被訪問的鏈接帶有下劃線而且是藍色的 已被訪問的鏈接帶有下劃線而且是紫色的 活動鏈接帶有下劃 ...
  • 一些簡單的字元串操作,歡迎大家參考學習。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...