【问题标题】:getJSON issue with loading images across Safari and iOS跨 Safari 和 iOS 加载图像的 getJSON 问题
【发布时间】:2018-11-26 01:39:07
【问题描述】:

我正在尝试加载一系列图像,这些图像在页面加载时以 300 毫秒的间隔消失。

图像是根据用户的屏幕尺寸从 JSON 文件中随机选择的。

这在 Chrome 中有效,但似乎随机失败,并且在 Safari(在随机图像上暂停)或 iOS(根本无法加载任何图像)中根本不起作用。

这是我的代码:

// get the screen orientation based on css media query
var orientation = window.getComputedStyle(document.querySelector('body'), ':after').getPropertyValue('content').replace(/['"]+/g, '');

window.slides = [];

// function to randomise the order of an array
function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

$(document).ready(function() {
    $( 'html' ).addClass( 'js' ).removeClass( 'no-js' );

  // define the category and number of slides
  var imageDirs = { "lovers": 16 };

  // get slide urls from json file
  $.getJSON('slides/slides.json', function (data) {
    for (var thisDir in imageDirs) {
      var theseSlides = [];
      theseSlides = data[thisDir][orientation];
      shuffle(theseSlides)
      slides = theseSlides.slice(0, imageDirs[thisDir]);
    }
  })
  .done(function() {
    // randomise order of slides
    shuffle(slides);

    // have one blank slide at the beginning of the animation
    slides.push("initial-slide.png");

    if ( slides.length ) {
      for (var i = 0; i < slides.length; i++) {
        $('#wrapper').append('<img class="slide" src="slides/' + slides[i] + '">');
      }
    }
  });
});

$(window).on('load', function(){
  // wait for images to load before displaying animation
  $('#wrapper').waitForImages(true).done(function() {
    $('#preloader').remove();

    var currentSlides = [];

    $('.slide').each(function(){
      currentSlides.push($(this));
    });

    // remove slides at an interval of 300ms
    var timer = setInterval(function(){
      if (currentSlides.length){
        currentSlides.pop().remove();
      } else {
        clearInterval(timer);
      }
    }, 300);
  });
});

JSFiddle:https://jsfiddle.net/robertirish/6g41vwnL/59/

现场直播:http://robertirish.com

提前致谢!

【问题讨论】:

  • 很难说没有看到小提琴或其他东西,但至少你的一个问题源于使用 setInterval。我认为您希望每 300 毫秒发生一次?一张幻灯片,然后是下一张,然后是下一张,是吗?正如你所拥有的,这只会发生一次。您需要设置一个递归 setTimeout。这是一篇很棒的文章,解释了调度以及 setInterval 和 setTimeout 之间的区别:javascript.info/settimeout-setinterval
  • 您好,感谢您的浏览。它确实在 Chrome 中循环遍历所有图像!只是不在 Safari 或 iOS 上,它根本不显示任何内容......
  • 用 JSFiddle 更新:jsfiddle.net/robertirish/6g41vwnL/59

标签: javascript jquery json image preloading


【解决方案1】:

感谢您的提琴。基本上你的问题是,在你的 load 事件中,图像还没有被附加到 DOM 中。你可以通过登录currentSlides看到这一点:https://jsfiddle.net/1fb3gczq/

因此,您需要在操作 DOM 之前确保它已准备就绪。我已将您的功能移至在这种情况下有效的 ajax 成功,但在较慢的连接上,您只想在所有图像都加载到 DOM 后才运行效果。这是一个有效的小提琴:https://jsfiddle.net/rcx3w48b/

【讨论】:

  • 感谢 James,这是一个巨大的帮助。您是否知道我将如何判断所有图像何时已加载到 DOM 中?由于我是 getJSON 和 ajax 的新手,所以我对操作顺序有点模糊。
  • 我想我已经使用 waitForImages jquery 插件解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-21
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
相关资源
最近更新 更多