原生js實現fadein 和 fadeout

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   get   

         js裡面設定DOM節點透明度的函數屬性:filter= "alpha(opacity=" + value+ ")"(相容ie)和opacity=value/100(相容FF和GG)。

         先來看看設定透明度的相容性代碼:

function setOpacity(ele, opacity) {    if (ele.style.opacity != undefined) {        ///相容FF和GG和新版本IE        ele.style.opacity = opacity / 100;    } else {        ///相容老版本ie        ele.style.filter = "alpha(opacity=" + opacity + ")";    }}

關於有的小夥伴這樣寫:

function setOpacity(ele, opacity) {    if (document.all) {        ///相容ie        ele.style.filter = "alpha(opacity=" + opacity + ")";    }    ele {        ///相容FF和GG        ele.style.opacity = opacity / 100;    }}

我想說這樣在IE10下運行有問題,點了之後沒反應。因為IE10支援opacity屬性不支援filter了,這個方法不可取。

 

fadein   函數代碼:

function fadein(ele, opacity, speed) {    if (ele) {        var v = ele.style.filter.replace("alpha(opacity=", "").replace(")", "") || ele.style.opacity;        v < 1 && (v = v * 100);        var count = speed / 1000;        var avg = count < 2 ? (opacity / count) : (opacity / count - 1);        var timer = null;        timer = setInterval(function() {            if (v < opacity) {                v += avg;                setOpacity(ele, v);            } else {                clearInterval(timer);            }        }, 500);    }}

 

fadeout   函數代碼:

function fadeout(ele, opacity, speed) {    if (ele) {        var v = ele.style.filter.replace("alpha(opacity=", "").replace(")", "") || ele.style.opacity || 100;        v < 1 && (v = v * 100);        var count = speed / 1000;        var avg = (100 - opacity) / count;        var timer = null;        timer = setInterval(function() {            if (v - avg > opacity) {                v -= avg;                setOpacity(ele, v);            } else {                clearInterval(timer);            }        }, 500);    }}

 

 

下面給一個demo樣本:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title><script type="text/javascript" src="fade.js"></script>    <script type="text/javascript">                 window.onload = function () {            document.getElementById(‘Button1‘).onclick = function () {                fadeout(document.getElementById(‘DV‘), 0, 6000);            }            document.getElementById(‘Button2‘).onclick = function () {                fadein(document.getElementById(‘DV‘), 80, 6000);            }        }    </script></head><body>    <div id="DV" style="background-color: green; width: 400px; height: 400px;"></div>    <input id="Button1" type="button" value="button" />    <input id="Button2" type="button" value="button" /></body></html>

有什麼更好的實現方式可以留言。。。

 

聯繫我們

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