jquery - Custom data-* types, css and javascript -
all. building full screen jquery gallery project working on, , running in small hiccup.
to see demo of happening, please visit http://www.idealbrandon.com/gallery.html.
basically, loading bg-image each slide using custom attribute, data-background. works fine first time through, whenever slide loaded second time, not load. html slide is:
<div class="slide" data-background="/img/gallery/2.jpg"> <div class="location">magical aqua ducks</div> <div class="verse"></div> </div> the javascript in question is
for(var = 0; < totalslides; i++){ $pagerlist .append('<li class="page" data-target="'+i+'"></li>'); if ($slides.eq(i).attr("data-background") != null){ $slides.eq(i).css("background-image", "url("+$slides.eq(i).attr("data-background")+")"); }; }; and entire javascript file is
(function($){ function prefix(el){ var prefixes = ["webkit", "moz", "o", "ms"]; (var = 0; < prefixes.length; i++){ if (prefixes[i] + "transition" in el.style){ return '-'+prefixes[i].tolowercase()+'-'; }; }; return "transition" in el.style ? "" : false; }; var methods = { init: function(settings){ return this.each(function(){ var config = { slidedur: 7000, fadedur: 800 }; if(settings){ $.extend(config, settings); }; this.config = config; var $container = $(this), slideselector = '.slide', fading = false, slidetimer, activeslide, newslide, $slides = $container.find(slideselector), totalslides = $slides.length, $pagerlist = $container.find('.pager_list'); prefix = prefix($container[0]); function animateslides(activendx, newndx){ function cleanup(){ $slides.eq(activendx).removeattr('style'); activeslide = newndx; fading = false; waitfornext(); }; if(fading || activendx == newndx){ return false; }; fading = true; $pagers.removeclass('active').eq(newslide).addclass('active'); $slides.eq(activendx).css('z-index', 3); $slides.eq(newndx).css({ 'z-index': 2, 'opacity': 1 }); if(!prefix){ $slides.eq(activendx).animate({'opacity': 0}, config.fadedur, function(){ cleanup(); }); } else { var styles = {}; styles[prefix+'transition'] = 'opacity '+config.fadedur+'ms'; styles['opacity'] = 0; $slides.eq(activendx).css(styles); //$slides.eq(activendx).css("background-image", "url("+$slides.eq(activendx).attr("data-background")+")"); var fadetimer = settimeout(function(){ cleanup(); },config.fadedur); }; }; function changeslides(target){ if(target == 'next'){ newslide = (activeslide * 1) + 1; if(newslide > totalslides - 1){ newslide = 0; } } else if(target == 'prev'){ newslide = activeslide - 1; if(newslide < 0){ newslide = totalslides - 1; }; } else { newslide = target; }; animateslides(activeslide, newslide); }; function waitfornext(){ slidetimer = settimeout(function(){ changeslides('next'); },config.slidedur); }; for(var = 0; < totalslides; i++){ $pagerlist .append('<li class="page" data-target="'+i+'"></li>'); if ($slides.eq(i).attr("data-background") != null){ $slides.eq(i).css("background-image", "url("+$slides.eq(i).attr("data-background")+")"); //alert($slides.eq(i).attr("data-background")); }; }; $container.find('.page').bind('click',function(){ var target = $(this).attr('data-target'); cleartimeout(slidetimer); changeslides(target); }); var $pagers = $pagerlist.find('.page'); $slides.eq(0).css('opacity', 1); $pagers.eq(0).addclass('active'); activeslide = 0; waitfornext(); }); } }; $.fn.easyfader = function(settings){ return methods.init.apply(this, arguments); }; })(jquery); thanks in advance
having had @ gallery.js file have following function called on fade transition: cleanup()
in function remove style attribute $slides:
$slides.eq(activendx).removeattr('style'); which removing background-image style too. never set again.
after above line remove styles may want include:
$slides.eq(activendx).css("background-image", "url("+$slides.eq(activendx).data("background")+")");
Comments
Post a Comment