jQuery Run函数只在窗口大小调整一次

时间:2017-03-02 10:53:47

标签: javascript 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();
    }
})

2 个答案:

答案 0 :(得分:1)

您必须存储已经调用from matplotlib import pyplot as plt from matplotlib import pylab as pl pl.plot(matrix[:,0],matrix[:,1], 'ro') 函数的内容,并且应该使用其他函数将表转置到桌面... 例如:

transposeTable

答案 1 :(得分:0)

如果这有用,可以满足我的需求

&#13;
&#13;
var x;
$(window).resize(function() {
  if ($(this).width() <= 600 && (x === 2 || x === undefined)) {
    if(x !== undefined){
      //function here
      $("body").append('size: small<br/>');
    }
    x = 1;
  }
  else if ($(this).width() > 600 && $(this).width() <= 1140 && (x === 1 || x === 3 || x === undefined)){
    if(x !== undefined){
      //function here
      $("body").append('size: medium<br/>');
    }
    x = 2;
  }
  else if ($(this).width() > 1140 && (x === 2 || x === undefined)){
    if(x !== undefined){
      //function here
      $("body").append('size: large<br/>');
    }
    x = 3;
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;