元素在重复触发时忽略 jQuery
Element ignores jQuery if triggered repeatedly
我正在使网站响应式,到目前为止它运行良好,不过我目前遇到的一个问题是,如果我反复快速更改窗口大小,它似乎忽略了jQuery。当这种情况发生时,一个在满足条件时应该"显示:none;"的元素会留在页面上。
css 很简单:
body:after{display:none; content:"default";}
@media only all and (max-width: 800px){
body:after{content:"tablet";}
}
jquery看起来像这样:
jQuery(document).ready(function($) {
var currentSize = "default";
var lazyLayout = _.debounce(function(e) {
}, 300, true);
$(window).resize(lazyLayout,function() {
var size = window.getComputedStyle(document.body, ':after').getPropertyValue('content');
/* Ridiculous thing to deal with inconsistent returning of
value from query. Some browsers have quotes some don't */
size = size.replace(/"/g, "");
size = size.replace(/'/g, "");
if (size != currentSize) {
if (size == 'tablet') {
var count = $('#mobileNav').length;
if (count < 1) {
var data = {
dataType: "HTML",
action: 'nav_media_query',
nonce: myAjax.nonce
};
$.post( myAjax.url, data, function( data ) {
$('#content-wrapper').removeClass('col-4-5');
$('#trending-Container').addClass('mobileNavStyle');
$('#trending-Container').append(data);
});
currentSize = 'tablet';
}
}
if(size == 'default') {
$('#mobileNav').remove();
currentSize = 'default';
}
}
}).resize();
});//end function
这将检查是否已加载媒体查询,但查找内容属性,然后触发ajax请求并将一些wordpress php加载到元素中。
如果我慢慢调整窗口大小,它可以完美地工作,但如果我快速反复调整窗口,它会破裂。
我可以使用一些jQuery函数来阻止它中断吗?
编辑:我已经更新了我的代码以添加_.js debounce方法,这应该有助于限制ajax请求。但是,在不再满足要求后未删除元素的问题仍然存在。
尝试在等待的调整大小函数中调用一个函数。我不太确定它是否会起作用,但尝试以这种方式使用 jquery 中的延迟函数:
setTimeout(showpanel, 1000)
function showpanel() {
//Code that you want to execute
}
Undescore.js解决了这个问题,我只是没有正确应用函数。
这是结果。
jQuery(document).ready(function($) {
var currentSize = "default";
$(window).resize(_.debounce(function(){
var size = window.getComputedStyle(document.body, ':after').getPropertyValue('content');
/* Ridiculous thing to deal with inconsistent returning of
value from query. Some browsers have quotes some don't */
size = size.replace(/"/g, "");
size = size.replace(/'/g, "");
if (size != currentSize) {
if (size == 'tablet') {
var count = $('#mobileNav').length;
if (count < 1) {
var data = {
dataType: "HTML",
action: 'nav_media_query',
nonce: myAjax.nonce
};
$.post( myAjax.url, data, function( data ) {
$('#content-wrapper').removeClass('col-4-5');
$('#trending-Container').addClass('mobileNavStyle');
$('#trending-Container').append(data);
});
currentSize = 'tablet';
}
}
if(size == 'default') {
$('nav').remove('#mobileNav');
$('#content-wrapper').addClass('col-4-5');
$('#trending-Container').removeClass('mobileNavStyle');
currentSize = 'default';
}
}
},200));
});//end function
相关文章:
- Firefox赢得了'不会显示我添加的jquery元素,但Chrome会显示
- 使用返回函数sinde.attr()jquery元素
- Wooccommerce所有JS和Jquery元素都未加载
- 追加JQuery元素和Angular
- 如何获取jQuery集合中的第k个元素..作为JQUERY元素
- 是否存在jQuery元素选择库的最小化版本
- 不能同时通过类和数字 ID 选择 jquery 元素
- 为什么jquery元素在ipad上没有响应
- Jquery元素用法
- 用于获取jQuery元素或值的方法
- 淡入淡出切换jquery元素
- 用于查找在 jQuery 元素集中具有特定类的元素的索引的单行代码
- 选择具有由 data() 设置的某个值的 JQuery 元素
- Jquery 元素的高级过滤
- 链接到另一个带有jQuery元素的页面
- jQuery:通过 .wrap() 添加到 DOM 的 jQuery 元素上的切换类
- 将字符串转换为 jquery 元素并将其追加到 DOM 中
- 与“click”事件一起存储的jQuery元素未按要求工作
- 在哈希数组中保存对 jquery 元素的引用
- 如何从核心JavaScript选择中获取jquery元素