I had to solve the same problem a while ago, and so far the most correct solution I have found is to use media queries to transfer the actual size of the window to Javascript. You must follow these steps:
- Add a hidden element to your page,
- Use media queries to change the
max-width property of this element, - Read the
max-width property of this element through Javascript.
For example, add the following element to the page:
<div id="currentMedia"></div>
Then write the following CSS rules:
#currentMedia { display: none; } @media (max-width: 720px) { #currentMedia { max-width: 720px; } }
Then from the Javascript side you can write:
if (parseInt(jQuery("#currentMedia").css("max-width"), 10) <= 720) {
And that will be accurate, regardless of the size of the scrollbar, since the value comes from the same media query that causes the carousel to disappear.
I tested this solution in all major recent browsers and gave the correct results.
source share