js實(shí)現(xiàn)類似新浪微博首頁(yè)內(nèi)容漸顯效果的方法

字號(hào):


    要點(diǎn)一:
    if(list_li.length>=1){
    list.insertbefore(li,list_li[0]);
    }else{
    list.appendchild(li);
    }
    從在前面插入新內(nèi)容,如果沒有新內(nèi)容,就是在后面插入新內(nèi)容。
    要點(diǎn)二:
    var height=li.offsetheight;
    li.style.height='0';
    取得li的高度,然后再li的高度設(shè)置為0,因?yàn)楦叨鹊淖兓菑?到現(xiàn)有高度。
    要點(diǎn)三:
    startrun(li,height,height,function(){
    startrun(li,opacity,100);
    })
    先是高度變化,再是透明度變化
    最后,上代碼:
    <!doctype html>
    <html>
    <head>
    <meta charset=gb2312 />
    <title>無(wú)標(biāo)題文檔</title>
    <style>
    <!--
    body,ul,li{margin:0; padding:0; font:12px/1.5 arial;}
    #list{width:400px; margin:10px auto;}
    #list li{list-style:none; padding:5px 0 ;
    overflow:hidden; border-bottom:1px dotted #ccc;
    filter:alpha(opacity:0); opacity:0; vertical-align:middle;}
    -->
    </style>
    <script>
    <!--
    window.onload = function(){
    var btn = document.getelementbyid(btn);
    var con = document.getelementbyid(con);
    var list = document.getelementbyid(list);
    var list_li = list.getelementsbytagname(li);
    btn.onclick = function(){
    var li = document.createelement(li);
    li.innerhtml = con.value;
    con.value='';
    if(list_li.length>=1){
    list.insertbefore(li,list_li[0]);
    }else{
    list.appendchild(li);
    }
    var height=li.offsetheight;
    li.style.height='0';
    startrun(li,height,height,function(){
    startrun(li,opacity,100);
    })
    }
    }
    function getstyle(obj,name){
    if(obj.currentstyle){
    return obj.currentstyle[name];
    }else{
    return getcomputedstyle(obj,false)[name];
    }
    }
    function startrun(obj,attr,target,fn){
    clearinterval(obj.timer);
    obj.timer = setinterval(function(){
    var cur = 0;
    if(attr == opacity){
    cur = math.round(parsefloat(getstyle(obj,attr))*100);
    }else{
    cur = parseint(getstyle(obj,attr));
    }
    var speed = (target-cur)/8;
    speed = speed>0?math.ceil(speed):math.floor(speed);
    if(cur == target){
    clearinterval(obj.timer);
    if(fn){
    fn();
    }
    }else{
    if(attr == opacity){
    obj.style.filter = alpha(opacity=+(cur+speed)+);
    obj.style.opacity = (cur+speed)/100;
    }else{
    obj.style[attr] = cur + speed + px;
    }
    }
    },30)
    }
    //-->
    </script>
    </head>
    <body>
    <textarea id=con cols=50 rows=5></textarea>
    <input id=btn name= type=button value=發(fā)布>
    <ul id=list>
    </ul>
    </body>
    </html>