javascript - Jquery image slider works perfectly from right to left, but from left to right I cannot get it centered -
i'm working on photography portfolio display/gallery slider.
i have half working prototype, cannot life figure out why centers images in 1 direction not other :s
interestingly if resize window , call move_slide() function second time goes right place... happening? can shed light?
i believe quite quirky problem. fresh pair of eyes can me allot.
so far code:
jquery(window).load(function() { // function set_slide_width(add_extra) { if (!add_extra) {add_extra = 0;} var slide_width = 0; $("#barely_slide article img").each(function(){ slide_width += $(this).outerwidth(); }); $("#barely_slide article").css("width",slide_width + add_extra); } // set_slide_width(); // function move_slide() { var focus_margin = 0; var img_real_height = 0; // var add_extra = 0; //var prev_deduct = 0; // $("#barely_slide article img").each(function(){ // //if ($(this).attr("class") == "previous") { // var theimage = new image(); // theimage.src = $(this).attr("src"); // var img_real_width = theimage.width; // var img_real_width_padding = img_real_width + ($(this).outerwidth() - $(this).width()); // prev_deduct = img_real_width_padding - $(this).outerwidth(); //} // if ($(this).attr("class") == "focus") { var theimage = new image(); theimage.src = $(this).attr("src"); var img_real_width = theimage.width; var img_real_width_padding = img_real_width + ($(this).outerwidth() - $(this).width()); img_real_height = theimage.height; // add_extra = img_real_width_padding - $(this).outerwidth(); // focus_margin += img_real_width_padding / 2; return false; } focus_margin += $(this).outerwidth(); }); // set_slide_width(add_extra); // var container_center = $("#barely_slide").outerwidth() / 2; var offset = container_center - focus_margin; //console.dir(offset); $("#barely_slide article").animate({"margin-left": offset }, "fast"); // var img_height_offset = (img_real_height / 2) - 150; $(".focus").animate({"height":img_real_height,"margin-top":-img_height_offset}, "fast"); } // move_slide(); // var resize_timeout; $(window).resize(function() { cleartimeout(resize_timeout); resize_timeout = settimeout(function(){ move_slide(); },100); }); // $("#barely_slide article img").on("click", function(){ if ($(this).attr("class") == "focus") {return false;} // $("#barely_slide article img").removeclass("previous"); $("#barely_slide article .focus").addclass("previous"); $(".previous").animate({"height":300,"margin-top":0}, "fast"); // $("#barely_slide article .focus").removeclass("focus"); $(this).addclass("focus"); move_slide(); return false; }); });
i found solution works better. have change move_slide
function:
function move_slide() { var focus_margin = 0; var img_real_height = 0; var add_extra = 0; $("#barely_slide article img").each(function(){ if ($(this).hasclass("focus")) { var theimage = new image(); theimage.src = $(this).attr("src"); var img_real_width = theimage.width; var img_real_width_padding = img_real_width + ($(this).outerwidth() - $(this).width()); img_real_height = theimage.height; add_extra = img_real_width_padding - $(this).outerwidth(); focus_margin += img_real_width_padding / 2; return false; /***************** changes begin *************************/ } else if ($(this).hasclass("previous")) { // if has previous class, calculate width height of 300. var shrinked_width = 300 * $(this).width() / $(this).height(); // add padding. var img_width_width_padding = shrinked_width + ($(this).outerwidth() - $(this).width()); // update focus_margin focus_margin += img_width_width_padding; } else { focus_margin += $(this).outerwidth(); } /***************** changes end ***************************/ }); set_slide_width(add_extra); var container_center = $("#barely_slide").outerwidth() / 2; var offset = container_center - focus_margin; $("#barely_slide article").animate({"margin-left": offset }, "fast"); var img_height_offset = (img_real_height / 2) - 150; $(".focus").animate({"height":img_real_height,"margin-top":-img_height_offset}, "fast"); }
here jsfiddle: http://jsfiddle.net/jqalb/4/