Search code examples
javascriptjqueryhtmlfadeinfadeout

Javascript fadeIn fadeOut keeps refading (No JQuery!)


I find JQuery really slow so I decided to create my own fadeIn and fadeOut functions so i can easily call them like jquery does. I'm halfway there as it fully works but unfortunately when I click on fadeIn it keeps refading in and when I click on fadeOut it keeps refading out.

I tried to use the display: none; and display: block styles within javascript but that only works for fading Out. It fully works now on IE6 + 7!!. The issue was inside css. If someone can help with javascript now I would be really appreciative.

here's my code:

HTML:

<html>
    <body>
        <a id="fadeI">FadeIn</a>
        <a id="fadeO">FadeOut</a>
        <div id="message"></div>
    </body>
</html>

CSS:

   <style>
#fadeI, #fadeO{
cursor: pointer;
margin-left: 10px;  
zoom: 1;
    }
#message{
        background: url('pictures/iphone1.png') no-repeat;
        width:90px;
        height: 158px;
        z-index: 1;}

</style>

Javascript:

<script>
    window.onload = function() {
        function fadeOut(id, seconds) {
            var opacity = 1;
            var interval = seconds * 10;
            var outListener = null;
            outListener = setInterval(function() {
                opacity = fadeOutInterval(opacity, id, outListener);
            } , interval);
        }

        function fadeOutInterval(opacity, id, outListener) {
        opacity = opacity - 0.1;
        setOpacity(id, opacity);
        if(opacity < 0) {
            clearInterval(outListener);
               //document.getElementById(id).style.display = 'none';
        }
        return opacity;
        }

        function fadeIn(id, seconds) {
            var opacity = 0;
            var interval = seconds * 10;
            var InListener = null;
            InListener = setInterval(function() {
                opacity = fadeInInterval(opacity, id, InListener);
            } , interval);
        }

        function fadeInInterval(opacity, id, InListener) {
            opacity = opacity + 0.1;
            setOpacity(id, opacity);    
            if(opacity > 1) {
            clearInterval(InListener);
        }   
        return opacity;
        }

        function setOpacity(id, level) {
        document.getElementById(id).style.opacity = level;
            document.getElementById(id).style.MozOpacity = level;
        document.getElementById(id).style.KhtmlOpacity = level;
            document.getElementById(id).style.filter = "alpha(opacity="
                + (level * 100) + ");";
        }

        //delay fadeOut
        setTimeout(delay, 3000);
        function delay(){
            fadeOut('message', 1)
            clearTimeout(delay,1);
            return false;
            }


        //Call functions
        var fadeI = document.getElementById('fadeI');
        var fadO = document.getElementById('fadeO');

        fadeI.onclick = function() {
            fadeIn('message', 1);
        };

        fadeO.onclick = function() {
            fadeOut('message', 1);
        };
    };
</script>

Solution

  • <script>
        window.onload = function() {
    
            var outListener = 0, InListener = 0, opacity = 1;
    
            function fadeOut(id, seconds) {
                if(gid(id).style.display != 'none' || gid(id).style.display == ''){
                    outListener = setInterval(function() {
                        opacity -= 0.1;
                        setOpacity(id);
                        if(opacity < 0) {
                            clearInterval(outListener);
                        }
                    }, seconds);
                }
            }
    
            function fadeIn(id, seconds) {
                if(gid(id).style.display == 'none'){
                    InListener = setInterval(function() {
                        opacity += 0.1;
                        setOpacity(id);
                        if(opacity > 1) {
                            clearInterval(InListener);
                        } 
                    }, seconds);
                }
            }
            function setOpacity(id) {
                document.getElementById(id).style.opacity = opacity;
                document.getElementById(id).style.MozOpacity = opacity;
                document.getElementById(id).style.KhtmlOpacity = opacity;
                document.getElementById(id).style.filter = 'alpha(opacity="'+ (opacity * 100) + '");';
                document.getElementById(id).style.display = (opacity<0) ? 'none' : 'block';
            }
    
            function gid(id){
                return document.getElementById(id);
            }
    
            var fadeI = gid('fadeI');
            var fadO = gid('fadeO');
    
            fadeI.onclick = function() {
                fadeIn('message', 1000);
            };
    
            fadeO.onclick = function() {
                fadeOut('message', 1000);
            };
        };
    </script>