js實作類別似新浪微博首頁內容漸顯效果的方法,首頁內容漸顯

來源:互聯網
上載者:User

js實作類別似新浪微博首頁內容漸顯效果的方法,首頁內容漸顯

本文執行個體講述了js實作類別似新浪微博首頁內容漸顯效果的方法。分享給大家供大家參考。具體分析如下:

要點一:

if(list_li.length>=1){list.insertBefore(li,list_li[0]);}else{list.appendChild(li);}

從在前面插入新內容,如果沒有新內容,就是在後面插入新內容。

要點二:

var height=li.offsetHeight;li.style.height='0';

取得li的高度,然後再li的高度設定為0,因為高度的變化是從0到現有高度。

要點三:

startrun(li,"height",height,function(){startrun(li,"opacity","100");})

先是高度變化,再是透明度變化

最後,上代碼:

<!DOCTYPE html><html><head><meta charset="gb2312" /><title>無標題文檔</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="發布"><ul id="list"></ul></body></html>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

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