Firefox 加载视差图像大小错误(仅在刷新时)

Firefox loads parallax image size wrong (only on refresh)

我在当前的 Firefox 中有一个奇怪的错误。我是运行一个视差脚本(我认为它来自Materialise),这是来源:

(function($) {
  $.fn.parallax = function() {
    var window_width = $(window).width();

    return this.each(function(i) {
      var $this = $(this);
      $this.addClass('parallax');

      $this.find('img').each(function() {
        $(this).css('background-image', 'url(' + $(this).prop('currentSrc') + ')');
        $(this).attr('src', 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==');
        $(this).attr('srcset', '');
      });

      function updateParallax(initial) {
        var container_height;
        // if (window_width < 992) {
        container_height = ($this.height() > 0) ? $this.height() : $this.children("img").height();
        // }
        // else {
        // container_height = ($this.height() > 0) ? $this.height() : 500;
        // }
        var img_height = $this.children("img").height();
        var parallax_dist = img_height - container_height;
        var bottom = $this.offset().top + container_height;
        var top = $this.offset().top;
        var scrollTop = $(window).scrollTop();
        var windowHeight = window.innerHeight;
        var windowBottom = scrollTop + windowHeight;
        var percentScrolled = (windowBottom - bottom) / (container_height + windowHeight);
        var parallax = -1 * parallax_dist * percentScrolled;

        console.log(
          'img_height: ' + img_height + '\n' +
          'parallax_dist: ' + parallax_dist + '\n' +
          'bottom: ' + bottom + '\n' +
          'top: ' + top + '\n' +
          'scrollTop: ' + scrollTop + '\n' +
          'windowHeight: ' + windowHeight + '\n' +
          'windowBottom: ' + windowBottom + '\n' +
          'percentScrolled: ' + percentScrolled + '\n' +
          'parallax: ' + parallax + '\n'
        );

        if ((bottom > scrollTop) && (top < (scrollTop + windowHeight))) {
          $this.children("img").first().css('bottom', parallax + "px");
        }
        if (initial) {
          $this.children("img").first().css('display', 'block');
        }
      }

      // Wait for image load
      $this.children("img").one("load", function() {
        updateParallax(true);
      }).each(function() {
        if (this.complete) $(this).load();
      });

      $(window).scroll(function() {
        window_width = $(window).width();
        updateParallax(false);
      });

      $(window).resize(function() {
        window_width = $(window).width();
        updateParallax(false);
      });

    });

  };
}(jQuery));
.parallax-container {
  position: relative;
  overflow: hidden;
  height: 500px;
}
.parallax {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
}
.parallax img {
  display: none;
  position: absolute;
  bottom: 0;
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  background-position: bottom;
  background-size: contain;
  background-repeat: no-repeat;
}
<div class="full-parallax parallax-container">
  <div class="parallax">
    <img src="http://lorempixel.com/1000/1000/" srcset="http://lorempixel.com/1000/1000/ 2000w, http://lorempixel.com/500/500/ 500w" alt="">
  </div>
</div>

<div class="content">
  <h1>Hello!</h1>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
  <p>Text text text text text text</p>
</div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>
  $(document).ready(function() {
    $('.parallax').parallax();
  });
</script>

一切正常,但在 Firefox 中,这仅在我第一次加载页面(或通过删除缓存重新加载)时有效。 如果我正常重新加载页面 (F5),视差加载不正确,(图像太远了)直到我第一次滚动,然后图像直接跳到位。

问题似乎是 FF 加载图像尺寸错误。在没有缓存的情况下重新加载时,它会读取具有 1707 的同一图像的图像高度,而在有缓存的情况下正常重新加载时,它会读取 678。不过,一旦我开始滚动,该值就会跳到 1707。

编辑:它也只会在页面滚动到绝对顶部时发生,如果我向下滚动 1px 它会正确加载图像。

Chrome 维瓦尔第做得很好。

不是您问题的正确解决方案,但解决方法可能是在页面加载时触发 scroll 事件:

  $(window).on("load", function(){
    $(window).trigger("scroll");
  });//window load
}(jQuery));// last line of your code. Add the trigger code just above this line

这很可能是 FF 错误 (https://bugzilla.mozilla.org/show_bug.cgi?id=1281438)