ScrollSpy
ScrollSpy is a small but powerful MooTools plugin that allows you to listen to scrolling within any DOM element and execute functions based upon the element's scroll position.
MooTools Javascript Class
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code1/*2---3description: ScrollSpy45authors:6 - David Walsh (http://davidwalsh.name)78license:9 - MIT-style license1011requires:12 core/1.2.1: '*'1314provides:15 - ScrollSpy16...17*/18var ScrollSpy = new Class({1920 /* implements */21 Implements: [Options,Events],2223 /* options */24 options: {25 min: 0,26 mode: 'vertical',27 max: 0,28 container: window,29 onEnter: $empty,30 onLeave: $empty,31 onTick: $empty32 },3334 /* initialization */35 initialize: function(options) {36 /* set options */37 this.setOptions(options);38 this.container = document.id(this.options.container);39 this.enters = this.leaves = 0;40 this.max = this.options.max;4142 /* fix max */43 if(this.max == 0) {44 var ss = this.container.getScrollSize();45 this.max = this.options.mode == 'vertical' ? ss.y : ss.x;46 }47 /* make it happen */48 this.addListener();49 },5051 /* a method that does whatever you want */52 addListener: function() {53 /* state trackers */54 this.inside = false;55 this.container.addEvent('scroll',function() {56 /* if it has reached the level */57 var position = this.container.getScroll();58 var xy = this.options.mode == 'vertical' ? position.y : position.x;59 /* if we reach the minimum and are still below the max... */60 if(xy >= this.options.min && xy <= this.max) {61 /* trigger Enter event if necessary */62 if(!this.inside) {63 /* record as inside */64 this.inside = true;65 this.enters++;66 /* fire enter event */67 this.fireEvent('enter',[position,this.enters]);68 }69 /* trigger the "tick", always */70 this.fireEvent('tick',[position,this.inside,this.enters,this.leaves]);71 }72 else {73 /* trigger leave */74 if(this.inside) {75 this.inside = false;76 this.leaves++;77 this.fireEvent('leave',[position,this.leaves]);78 }79 }80 }.bind(this));81 }82});Class: ScrollSpy
Implements:
Options, Events
ScrollSpy Method: constructor
Notes:
- ScrollSpy debuted Wednesday, May 27, 2009 on the David Walsh Blog: http://davidwalsh.name/scrollspy
- ScrollSpy requires MooTools Core only -- no MooTools More dependencies.
- Visit http://davidwalsh.name/scrollspy for example usages.
Syntax:
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code1var myScrollSpy = new ScrollSpy(options);Arguments:
- options - (object, optional) Initial options for the class.
Options:
- min - (integer, defaults to 0) The minimum value of the X or Y coordinate, depending on mode.
- mode - (string, defaults to 'vertical') Defines whether to listen to X or Y scrolling.
- max - (integer, defaults to 0) The maximum value of the X or Y coordinate, depending on mode.
- container - (element, defaults to window) The element whose scrolling to listen to.
Returns:
- (object) A new ScrollSpy instance.
Events:
enter
- (function) Function to execute when the element's designated area is scrolled into.
Signature
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code1onEnter(position, enters)Arguments:
- position - (object) The container's scroll position object.
- enters - (integer) The number of times the scroll area has been entered.
leave
- (function) Function to execute when the element's designated area is scrolled out of.
Signature
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code1onLeave(position, enters)Arguments:
- position - (object) The container's scroll position object.
- enters - (integer) The number of times the scroll area has been left.
tick
- (function) Function to execute on each scroll event when the scroll position is between the enter and exit.
Signature
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code1onTick(position, inside, enters, leave)Arguments:
-->
- position - (object) The container's scroll position object.
- inside - (boolean) Represents whether or not the scroll is within the designated min and max area.
- enters - (integer) The number of enters.
- leaves - (integer) The number of leaves.
via davidwalsh.name
No comments:
Post a Comment