2

When vertical scrollbar appears on the page, windows resize event need to be triggered. Is there any way to capture scrollbar appearance event using 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. It seems to work fine when I resize page, so I want to trigger resize event manually when vertical scrollbar appears.

kkrishnaai
  • 325
  • 1
  • 5
  • 9
  • If you know what's causing the page overflow to occur, you can use that as your event listener (of sorts). – athms Jul 03 '14 at 11:26
  • 2
    Your problem seems to lie entirely elsewhere than what you're trying to solve. Solve the positioning problem, don't write a workaround to an originally bad solution. – Etheryte Jul 03 '14 at 11:26
  • 1
    can you provide any code you have tryed? – Sam Denton Jul 03 '14 at 11:29
  • @athms your solution sounds good, I can have some listeners, but I need to add them in many different places as this page overflow occurs for different reasons. – kkrishnaai Jul 03 '14 at 12:28
  • Possible duplicate of [Scrollbar appear / disappear event in jQuery?](http://stackoverflow.com/questions/2578046/scrollbar-appear-disappear-event-in-jquery) – user Apr 16 '17 at 10:11

3 Answers3

1

You could use setInterval to monitor for the scrollbar. If the document width exceeds the window width, you can trigger the window.resize event manually.

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/

madhippie
  • 168
  • 1
  • 9
  • This solves my problem, but I don't wan't to monitor width size frequently, is there any other way to capture scrollbar appearance event? – kkrishnaai Jul 03 '14 at 12:24
  • As far as i know there is no possibility to monitor the appearence of the scrollbar by any event. If this problem occurs onload, you could put the trigger in the .ready function. If you manipulate the DOM via jQuery you could always provide the checkForScrollbar function in a callback function. I'm afraid I'll need to see some code to give you further advice. – madhippie Jul 03 '14 at 14:38
1

OrganicPanda has posted a clever solution without the need for a timer. Basically he places an iFrame with 100% somewhere and listens to 'resize' events on it:

Detect when window vertical scrollbar appears

Community
  • 1
  • 1
ernesto
  • 1,771
  • 2
  • 18
  • 18
-1

u can use this:this will through alert on resize

$(window).on('load resize', function(){  
var w1 = $(window).width();
alert(w1);
 }) 
aashi
  • 492
  • 3
  • 11