ajm*_*jma 5 performance jquery responsive-design gridster
我一直在修补一些关于使gridster.js响应的圆形方法的想法.我在gridster的github问题中发现了一个代码snippit,并意识到如果我在某个窗口大小调整下运行它并销毁并重新运行gridster我可以得到gridster响应responsivley的效果.让我告诉你我的意思 -
$(window).resize(function(){
var width = $(window).width();
var columns = "";
if(Modernizr.mq('screen and (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 to 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');
}
});
Run Code Online (Sandbox Code Playgroud)
因此,当gridster应该分成1列(它的基数为4)时,它会破坏并重新运行1列.这种方法效果出奇的好,问题在于这不是实现这一目标的好方法.我也没有显示所有断点(它根据窗口大小分为3-2 -1列).它运行得非常差,因为它在每个窗口上的调整都会调整大小,我想知道是否有更简洁的方法来执行此操作,其中函数只会在它们达到窗口大小时触发一次.
此外-这仅仅是大小gridster 下来,所以如果你的尺寸了它不会扩大,我还没有尝试过建立这些功能呢.
任何使用可能不会监听窗口重新大小的每个实例的指针或建议都会非常有用.
谢谢!!
Jon*_*son 16
我从你的问题中收集到,当用户调整窗口大小时,gridster正在不断重新运行; 也就是说,如果您通过逐个像素地缓慢拖动窗口来调整窗口大小,则会针对每个像素更改重新运行网格.
尝试使用限制resize事件setTimeout.当resize发生火灾时,你设置一个计时器来重新运行gridster - 比如计时器的持续时间是200ms.如果在200ms内,另一次resize发射,则重置计时器.使用这种方法,重新运行仅在用户完成大小调整时(至少暂时)发生.您可以调整计时器持续时间以获得更好的体验.
尝试:
var resizeTimeout;
$(window).resize(function(){
if(!!resizeTimeout){ clearTimeout(resizeTimeout); }
resizeTimeout = setTimeout(function(){
//do gridster re-run here
},200);
});
Run Code Online (Sandbox Code Playgroud)
示例小提琴(在慢慢调整窗口大小时查看控制台)
编辑:对于任何对此模式感兴趣的人,我从上面的代码中提取了限制逻辑并将其放入jQuery插件中:
(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 for the first time: ' + eventName);
}
_timeout = setTimeout(function(){
callback(e);
},delayInMs);
});
};
})(jQuery);
Run Code Online (Sandbox Code Playgroud)
哪个会像
$(function(){
$(document).onDelayed('click',500,function(){
alert('clicking is done');
});
$(window).onDelayed('resize',1000,function(){
alert('resize is finished');
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5808 次 |
| 最近记录: |