js原生實現div漸入漸出

來源:互聯網
上載者:User

標籤:簡單的   document   style   back   fadeout   func   on()   targe   onclick   

jq對漸入漸出進行封裝,簡單的使用連個方法就可以實現。fadeIn(),fadeOut();如果我們介面沒有使用jq那麼原生怎麼實現呢?

我們講解一下,這個原理。當我們要實現漸入的時候,首先是讓隱藏的div慢慢的顯示,通過讓opacity慢慢從 0.0 (完全透明)到 1.0(完全不透明)。漸出就是邏輯反過來的。

下面我們直接貼代碼:

css:

* {margin:0; padding:0}body {font:12px Verdana,Arial; color:#777; background:#222}a {color:#999; text-decoration:none}a:hover {color:#bbb}#wrapper {width:500px; margin:75px auto}#buttons {height:35px}.button {border:1px solid #eee; background:#ccc; border-radius:3px; -moz-border-radius:3px; padding:4px 0 5px; width:245px; text-align:center; cursor:pointer; float:left; color:#555}.button:hover {border:1px solid #fff; background:#d9d9d9; color:#333}.floatright {float:right}#fade {opacity:0; filter:alpha(opacity=‘0‘) border-radius:3px; -moz-border-radius:3px; margin-bottom:10px; padding:9px 10px 0; height:26px; border:1px solid #548954; background:#355c33; color:#79af72; text-shadow:1px 1px #21341d}

html:

<div id="wrapper"><div id="fade">JavaScript</div><div id="buttons"> <div class="button" onclick="fadeEffect.init(‘fade‘, 1)">Fade In</div> <div class="button floatright" onclick="fadeEffect.init(‘fade‘, 0)">Fade Out</div></div></p></div>

js:

var fadeEffect=function(){ return{  init:function(id, flag, target){   this.elem = document.getElementById(id);   clearInterval(this.elem.si);   this.target = target ? target : flag ? 100 : 0;   this.flag = flag || -1;   this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;   this.si = setInterval(function(){fadeEffect.tween()}, 20);  },  tween:function(){   if(this.alpha == this.target){    clearInterval(this.si);   }else{    var value = Math.round(this.alpha + ((this.target - this.alpha) * .05)) + (1 * this.flag);    this.elem.style.opacity = value / 100;    this.elem.style.filter = ‘alpha(opacity=‘ + value + ‘)‘;    this.alpha = value   }  } }}();

 

js原生實現div漸入漸出

相關文章

聯繫我們

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