javascript - Script to tile images not initialized until you resize the browser -
i have page supposed tile images this
but when first land on page see images stack behind each other
if resize browser @ shift position. @ on jquery makes happen
jquery
var colcount = 0; var colwidth = 300; var margin = 10; var spaceleft = 0; var windowwidth = 0; var blocks = []; $(function(){ $(window).resize(setupblocks); }); function setupblocks() { windowwidth = $(window).width(); blocks = []; // calculate margin blocks evenly spaced within window colcount = math.floor(windowwidth/(colwidth+margin*2)); spaceleft = (windowwidth - ((colwidth*colcount)+(margin*(colcount-1)))) / 2; console.log(spaceleft); for(var i=0;i<colcount;i++){ blocks.push(margin); } positionblocks(); } function positionblocks() { $('.block').each(function(i){ var min = array.min(blocks); var index = $.inarray(min, blocks); var leftpos = margin+(index*(colwidth+margin)); $(this).css({ 'left':(leftpos+spaceleft)+'px', 'top':min+'px' }); blocks[index] = min+$(this).outerheight()+margin; }); } // function min value in array array.min = function(array) { return math.min.apply(math, array); };
i think there must not wrapping.
in html have on <body>
html
<body onload="setupblocks();">
my guess line causing problem this
$(function(){ $(window).resize(setupblocks); });
i google don't know called in there see other options. can me figure out gap in information.
edit: tutorial got works fine because had of items hand coded. in projects being loaded mustache template.
so though script load onload
guess, how fire off both on load, , again when resized?
call setupblocks
function after page loaded:
$(function(){ setupblocks(); $(window).resize(setupblocks); });
edit:
looking @ html page, looks creating elements dynamically after page loaded. try update code in html page:
$(function() { $.getjson('img.js', function(data) { var template = $('#imgtpl').html(); var html = mustache.to_html(template, data); $('#image-list').html(html); setupblocks(); }); });
Comments
Post a Comment