简体   繁体   English

图像前延迟加载Flexslider

[英]Lazy Loading Flexslider before images

Hey guys im trying to lazy load my flexslider slide show on my website but Im running into a problem where all the images will load and appear on the site for a few seconds then the slider will load. 大家好,我试图在我的网站上延迟加载我的flexslider幻灯片,但是我遇到了一个问题,所有图像都将加载并出现在网站上几秒钟,然后滑块将被加载。 I think this is happening because the slider is loads only after all the images are loaded. 我认为这是因为仅在加载所有图像之后才加载滑块。 My website is a wordpress site and i loaded the flexslider in a few divs. 我的网站是wordpress网站,我在几个div中加载了flexslider。

This is what it looks like before it loads: 这是加载之前的样子: 在此处输入图片说明

I've tried using a script that supposedly loads the slider first before the images but that doesnt seem to work: 我尝试使用一个脚本,该脚本应该首先在图像之前加载滑块,但似乎不起作用:

$(window).load(function() {
  $('.flexslider').flexslider({
    touch: true,
    slideshow: false,
    controlNav: true,
    slideshowSpeed: 7000,
    animationSpeed: 600,
    initDelay: 0,
    start: function(slider) { // Fires when the slider loads the first slide
      var slide_count = slider.count - 1;

      $(slider)
        .find('img.lazy:eq(0)')
        .each(function() {
          var src = $(this).attr('data-src');
          $(this).attr('src', src).removeAttr('data-src');
        });
    },
    before: function(slider) { // Fires asynchronously with each slider animation
      var slides     = slider.slides,
          index      = slider.animatingTo,
          $slide     = $(slides[index]),
          $img       = $slide.find('img[data-src]'),
          current    = index,
          nxt_slide  = current + 1,
          prev_slide = current - 1;

      $slide
        .parent()
        .find('img.lazy:eq(' + current + '), img.lazy:eq(' + prev_slide + '), img.lazy:eq(' + nxt_slide + ')')
        .each(function() {
          var src = $(this).attr('data-src');
          $(this).attr('src', src).removeAttr('data-src');
        });
    }
  });
});

Any help will be appreciated Thanks 任何帮助将不胜感激谢谢

So I couldn't figure out a clear cut strategy to lazy load the flex-slider on my site. 因此,我想不出一个明确的策略来将flex-slider延迟加载到我的网站上。 The nature of my problem was that my pictures were loaded first before the flex-slider loaded. 我的问题的本质是在加载flex-slider之前先加载我的图片。 So a workaround for this problem would be to allow the images to be hidden until flex-slider is loaded. 因此,解决此问题的方法是允许隐藏图像,直到加载flex-slider为止。

I changed my CSS to: 我将CSS更改为:

#banner{display:none;}

I then added a javascript line that would show that id after the flex-slider was initialised. 然后,我添加了一条JavaScript行,在初始化flex-slider之后将显示该id。

jQuery('div#banner').show();

Now the site loads the rest of the content and then loads the flex-slider after. 现在,该站点将加载其余内容,然后再加载flex-slider。

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

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