我希望在窗口达到一定宽度(1023px)时交换2个div的内容,但我不希望在窗口达到一定宽度(1023px)后每次调整窗口大小时都继续运行交换代码:
$(window).resize(function() {
if($(window).width() <= 1023) {
var $left_col = $('.about-left-col').html();
var $right_col = $('.about-right-col').html();
$('.about-right-col').html($left_col);
$('.about-left-col').html($right_col);
}
});发布于 2016-11-07 14:03:33
使用==
$(window).resize(function() {
if($(window).width() == 1023) {
var $left_col = $('.about-left-col').html();
var $right_col = $('.about-right-col').html();
$('.about-right-col').html($left_col);
$('.about-left-col').html($right_col);
}
});发布于 2016-11-07 14:04:10
您可以设置一个简单的变量(在调整函数大小的上面)并对其进行检查。
var wasResized = false;调整大小时将其设置为true,并在条件为true/false时对其进行检查。
发布于 2016-11-07 14:04:19
使用全局变量来存储状态
var notChanged = true;
$(window).resize(function() {
if($(window).width() <= 1023) {
if(notChanged) {//test if its not changed
var $left_col = $('.about-left-col').html();
var $right_col = $('.about-right-col').html();
$('.about-right-col').html($left_col);
$('.about-left-col').html($right_col);
notChanged = false;//set it to false so the code doesn't trigger anymore
}
}
});https://stackoverflow.com/questions/40458718
复制相似问题