1
我有一個功能,我必須只運行一次,如果達到特定的寬度。jQuery在窗口調整大小隻運行一次功能
功能是用於轉表(帶行列)只能在移動
我需要什麼:
1. ON LOAD
a. if width <992 run transposeTable (mobiles)
b. if width> 992 do nothing
2. ON RESIZE
a. if width <992 run transposeTable ONLY ONCE BUT if loaded page has a smaller width than 992px do nothing (see 1)
b. if width> 992 run transponseTable ONLY ONCE BUT if loaded page has a width greater than 992px to nothing (see 1)
這裏的解決方案(有一些修改)由於@Olaf Nankman
var transposed = "desktop";
$(document).ready(function(){
if($(window).width() < 992){
transposed = "mobile"
transposeTable();
}else{
transposed = "desktop"
}
})
$(window).resize(function(){
if($(window).width() < 992 && transposed != "mobile"){
transposed = "mobile"
transposeTable();
}
if($(window).width() > 992 && transposed != "desktop"){
transposed = "desktop"
transposeTable();
}
})
什麼是做工精細在'if'和'else'上調用'transposeTable()'的用法?你現在面臨什麼問題? –
'var isTransposed = false;''function transposeTable(){if(isTransposed)return; isTransposed = true; ...' –