9

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>
4

1 回答 1

22

为or使用fadeOut()jQuery 函数:document"html"

$(document).fadeOut();

或者

$("html").fadeOut();

阅读您的评论后,我了解您希望在单击链接时淡出页面。

不要使用$(window).unload但检测链接上的点击事件并手动设置位置以防止默认浏览器行为。

// 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;
});
于 2013-10-16T07:31:23.787 回答