跨浏览器窗口调整大小事件 - JavaScript / jQuery

发布于 2024-07-14 04:28:15 字数 160 浏览 7 评论 0原文

在 Firefox 中使用窗口调整大小事件的正确(现代)方法是什么,WebKit和 Internet Explorer?

您可以打开/关闭两个滚动条吗?

What is the correct (modern) method for tapping into the window resize event that works in Firefox, WebKit, and Internet Explorer?

And can you turn both scrollbars on/off?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

绮筵 2024-07-21 04:28:15

jQuery 有一个内置方法

$(window).resize(function () { /* do something */ });

为了 UI 响应能力,您可以考虑使用setTimeout 仅在几毫秒后调用您的代码,如以下示例所示,灵感来自 这个

function doSomething() {
    alert("I'm done resizing for the moment");
};

var resizeTimer;
$(window).resize(function() {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(doSomething, 100);
});

jQuery has a built-in method for this:

$(window).resize(function () { /* do something */ });

For the sake of UI responsiveness, you might consider using a setTimeout to call your code only after some number of milliseconds, as shown in the following example, inspired by this:

function doSomething() {
    alert("I'm done resizing for the moment");
};

var resizeTimer;
$(window).resize(function() {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(doSomething, 100);
});
jJeQQOZ5 2024-07-21 04:28:15
$(window).bind('resize', function () { 

    alert('resize');

});
$(window).bind('resize', function () { 

    alert('resize');

});
莫相离 2024-07-21 04:28:15

以下是利用调整大小事件的非 jQuery 方法:

window.addEventListener('resize', function(event){
  // do stuff here
});

它适用于所有现代浏览器。 它不会为您限制任何内容。 这是一个实际的示例

Here is the non-jQuery way of tapping into the resize event:

window.addEventListener('resize', function(event){
  // do stuff here
});

It works on all modern browsers. It does not throttle anything for you. Here is an example of it in action.

め七分饶幸 2024-07-21 04:28:15

很抱歉提出一个旧线程,但如果有人不想使用 jQuery,你可以使用这个:

function foo(){....};
window.onresize=foo;

Sorry to bring up an old thread, but if someone doesn't want to use jQuery you can use this:

function foo(){....};
window.onresize=foo;
记忆之渊 2024-07-21 04:28:15

由于您对 jQuery 持开放态度,

Since you are open to jQuery, this plugin seems to do the trick.

涫野音 2024-07-21 04:28:15

使用 jQuery 1.9.1 我刚刚发现,虽然技术上相同)*,但这在 IE10 中不起作用(但在 Firefox 中):

// did not work in IE10
$(function() {
    $(window).resize(CmsContent.adjustSize);
});

而这在两种浏览器中都有效:

// did work in IE10
$(function() {
    $(window).bind('resize', function() {
        CmsContent.adjustSize();
    };
});

编辑:
)* 实际上在技术上相同,正如 WraithKenny亨利·布莱斯

Using jQuery 1.9.1 I just found out that, although technically identical)*, this did not work in IE10 (but in Firefox):

// did not work in IE10
$(function() {
    $(window).resize(CmsContent.adjustSize);
});

while this worked in both browsers:

// did work in IE10
$(function() {
    $(window).bind('resize', function() {
        CmsContent.adjustSize();
    };
});

Edit:
)* Actually not technically identical, as noted and explained in the comments by WraithKenny and Henry Blyth.

缱倦旧时光 2024-07-21 04:28:15

jQuery 默认提供 $(window).resize() 函数:

<script type="text/javascript">
// function for resize of div/span elements
var $window = $( window ),
    $rightPanelData = $( '.rightPanelData' )
    $leftPanelData = $( '.leftPanelData' );

//jQuery window resize call/event
$window.resize(function resizeScreen() {
    // console.log('window is resizing');

    // here I am resizing my div class height
    $rightPanelData.css( 'height', $window.height() - 166 );
    $leftPanelData.css ( 'height', $window.height() - 236 );
});
</script> 

jQuery provides $(window).resize() function by default:

<script type="text/javascript">
// function for resize of div/span elements
var $window = $( window ),
    $rightPanelData = $( '.rightPanelData' )
    $leftPanelData = $( '.leftPanelData' );

//jQuery window resize call/event
$window.resize(function resizeScreen() {
    // console.log('window is resizing');

    // here I am resizing my div class height
    $rightPanelData.css( 'height', $window.height() - 166 );
    $leftPanelData.css ( 'height', $window.height() - 236 );
});
</script> 
失眠症患者 2024-07-21 04:28:15

我认为 jQuery 插件“jQuery resize event”是对此的最佳解决方案,因为它负责限制事件,以便它在所有浏览器中都相同。 它与安德鲁斯答案类似,但更好,因为您可以将调整大小事件挂接到特定元素/选择器以及整个窗口。 它为编写干净的代码开辟了新的可能性。

该插件位于此处

如果添加大量侦听器,则会出现性能问题,但对于大多数使用情况来说,它是完美的。

I consider the jQuery plugin "jQuery resize event" to be the best solution for this as it takes care of throttling the event so that it works the same across all browsers. It's similar to Andrews answer but better since you can hook the resize event to specific elements/selectors as well as the entire window. It opens up new possibilities to write clean code.

The plugin is available here

There are performance issues if you add a lot of listeners, but for most usage cases it's perfect.

只怪假的太真实 2024-07-21 04:28:15

我认为你应该对此添加进一步的控制:

    var disableRes = false;
    var refreshWindow = function() {
        disableRes = false;
        location.reload();
    }
    var resizeTimer;
    if (disableRes == false) {
        jQuery(window).resize(function() {
            disableRes = true;
            clearTimeout(resizeTimer);
            resizeTimer = setTimeout(refreshWindow, 1000);
        });
    }

I think you should add further control to this:

    var disableRes = false;
    var refreshWindow = function() {
        disableRes = false;
        location.reload();
    }
    var resizeTimer;
    if (disableRes == false) {
        jQuery(window).resize(function() {
            disableRes = true;
            clearTimeout(resizeTimer);
            resizeTimer = setTimeout(refreshWindow, 1000);
        });
    }
最好是你 2024-07-21 04:28:15

希望它对 jQuery 有所帮助

首先定义一个函数,如果有现有函数,请跳到下一步。

 function someFun() {
 //use your code
 }

浏览器调整大小使用如下。

 $(window).on('resize', function () {
    someFun();  //call your function.
 });

hope it will help in jQuery

define a function first, if there is an existing function skip to next step.

 function someFun() {
 //use your code
 }

browser resize use like these.

 $(window).on('resize', function () {
    someFun();  //call your function.
 });
逐鹿 2024-07-21 04:28:15

除了提到的窗口调整大小函数之外,重要的是要了解,如果在没有消抖事件的情况下使用调整大小事件,则会触发很多事件。

Paul Irish 有一个出色的功能,可以大量消除调整大小调用。 非常推荐使用。 跨浏览器工作。 前几天在IE8下测试了一下,一切正常。

http://www.paulirish.com/2009/throttled-smartresize- jquery-event-handler/

请务必查看演示以了解差异。

这是为了完整性而设计的函数。

(function($,sr){

  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;

      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null;
          };

          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);

          timeout = setTimeout(delayed, threshold || 100);
      };
  }
  // smartresize 
  jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };

})(jQuery,'smartresize');


// usage:
$(window).smartresize(function(){
  // code that takes it easy...
});

Besides the window resize functions mentioned it is important to understand that the resize events fire a lot if used without a deboucing the events.

Paul Irish has an excellent function that debounces the resize calls a great deal. Very recommended to use. Works cross-browser. Tested it in IE8 the other day and all was fine.

http://www.paulirish.com/2009/throttled-smartresize-jquery-event-handler/

Make sure to check out the demo to see the difference.

Here is the function for completeness.

(function($,sr){

  // debouncing function from John Hann
  // http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  var debounce = function (func, threshold, execAsap) {
      var timeout;

      return function debounced () {
          var obj = this, args = arguments;
          function delayed () {
              if (!execAsap)
                  func.apply(obj, args);
              timeout = null;
          };

          if (timeout)
              clearTimeout(timeout);
          else if (execAsap)
              func.apply(obj, args);

          timeout = setTimeout(delayed, threshold || 100);
      };
  }
  // smartresize 
  jQuery.fn[sr] = function(fn){  return fn ? this.bind('resize', debounce(fn)) : this.trigger(sr); };

})(jQuery,'smartresize');


// usage:
$(window).smartresize(function(){
  // code that takes it easy...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文