简体   繁体   English

使用Jquery淡出进行页面过渡

[英]Using Jquery fadeout for page transition

Hello and thank you in advance for any solutions. 您好,谢谢您提供任何解决方案。 I've been trying to add a fade in and fade out function when users are switching through pages. 我一直在尝试在用户切换页面时添加淡入和淡出功能。 I have tried many solutions found here and on other forums but none of it seems to work for the fade out. 我尝试了在这里和其他论坛上找到的许多解决方案,但似乎都无法解决。 The fade in works just fine I just had to add .ghostly to the body tag. 淡入效果很好,我只需要在body标签上添加.ghostly即可。 Nothing I have done has worked for the fadeout. 我所做的一切都对淡出没有作用。 Any help would be greatly appreciated as I'm fairly new to coding. 任何帮助将不胜感激,因为我是编码的新手。

<script src="http://code.jquery.com/jquery-latest.js"></script>
<style>
    .ghostly {
        opacity: 0;
    }
</style>
<script>
    $(document).ready(function() {
        $('.ghostly').animate({
            opacity: '1'
        }, 3000);
    });
    $("a").click(function(event) {
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(1000, redirectPage(linkLocation));
    });

    function redirectPage(link) {
        document.location.href = link;
    }
</script>

1. Fadeout firing immediately! 1.淡出立即射击!

When you click the anchor, the next page will start loading and any code on the current page will cease. 当您单击锚点时,下一页将开始加载,当前页上的所有代码都将停止。 You have incorrectly covered that situation by not waiting for the fadeOut to finish! 没有等待fadeOut完成就错误地解决了这种情况! You need to wrap that call in an anonymous function, 您需要将该调用包装在匿名函数中,

eg 例如

$("body").fadeOut(1000, function(){redirectPage(linkLocation)});

otherwise you are simply calling redirectPage immediately and passing its return value to the fadeOut method ! 否则,您只是立即调用redirectPage 并将其返回值传递给fadeOut方法

Option: An alternative transition method, is to use Ajax for loading of all pages. 选项:另一种过渡方法是使用Ajax加载所有页面。 Then you can apply any effect you want (you can do that generically in your anchor click event handler). 然后,您可以应用所需的任何效果(可以在定位点击事件处理程序中进行常规操作)。 This requires you update the browser history as the pages change, but is very effective (you can transition pages by any means then). 这需要您在页面更改时更新浏览器历史记录,但是非常有效(您可以通过任何方式转换页面)。

2. Ajax loading: 2. Ajax加载:

You can do something like this to replace the entire page: 您可以执行以下操作来替换整个页面:

JSFiddle: http://jsfiddle.net/TrueBlueAussie/bwdwc/3/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/bwdwc/3/

jQuery(function ($) {
    $('.ghostly').animate({
        opacity: '1'
    }, 3000);
    $(document).on('click', "a", function (event) {
        event.preventDefault();
        $.when($("body").fadeOut(4000).promise(), $.ajax({
            url: this.href,
            type: 'get',
            dataType: 'html'
        })).done(function (html) {
            var newDoc = document.open("text/html", "replace");
            newDoc.write(html);
            newDoc.close();
            $("body").css({
                display: "none"
            });
            $("body").fadeIn(4000);
        });
    });
});

It uses $.when() to combine the animation promise and the Ajax call. 它使用$.when()组合动画承诺和Ajax调用。 When both complete, it processes the data from the Ajax page load and replaces the entire body of the page. 两者都完成后,它将处理来自Ajax页面加载的数据,并替换页面的整个正文。

*Note: This will not load the page in the link due to a 'Access-Control-Allow-Origin' error, but should work fine within your own website. *注意: 由于出现“访问控制允许来源”错误,因此该页面不会加载链接中的页面,但在您自己的网站上应该可以正常工作。

3. Put most jQuery code in document ready : 3. 准备好大多数jQuery代码到文档中

Any jQuery code that accesses " existing " elements needs to go in the document ready (that is elements you assume are already loaded. In this case 'a' = all anchors ). 任何访问“ 现有 ”元素的jQuery代码都需要准备好进入文档(即您假设已加载的元素。在这种情况下, 'a' = 所有anchors )。

eg 例如

$(document).ready(function() {
    $('.ghostly').animate({
        opacity: '1'
    }, 3000);
    $("a").click(function(event) {
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(1000, redirectPage(linkLocation));
    });
});

You can put jQuery code at the end of body to get a similar effect, but it makes for less portable code (especially when the code is in separate .js files... which it often will/should be to improve caching). 您可以将jQuery代码放在body的末尾,以获得类似的效果,但是它使可移植的代码更少(尤其是当代码位于单独的.js文件中时……通常/应该改善缓存)。

4. Or use a deferred event handler: 4.或使用延迟事件处理程序:

If you use deferred event handler you can do this instead: 如果使用延迟事件处理程序,则可以执行以下操作:

<script>
    $(document).ready(function() {
        $('.ghostly').animate({
            opacity: '1'
        }, 3000);
    });

    // Catch all anchor clicks bubbled to the document...
    $(document).on('click', "a", function(event) {
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(1000, redirectPage(linkLocation));
    });

    function redirectPage(link) {
        document.location.href = link;
    }
</script>

Deferred event handlers work by listening for events at an ancestor element that does not change (eg document in this case, as it is already present), then use the jQuery selector to match elements, then call the function for any matching elements that caused the event . 延迟事件处理程序的工作方式是侦听未更改的祖先元素上的事件(例如,在本例中为文档,因为它已经存在), 然后使用jQuery选择器来匹配元素, 然后针对导致事件

5. Notes: 5.注意事项:

I would also recommend you use this shortcut for document ready anyway: 我也建议您始终使用此快捷方式来准备文档:

$(function(){
     // Your code here
});

or better yet (to avoid namespace clashes with $): 或更好的方法(以避免名称空间与$冲突):

jQuery(function($){
     // Your code using the local $ here
});

This second version is the best catch-all as this document ready passing a reference to jQuery as the first parameter. 第二版是最好的工具,因为本文档准备将对jQuery的引用作为第一个参数传递。 Note: Although it looks similar this is not an IIFE (immediately invoked function expression) that you sometimes see wrapping jQuery code . 注意:尽管它看起来很相似,但这不是IIFE(立即调用的函数表达式),您有时会看到它包装了jQuery代码

6. Final recommendation: 6.最终建议:

Put that all together and you get this: 将所有内容放在一起,您会得到:

jQuery(function($) {
    $('.ghostly').animate({
        opacity: '1'
    }, 3000);
    $(document).on('click', "a", function(event) {
        event.preventDefault();
        linkLocation = this.href;
        $("body").fadeOut(1000, function(){redirectPage(linkLocation)});
    });
});

try tying the fadeout handler to the unload event 尝试将淡出处理程序绑定到unload事件

$( window ).unload(function() {
alert( "Handler for .unload() called." );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM