【问题标题】:live browser size detection实时浏览器大小检测
【发布时间】:2013-01-19 10:16:49
【问题描述】:

此代码仅根据加载时浏览器的大小起作用,只是想知道我可以为它实现什么以获得当前浏览器大小并根据当前信息工作。

我曾尝试将其包装在 resize() 中,但它会导致其行为异常,即切换开关连续打开和关闭,或者在缩小的浏览器中加载时它根本不起作用。

它是一个响应式网站,页脚菜单只是大屏幕上的静态链接,但在小屏幕上变成下拉菜单。

    var myWidth = 0, myHeight = 0;

    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }   

    if(myWidth < 980) {
        $("#footer h3").click(function () {
                $(this).toggleClass("active");
                $(this).parent().find("ul").slideToggle('medium');      
        });
    }

【问题讨论】:

    标签: javascript jquery css responsive-design


    【解决方案1】:

    试试这样的:

    var waitForFinalEvent = (function () {
        var timers = {};
        return function (callback, ms, uniqueId) {
            if (!uniqueId) uniqueId = "Don't call this twice";
            if (timers[uniqueId]) clearTimeout (timers[uniqueId]);
            timers[uniqueId] = setTimeout(callback, ms);
        };
    })();
    
    $(window).resize(function(){
        waitForFinalEvent(function(){
            // put all your code here 
        }, 20, "resize"); // replace 20 with every milliseconds to execute during
                          // resize
    })
    

    您输入的代码将在每次调整窗口大小时执行,仅使用resize() 并不总是有效,因为它不一定会在您调整大小时进行检查。

    【讨论】:

      【解决方案2】:

      您应该改用 css 媒体查询:

      @media only screen and (max-device-width: 980px) {
         // css goes here...   
      }
      

      或包含条件样式表:

      <link rel="stylesheet" type="text/css" media="only screen and (max-device-width: 480px)" href="small-device.css" />
      

      Here is a great article disusing responsive design

      【讨论】:

      【解决方案3】:
      var myWidth = 0, myHeight = 0;
      
      
      function getSize(){
      
          if( typeof( window.innerWidth ) == 'number' ) {
              //Non-IE
              myWidth = window.innerWidth;
              myHeight = window.innerHeight;
          } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
              //IE 6+ in 'standards compliant mode'
              myWidth = document.documentElement.clientWidth;
              myHeight = document.documentElement.clientHeight;
          }
      
      }
      
      getSize(); // run first time
      
      $(window).resize(function(){
        getSize(); // do it on resize
      });
      
      
      $("#footer h3").click(function () {
      
              getSize();  // not needed but good to have
      
              if(myWidth < 980) {
                  $(this).toggleClass("active");
                  $(this).parent().find("ul").slideToggle('medium');
              }
      
      });
      

      【讨论】:

      • 谢谢,这很好,我遇到了另一个问题,即使我回到大屏幕尺寸,切换仍然保持不变,但我想我可以自己解决这个问题。
      猜你喜欢
      • 2012-09-28
      • 2010-10-18
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多