Skip to content

Instantly share code, notes, and snippets.

@ajmalafif
Forked from dcatkins40/Flexslider to Change Background Image
Last active December 11, 2015 19:28
Show Gist options
  • Save ajmalafif/4648714 to your computer and use it in GitHub Desktop.
Save ajmalafif/4648714 to your computer and use it in GitHub Desktop.
[wp - forked] - Flexslider to Change Background Image
$(document).ready(function() {
var imgArr = new Array (
<?php
$total = count(get_field('field'));
$count = 1;
while(the_repeater_field('field', $pagenumber)): ?>
'<?php the_sub_field('sub_field'); ?>'<?php if($count < $total){echo ',';} $count++; ?>
<?php endwhile; ?>
);
var n = 0;
function preload(arrayOfImages) {
$(arrayOfImages).each(function(){
$('<img/>')[0].src = this;
});
}
preload(imgArr);
$('.flexslider').flexslider({
start: function(slider) {
$('#container').attr('style', 'background-image: url(' + imgArr[n] + '); background-position: center; background-repeat: no repeat; background-size: cover;');
},
before: function(slider) {
},
after: function(slider) {
n = slider.currentSlide;
$('#container').attr('style', 'background-image: url(' + imgArr[n] + '); background-position: center; background-repeat: no repeat; background-size: cover;');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment