我希望在窗口达到一定宽度(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:07:27
您可以这样做,使用闭包在函数执行时设置一个计数器,然后根据需要继续执行。
$(window).resize(reposition);
function reposition = ({
var count = 0;
return function() {
if (count !== 0) {
return;
}
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);
count++;
}
};
})();
https://stackoverflow.com/questions/40458718
复制相似问题