jQuery finding scrollable elements

JavaScript performance comparison

Revision 2 of this test case created

Info

A custom jquery selector that finds all elements with scrollbars

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="testarea">
        <div style="height: 100px; overflow: auto; ">
                <div>No Scroll</div>
        </div>
        <div style="height: 100px; width: 100px;">
                <div style="height: 150px;">No Overflow</div>
        </div>
        <div style="height: 100px; width: 100px; overflow: auto; ">
                <div style="height: 150px;">Vertical Scroll</div>
        </div>
        <div style="height: 100px; width: 100px; overflow: auto; ">
                <div style="height: 150px;">Horizontal Scroll</div>
        </div>
        <div style="height: 100px; width: 100px; overflow: hidden; ">
                <div>Forced Scroll want see if it also react if we habe lot of new text inside witch is also hidden.</div>
        </div>
</div>
<script>
  (function($) {
   $.extend($.expr[":"], {
    scrollable: function(element) {
     var vertically_scrollable, horizontally_scrollable;
     if ($(element).css('overflow') == 'scroll' || $(element).css('overflowX') == 'scroll' || $(element).css('overflowY') == 'scroll') return true;
 
     vertically_scrollable = (element.clientHeight < element.scrollHeight) && (
     $.inArray($(element).css('overflowY'), ['scroll', 'auto']) != -1 || $.inArray($(element).css('overflow'), ['scroll', 'auto']) != -1);
 
     if (vertically_scrollable) return true;
 
     horizontally_scrollable = (element.clientWidth < element.scrollWidth) && (
     $.inArray($(element).css('overflowX'), ['scroll', 'auto']) != -1 || $.inArray($(element).css('overflow'), ['scroll', 'auto']) != -1);
     return horizontally_scrollable;
    }
   });
  })(jQuery)
</script>

Preparation code output

No Scroll
No Overflow
Vertical Scroll
Horizontal Scroll
Forced Scroll want see if it also react if we habe lot of new text inside witch is also hidden.

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
See if we get the correct scrollable elements
$('#testarea').find(':scrollable').css('border', '2px dotted green');
pending…
See if we get the correct scrollable elements
$('#testarea').find(':scrollable').css('border', '2px dotted green');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment