performance - Efficient way of using window.resize (or other method) to fire jquery functions, and making gridster.js repsonsive -
i have been tinkering thoughts on round-about way of making gridster.js responsive. found code snippit in github issues in gridster , realized if ran under window sizing , destroyed , re-ran gridster can effect of gridster responding responsivley. let me show mean -
$(window).resize(function(){ var width = $(window).width(); var columns = ""; if(modernizr.mq('screen , (max-width:640px)')){ columns = 1; gridster.destroy(); var toofar = $('.gridster .gs-w').filter(function () { return $(this).data('col') > columns; }); $(toofar).each(function () { $(this).attr('data-col', '1'); }); var toobig = $('.gridster li').filter(function () { return $(this).data('sizex') > columns; }); $(toobig).each(function () { $(this).attr('data-sizex', columns); }); $(".gridster ul").gridster({ widget_base_dimensions: [300, 300], widget_margins: [5, 5], max_cols: 1, resize: { enabled: true }, draggable: { handle: '.dragdiv' }, serialize_params: function ($w, wgd) { return { /* add element id data*/ id: $w.attr('id'), /* defaults */ col: wgd.col, row: wgd.row, size_x: wgd.size_x, size_y: wgd.size_y, /* htmlcontent: $($w).html() */ } } }).data('gridster'); } }); so when gridster should break 1 column (it has base of 4), destroys , re runs 1 column. works surprisingly well, issue not pretty way of achieving this. not showing break points ( breaks into, 3 -2 -1 columns based on window size). runs poorly because firing on every window resize, , i'm wondering if there cleaner way functions fire once when hit window size.
also - size gridster down, if size out not expand, have not tried building functions yet.
any pointers or suggestions in using maybe isn't listening every single instance of window re-size super helpful.
thanks!!
i gather question gridster being re-run continuously user resizes window; is, if resize window dragging window slowly, pixel pixel, there gridster re-run every pixel change.
try throttling resize event settimeout. when resize fires, set timer re-run of gridster -- timer duration 200ms. if within 200ms, resize fires, reset timer. approach, re-run occurs when user done resizing (at least moment). can tweak timer duration better experience.
try:
var resizetimeout; $(window).resize(function(){ if(!!resizetimeout){ cleartimeout(resizetimeout); } resizetimeout = settimeout(function(){ //do gridster re-run here },200); }); example fiddle (look @ console resize window)
edit: interested in pattern, i've extracted throttling logic above code , put jquery plugin:
(function($){ $.fn.ondelayed = function(eventname,delayinms,callback){ var _timeout; this.on(eventname,function(e){ if(!!_timeout){ cleartimeout(_timeout); console.log('timer being re-set: ' + eventname); } else { console.log('timer being set first time: ' + eventname); } _timeout = settimeout(function(){ callback(e); },delayinms); }); }; })(jquery); which used like
$(function(){ $(document).ondelayed('click',500,function(){ alert('clicking done'); }); $(window).ondelayed('resize',1000,function(){ alert('resize finished'); }); });
Comments
Post a Comment