Capture document width with scroll bar

Asked

Viewed 161 times

0

Good afternoon,

I’m making a responsive site where I use CSS with media="screen" and jQuery.

Problem:
However Javascript and CSS do not recognize the same document width value when the page has some scrollbar (when testing in Chrome using Adaptive Design Mode - Ctrl+Shift+M - works normally) that is, works perfectly on mobile devices and other devices not, because they have the scrollbar.

Example:
If you run a alert ($(window).width()); to display the width of a document with scroll bar on a desktop with 1920/1080 resolution, will bring the following response 1903 instead of 1920

Doubt:
Is there any way to take the width of a website window with scroll or make both Javascript and CSS recognize the width of the document as being the same on the desktop?

  • See the example http://stackoverflow.com/questions/986937/how-can-i-get-the-browsers-scrollbar-sizes/

  • @Kingrider the example you indicated was also very useful, it was in it that I found 'window.innerWidth' that served what I needed. vlw.

1 answer

1


You can solve your problem like this:

        $(function(){
            var hasScroll= document.body.scrollHeight > document.body.clientHeight;
            if (hasScroll) {
                // Aqui são suas definições/regras se existe a barra de rolagem
                var widthPage = ($(window).width() + 17);
                console.log(widthPage);
            }else{
                // Aqui são suas definições/regras se NÃO existe a barra de rolagem
                var widthPage = ($(window).width());
                console.log(widthPage);
            }
        });

With this you take the width of the page + the width of the scroll bar.
With this solution you also check whether or not the scroll bar exists.
I hope I’ve helped.

  • If I do this the problem goes to the mobile devices that are working perfectly. and would have another problem with pages without scroll bar.

  • This is easily solved with JS @Wesley . I updated my answer see if it suits you.

  • It really worked, but I ended up using 'window.innerWidth' which in my case here worked both with bar and without bar, and the same thing for mobile... you think I can leave so by being working or the right would be to use your answer?

  • It seems you didn’t need validation, so your solution is more suitable for your problem.

  • I marked your answer as accepted because it also solves my problem...thanks for the help and attention devoted to my question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.