简体   繁体   English

jQuery:当滚动条出现时如何触发Windows调整大小事件

[英]jQuery: How to trigger windows resize event when scrollbar appears

When vertical scrollbar appears on the page, windows resize event need to be triggered. 当页面上出现垂直滚动条时,需要触发Windows调整大小事件。 Is there any way to capture scrollbar appearance event using javascript? 有没有办法使用javascript捕获滚动条出现事件?

I'm having problem with the width of the page as it makes div to jump to next line when the vertical scrollbar appears. 我的页面宽度有问题,因为当垂直滚动条出现时,它会使div跳到下一行。 It seems to work fine when I resize page, so I want to trigger resize event manually when vertical scrollbar appears. 调整页面大小时,它似乎工作正常,因此当垂直滚动条出现时,我想手动触发调整大小事件。

You could use setInterval to monitor for the scrollbar. 您可以使用setInterval监视滚动条。 If the document width exceeds the window width, you can trigger the window.resize event manually. 如果文档宽度超过窗口宽度,则可以手动触发window.resize事件。

function checkForScrollbar() {
    if ($(window).width() < $(document).width()) {
        $(window).trigger('resize');    
    }    
}

$(document).ready(function() {
    setInterval(function() { checkForScrollbar(); }, 500);   

    $(window).on('resize', function() {
        //Resize triggered.
        //Do Your Stuff
    });

});

See this JS Fiddle: http://jsfiddle.net/USvsW/9/ 看到这个JS小提琴: http : //jsfiddle.net/USvsW/9/

OrganicPanda has posted a clever solution without the need for a timer. OrganicPanda发布了一个聪明的解决方案,不需要计时器。 Basically he places an iFrame with 100% somewhere and listens to 'resize' events on it: 基本上,他将100%放置在iFrame上,并在其上监听“调整大小”事件:

Detect when window vertical scrollbar appears 检测何时出现窗口垂直滚动条

u can use this:this will through alert on resize 您可以使用此功能:这将通过调整大小警报

$(window).on('load resize', function(){  
var w1 = $(window).width();
alert(w1);
 }) 

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

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