Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 15th, 2012  |  syntax: JavaScript  |  size: 3.62 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // when the DOM is ready...
  2. $(document).ready(function () {
  3.  
  4.     var $panels = $('#slider .scrollContainer > div');
  5.     var $container = $('#slider .scrollContainer');
  6.  
  7.     // if false, we'll float all the panels left and fix the width
  8.     // of the container
  9.     var horizontal = true;
  10.        
  11.     // float the panels left if we're going horizontal
  12.     if (horizontal) {
  13.         $panels.css({
  14.             'float' : 'left',
  15.             'position' : 'relative' // IE fix to ensure overflow is hidden
  16.         });
  17.  
  18.         // calculate a new width for the container (so it holds all panels)
  19.         $container.css('width', $panels[0].offsetWidth * $panels.length);
  20.     }
  21.  
  22.     // collect the scroll object, at the same time apply the hidden overflow
  23.     // to remove the default scrollbars that will appear
  24.     var $scroll = $('#slider .scroll').css('overflow', 'hidden');
  25.  
  26.     // apply our left + right buttons
  27.     //$scroll
  28.        // .before('<img class="scrollButtons left" src="../javasccript/images/scroll_left.png" />')
  29.         //.after('<img class="scrollButtons right" src="../javasccript/images/scroll_right.png" />');
  30.  
  31.     // handle nav selection
  32.     function selectNav() {
  33.         $(this)
  34.             .parents('ul:first')
  35.                 .find('a')
  36.                     .removeClass('selected')
  37.                 .end()
  38.             .end()
  39.             .addClass('selected');
  40.     }
  41.  
  42.     $('ul.navigation').find('a').click(selectNav);
  43.  
  44.     // go find the navigation link that has this target and select the nav
  45.     function trigger(data) {
  46.         var el = $('ul.navigation').find('a[href$="' + data.id + '"]').get(0);
  47.         selectNav.call(el);
  48.     }
  49.  
  50.     if (window.location.hash) {
  51.         trigger({ id : window.location.hash.substr(1) });
  52.     } else {
  53.         $('ul.navigation a:first').click();
  54.     }
  55.  
  56.     // offset is used to move to *exactly* the right place, since I'm using
  57.     // padding on my example, I need to subtract the amount of padding to
  58.     // the offset.  Try removing this to get a good idea of the effect
  59.    var offset = parseInt((horizontal ?
  60.         $container.css('paddingTop') :
  61.         $container.css('paddingLeft'))
  62.         || 0) * -1;
  63.  
  64.  
  65.     var scrollOptions = {
  66.         target: $scroll, // the element that has the overflow
  67.  
  68.         // can be a selector which will be relative to the target
  69.         items: $panels,
  70.  
  71.         navigation: '.navigation a',
  72.  
  73.         // selectors are NOT relative to document, i.e. make sure they're unique
  74.         prev: 'img.left',
  75.         next: 'img.right',
  76.  
  77.         // allow the scroll effect to run both directions
  78.         axis: 'xy',
  79.  
  80.         onAfter: trigger, // our final callback
  81.  
  82.         offset: offset,
  83.  
  84.         // duration of the sliding effect
  85.         duration: 500,
  86.  
  87.         // easing - can be used with the easing plugin:
  88.         // http://gsgd.co.uk/sandbox/jquery/easing/
  89.         easing: 'swing'
  90.     };
  91.  
  92.     // apply serialScroll to the slider - we chose this plugin because it
  93.     // supports// the indexed next and previous scroll along with hooking
  94.     // in to our navigation.
  95.     $('#slider').serialScroll(scrollOptions);
  96.  
  97.     // now apply localScroll to hook any other arbitrary links to trigger
  98.     // the effect
  99.     $.localScroll(scrollOptions);
  100.  
  101.     // finally, if the URL has a hash, move the slider in to position,
  102.     // setting the duration to 1 because I don't want it to scroll in the
  103.     // very first page load.  We don't always need this, but it ensures
  104.     // the positioning is absolutely spot on when the pages loads.
  105.     scrollOptions.duration = 1;
  106.     $.localScroll.hash(scrollOptions);
  107.  
  108. });