简体   繁体   English

滚动条出现或消失时,为什么调整大小事件不会触发?

[英]Why resize event doesn't fire when scrollbar appears or disappears?

When browser's vertical scrollbar appears or disappears, the width of viewport or browser window changes (can be tested using jQuery's $(window).width() method), but window's resize event is not triggered. 当浏览器的垂直滚动条出现或消失时,视口或浏览器窗口的宽度会发生变化(可以使用jQuery的$(window).width()方法进行测试),但不会触发窗口的resize事件。 How come? 怎么会?

Re-size is an event that is driven by the actual browser window changing size. 重新调整大小是由实际浏览器窗口更改大小驱动的事件。

What if I removed elements from my page until my content fit within the screen ? 如果我从页面中删除元素直到我的内容适合屏幕,该怎么办? That's not a window re-size. 这不是一个窗口重新调整大小。 Or if i change the overflow for the page to hidden. 或者,如果我将页面的溢出更改为隐藏。 The scroll bars will disappear, however this again is not a re size. 滚动条将消失,但这又不是一个大小。

What I am getting at is this: Scrollbar visibility does not necessarily mean that there was a re-size event. 我得到的是:滚动条可见性并不一定意味着有一个重新调整大小的事件。

而不是调整大小,使用overflowchanged事件。

Browsers don't recognize it as resize. 浏览器不会将其识别为调整大小。 Then if you need "scrollbar appeared" and "scrollbar disappeared" events, use this code: 然后,如果您需要“滚动条出现”和“滚动条消失”事件,请使用以下代码:

<div id="footerDiv" style="float: left; height: 1px; width: 100%;"></div>



bodywidth = 0;

$(document).ready(function () {

    bodywidth = $("#footerDiv").width();
    setInterval(scrollbarHelper, 100);

});

function scrollbarHelper() { 

    var newwidth = $("#footerDiv").width();

    if (bodywidth !== newwidth) {

        if (bodywidth > newwidth) {
            alert("Scrollbar Appeared");
            // Your code here
        }
        else if (bodywidth < newwidth) {
            alert("Scrollbar Disppeared");
            // Your code here
        }

        bodywidth = newwidth;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM