简体   繁体   English

jQuery淡入淡出过渡到下一页时会引起怪异的闪烁

[英]jQuery fade transition causes weird flicker when fading into next page

I have an issue with my jQuery code where whenever I transition to another html file in my repository, there is this subtle flicker that happens before the next page transitions in. 我的jQuery代码有一个问题,每当我过渡到存储库中的另一个html文件时,都会在下一页过渡之前发生这种微妙的闪烁。

Here is the code: 这是代码:

$(document).ready(function() {
    $('body').hide().fadeIn(200);
    $("a").click(function(e) {
        e.preventDefault();
        $link = $(this).attr("href");
        $("body").fadeOut(200,function(){
          window.location =  $link; 
        });
    });
 });

I'm really not sure how to solve it and could use some help in figuring out...thanks! 我真的不确定如何解决这个问题,可以在找出...谢谢方面使用一些帮助!

To understand this behavior, first take a look at how $(document).ready works in jQuery: 要了解这种行为,请先看一下$(document).ready在jQuery中的工作方式:

The ready event occurs when the DOM (document object model) has been loaded. 当已加载DOM(文档对象模型)时,发生ready事件。

The code inside $(document).ready - and thus, the hide() function - gets executed once the page has already been rendered, naturally causing a noticeable flicker before hiding the body all of sudden and then fading it in. $(document).ready的代码-因此, hide()函数-在页面呈现后立即执行,自然会引起明显的闪烁,然后突然将body隐藏起来,然后逐渐淡入。

One way to solve this issue could be by adding some CSS and approaching the problem from a different angle. 解决此问题的一种方法可能是添加一些CSS,然后从另一个角度解决问题。

First, you could add a class, ex. 首先,您可以添加一个类,例如。 .faded-out to the body element in HTML. .faded-out到HTML中的body元素。

The following CSS code would need to be added: 需要添加以下CSS代码:

body{
    transition: opacity 200ms ease-in-out;
}

.faded-out{
    opacity: 0;
}

(the 200ms corresponds to 200 in jQuery; ease-in-out corresponds to default animation timing function in jQuery, swing ) 200ms对应于jQuery中的200ease-in-out对应于jQuery中的默认动画定时功能, swing

Then, you could remove .faded-out class from body inside of $(document).ready like this: 然后,您可以从$(document).ready .faded-out内部的body删除.faded-out类,如下所示:

$(document).ready(function() {
    $('body').removeClass("faded-out");
    ...your remaining code
    });
 });

Perceived effect should be the same, minus the flicker, as the body is faded out before $(document).ready gets called. 感觉到的效果应该是相同的,减去闪烁,因为在调用$(document).ready之前主体会淡出。

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

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