SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member rickydazla's Avatar
    Join Date
    Jan 2011
    Location
    Detroit, MI
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Detecting horizontal scrollbars

    Hello peoples,

    I have mangled up the following to add fixed position left/right scroll links to a horizontal scrolling site:
    Code JavaScript:
    $(document).ready(function() {
      $('#container').append('<ul class="scroll_controllers"><li class="left"><a href="#" title="Scroll Left">&laquo;</a></li><li class="right"><a href="#" title="Scroll Right">&raquo;</a></li></ul>');
      $('.right a').click( function(){
        $.scrollTo( '+=600px', 900, {axis:'x'} );
        return false;
      });
      $('.left a').click( function() {
        $.scrollTo( '-=600px', 900, {axis:'x'} );
        return false;
      });
    });
    I'd really like to make it conditional on there being a need to scroll. Any idea what the best way to detect this would be?

    Thanks in advance,

    Rick

  2. #2
    SitePoint Zealot Gar onn's Avatar
    Join Date
    Feb 2011
    Location
    Belgium
    Posts
    130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    //scrol 1px to the left
    $(document).scrollLeft(1);
     
    //check if it scrolled
    // if this is 0 than jQuery was not able to scroll to left
    // because there is no posibility to (no scroll bar)
    if($(document).scrollLeft() != 0){
    // x-scroll
    alert('there is a horizontal scroll bar\n you must have a small screen');
    }else{
    // no x-scroll
    alert('there is NO a horizontal scroll bar');
    }
     
    //scroll back to original location
    $(document).scrollLeft(0);

    that should work
    Ind It is quite invisible

    note: only tested it in chrome, but it should work in the others

  3. #3
    SitePoint Member rickydazla's Avatar
    Join Date
    Jan 2011
    Location
    Detroit, MI
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha! That works like a charm. I had been looking at ways to detect the presence of a scroll bar by calculating element sizes vs. window/viewport size etc etc and it was all getting very convoluted! I didn't think to just scroll the damn thing! Thanks!


    Rick


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •