【问题标题】:Window resize trigger event is not working in IE11窗口调整大小触发事件在 IE11 中不起作用
【发布时间】:2015-08-14 19:03:17
【问题描述】:

我想用代码调用resize事件。

我正在使用以下代码。它在其他浏览器中运行良好,但在 IE11 中运行良好。

if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
            $(window).trigger('resize');

        } else {
            window.dispatchEvent(new Event('resize'));

        }

请指教,我有什么遗漏吗?

【问题讨论】:

  • 只需使用$(window).trigger('resize') 它也可以在 IE 11 中使用
  • @Satpal nope,它不工作:(
  • @rightPath 提供简约样本来复制您的问题本身。所以发布代码绑定事件你试图触发或更好地解释你在寻找什么,不要只说it is not working...

标签: javascript jquery html cross-browser


【解决方案1】:

我遇到的另一篇帖子How to trigger the window resize event in JavaScript?的解决方案效果很好。

相同的片段:

if (typeof(Event) === 'function') {
    // modern browsers
     window.dispatchEvent(new Event('resize'));
} else {
    // for IE and other old browsers
    // causes deprecation warning on modern browsers
    var evt = window.document.createEvent('UIEvents'); 
    evt.initUIEvent('resize', true, false, window, 0); 
    window.dispatchEvent(evt);
}

【讨论】:

    【解决方案2】:

    试试这个

    var resizeEvent = window.document.createEvent('UIEvents'); 
    resizeEvent .initUIEvent('resize', true, false, window, 0); 
    window.dispatchEvent(resizeEvent);

    【讨论】:

      【解决方案3】:

      即使是 Internet explorer 11 也不支持调整大小事件。因此,我使用以下解决方案解决了这个问题。

      if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0) {
           var evt = document.createEvent('UIEvents');
           evt.initUIEvent('resize', true, false, window, 0);
           window.dispatchEvent(evt);
          } else {
             window.dispatchEvent(new Event('resize'));
      
          }
      

      【讨论】:

      • 感谢 Shivek - 这是在尝试了许多其他可能的解决方案后唯一对我有用的答案。关键是使用 UIEvents 而不是 HTMLEvents
      • 我的代码适用于除 IE 之外的所有内容,所以需要 else {} 吗?谢谢,
      • 这很好,这是唯一不会导致控制台出现很多错误的解决方案
      猜你喜欢
      • 2023-03-10
      • 2021-10-11
      • 1970-01-01
      • 2016-10-31
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多