简体   繁体   English

jquery 加载回调 function 不同步工作

[英]jquery load callback function not working synchronously

Ok from looking previously I heard that I could use a callback function to make a jquery load function synchronous.好的,从以前的角度来看,我听说我可以使用回调 function 使 jquery 加载 function 同步。

For context I'm trying to do a crossfading transition that is triggered from an onclick function.对于上下文,我正在尝试执行从 onclick function 触发的交叉淡入淡出过渡。 Basically I want to ensure that the content on bottom has loaded before the top starts going transparent but that doesn't always happen clearly, there is an image on them which quite clearly hasn't loaded by that time, is there a fix to ensure it waits?基本上我想确保在顶部开始透明之前已经加载了底部的内容,但这并不总是清楚地发生,它们上面有一个图像很明显当时还没有加载,是否有修复程序来确保它等待?

Below is a simplified version of the code to show the issue.下面是显示问题的代码的简化版本。 Help would be appreciated.帮助将不胜感激。

 if (onTop === true)  {
    onTop = false;
    $("#bottom").load(Location, function() {
        setTimeout(function() {
            if (onTop === false) {
                document.getElementById("bottom_container").style.zIndex = 0;
            }
        }, 800);
        document.getElementById("top_container").style.opacity = 0;

        document.getElementById("bottom_container").style.height = null;
        var bottomContainerHeight = document.getElementById("bottom_container").clientHeight;
        document.getElementById("top_container").style.height = (bottomContainerHeight) + "px";
        //Makes sure the top content is equal in height to the top content as to not overflow past it.
    });
  }
  else {
      onTop = true;
      $("#top").load(Location, function() {
          document.getElementById("bottom_container").style.zIndex = -2;
          document.getElementById("top_container").style.opacity = 1;

          document.getElementById("top_container").style.height = null;
          var topContainerHeight = document.getElementById("top_container").clientHeight;
          document.getElementById("bottom_container").style.height = (topContainerHeight) + "px";
          //Makes sure the bottom content is equal in height to the top content as to not overflow past it.
      });
  }

You could accomplish this using CSS animations and/or transitions.您可以使用 CSS 动画和/或过渡来完成此操作。

The z-index doesn't matter in this case.在这种情况下,z-index无关紧要

Since you'll be fading the elements in and out, it doesn't matter what layer they're on.由于您将淡入淡出元素,因此它们在哪个图层上并不重要。

@keyframes fade {
    0% { opacity: 0; }
    100% { opacity: 1; }
}
.fade-in {
    animation: fade 500ms linear 0 1 normal forwards;
}
.fade-out {
    animation: fade 500ms linear 0 1 reverse forwards;
}

Then you can just toggle the classes for each to get the effect.然后你可以切换每个类来获得效果。

document.querySelector("#top").classList.add("fade-out");
document.querySelector("#top").classList.remove("fade-in");
document.querySelector("#bottom").classList.add("fade-in");
document.querySelector("#bottom").classList.add("fade-out");

The images themselves can be preloaded.图像本身可以预加载。

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

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