-
-
Save Herm71/89d12f38ebd8c78c7f04cf49c19e8656 to your computer and use it in GitHub Desktop.
Lazy loading images for FlexSlider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section class="slider"> | |
<div class="flexslider"> | |
<ul class="slides"> | |
<li><img src="http://imgur.com/..." alt=""></li> | |
<li><img class="lazy" data-src="http://imgur.com/..." alt=""></li> | |
<li><img class="lazy" data-src="http://imgur.com/..." alt=""></li> | |
<li><img class="lazy" data-src="http://imgur.com/..." alt=""></li> | |
<li><img class="lazy" data-src="http://imgur.com/..." alt=""></li> | |
</ul> | |
</div> | |
</section> | |
<script src="jquery.flexslider.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
$(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'); | |
}); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment