Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fading out a whole page with jquery

So, I'm attempting to fade the page out when a user goes to another section of my website. After reading a bit on stack overflow, I have written the following code. But it just seems messy/ugly. Is there a proper way to fade out a webpage, or is it all hacky? It seems (at the moment) like my page is getting dumped before it has the chance to fade.

<link rel="stylesheet" href="http://www.catlard.com/styles/body.css" type="text/css"/>
<link rel="icon" href="http://www.catlard.com/caticon.ico" />
<link rel="shortcut icon" href="http://www.catlard.com/caticon.ico?v=2" />
<script src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"> </script>
<script type="text/javascript">
    $(document).ready( function(){
        $("html").hide();
        $("html").delay(250).fadeIn();  
        $(window).unload(function () {
            $("html").fadeOut();
        });
    });
</script>
like image 731
Catlard Avatar asked Oct 16 '13 07:10

Catlard


People also ask

How do you fadeOut in jQuery?

The jQuery fadeToggle() method toggles between the fadeIn() and fadeOut() methods. If the elements are faded out, fadeToggle() will fade them in. If the elements are faded in, fadeToggle() will fade them out.

Which are the jQuery fading methods?

jQuery example: fadeIn(), fadeOut(), and fadeToggle() This is an example using jQuery's fadeIn() , fadeOut() , and fadeToggle() methods to change the visibility of elements on the page with a fading effect.

What is the difference between fadeOut and hide in jQuery?

fadeOut() the place will be removed at once. . show(duration) and . hide(duration) animate the size of element (also the opacity) to 100% and 0% and the place of elements is also animated in that duration.


1 Answers

Use fadeOut() jQuery function for document or "html":

$(document).fadeOut();

or

$("html").fadeOut();

After reading your comments I understand you want to fade out the page when clicking a link.

Don't use $(window).unload but detect the click events on the links and set the location manually preventing the default browser behavior.

// delegate all clicks on "a" tag (links)
$(document).on("click", "a", function () {

    // get the href attribute
    var newUrl = $(this).attr("href");

    // veryfy if the new url exists or is a hash
    if (!newUrl || newUrl[0] === "#") {
        // set that hash
        location.hash = newUrl;
        return;
    }

    // now, fadeout the html (whole page)
    $("html").fadeOut(function () {
        // when the animation is complete, set the new location
        location = newUrl;
    });

    // prevent the default browser behavior.
    return false;
});
like image 121
Ionică Bizău Avatar answered Oct 19 '22 18:10

Ionică Bizău