css - Twitter bootstrap carousel with pictures that are not uniform -
let me start sorry long post. i'm attempting use bootstrap carousel , unfortunately pictures have been given not uniform. example 100x200, doe 150x100, etc. aspect ratios different, letter vs landscape. ive attempted number of things, including using following helper function on load of each of images in carousel:
function scaleimage(srcwidth, srcheight, targetwidth, targetheight, fletterbox) { var result = { width: 0, height: 0, fscaletotargetwidth: true }; if ((srcwidth <= 0) || (srcheight <= 0) || (targetwidth <= 0) || (targetheight <= 0)) { return result; } // scale target width var scalex1 = targetwidth; var scaley1 = (srcheight * targetwidth) / srcwidth; // scale target height var scalex2 = (srcwidth * targetheight) / srcheight; var scaley2 = targetheight; // figure out 1 should use var fscaleonwidth = (scalex2 > targetwidth); if (fscaleonwidth) { fscaleonwidth = fletterbox; } else { fscaleonwidth = !fletterbox; } if (fscaleonwidth) { result.width = math.floor(scalex1); result.height = math.floor(scaley1); result.fscaletotargetwidth = true; } else { result.width = math.floor(scalex2); result.height = math.floor(scaley2); result.fscaletotargetwidth = false; } result.targetleft = math.floor((targetwidth - result.width) / 2); result.targettop = math.floor((targetheight - result.height) / 2); return result; } function onimageload(evt) { var img = evt.currenttarget; // what's size of image , it's parent var w = $(img).prop('naturalwidth'); var h = $(img).prop('naturalheight'); //var tw = $(img).parent().width(); //var th = $(img).parent().height(); var tw = $(img).parent().parent().parent().parent().width(); var th = $(img).parent().parent().parent().parent().height(); // compute new size , offsets var result = scaleimage(w, h, tw, th, true); // adjust image coordinates , size img.width = result.width; img.height = result.height; $(img).css("left", result.targetleft); $(img).css("top", result.targettop); }
and using following each of images carousel
<img src="~/images/img1_tall.jpg" alt="tall" id="firstimage" onload="onimageload(event);" />
and first image in carousel works great, each 1 after seem end natural size , horizontally centered against top boarder of carousel.
i've changed "onload" pass values of length , width of image didn't work either, in debug seems first image kicks off "onload" event.
the effect going if ratio of container 3:4 , ratio of image 1:2, image stretch meet left , right edges , center vertically , have letter box above , below, container not change navigation buttons of carousel not move. if image 2:1, image stretch meet top , bottom centered horizontally letterboxes on right , left, again keeping navigation buttons unmoved.
any appreciated... including:
what trying crazy
do want http://jsbin.com/zotelasa/1 . code can active items w,h or other variables used in code run scale image. because of parent.parent codes applies carousels main divs can set own container.
Comments
Post a Comment