码迷,mamicode.com
首页 > Web开发 > 详细

原生js实现fadein 和 fadeout

时间:2014-06-29 12:49:31      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   get   

         js里面设置DOM节点透明度的函数属性:filter= "alpha(opacity=" + value+ ")"(兼容ie)和opacity=value/100(兼容FF和GG)。

         先来看看设置透明度的兼容性代码:

bubuko.com,布布扣
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 + ")";
    }
}
bubuko.com,布布扣

关于有的小伙伴这样写:

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

我想说这样在IE10下运行有问题,点了之后没反应。因为IE10支持opacity属性不支持filter了,这个方法不可取。

 

fadein   函数代码:

bubuko.com,布布扣
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);
    }
}
bubuko.com,布布扣

 

fadeout   函数代码:

bubuko.com,布布扣
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);
    }
}
bubuko.com,布布扣

 

 

下面给一个demo示例:

bubuko.com,布布扣
<!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>
bubuko.com,布布扣

有什么更好的实现方式可以留言。。。

 

原生js实现fadein 和 fadeout,布布扣,bubuko.com

原生js实现fadein 和 fadeout

标签:style   blog   http   java   color   get   

原文地址:http://www.cnblogs.com/a546558309/p/3737070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!