Introducing MooTools NextPrev

By  on  

MooTools NextPrev

One thing I love doing is duplicating OS functionalities. One of the things your OS allows you to do easily is move from one item to another. Most of the time you're simply trying to get to the next or the previous item. MooTools NextPrev is a compact JavaScript class that allows you to move about a collection of items quickly using human terms.

The MooTools Class

var NextPrev = new Class({
	Implements: [Options,Events],
	options: {
		baseEvent: 'keyup',
		eventContainer: document,
		eventCheckNext: function(e){
			return true;
		},
		eventCheckPrevious: function(e){ 
			return true;
		},
		onLoad: $empty,
		onNext: $empty,
		onPrevious: $empty,
		startIndex: 0
	},
	initialize: function(collection,options) {
		this.setOptions(options);
		this.collection = $$(collection);
		if(this.options.container == document) this.options.container = document.body;
		this.index = this.options.startIndex;
		if(this.options.baseEvent) {
			$(this.options.eventContainer).addEvent(this.options.baseEvent,function(e) {
				if(this.options.eventCheckNext(e)) {
					this.move.bind(this)('next');
				}
				else if(this.options.eventCheckPrevious(e)) {
					this.move.bind(this)('previous');
				}
			}.bind(this));
		}
		this.fireEvent('load',[this.collection[this.index]]);
	},
	move: function(norp) {
		var previous = this.index;
		switch($type(norp)) {
			case 'string':
				var ev = 'next';
				if(norp == 'next') {
					var plus = this.index + 1;
					this.index = this.collection[plus] ? plus : 0;
				}
				else {
					var minus = this.index - 1;
					this.index = this.collection[minus] ? minus : this.collection.length-1;
					ev = 'previous';
				}
				this.fireEvent(ev,[this.collection[this.index],this.collection[previous]]);
				break;
			case 'element':
				this.index = this.collection.indexOf(norp) || 0;
				break;
			default:
				this.index = norp;
				break;
		}
		this.fireEvent('change',[this.collection[this.index],this.collection[previous]]);
		return this;
	}
});

The following are arguments, options, and events for NextPrev:

Arguments

  • collection: The collection of elements..
  • options: The instance options.

Options

  • baseEvent: (defaults to 'keyup') The event to listen to.
  • eventContainer: (defaults to document) The event listener container.
  • eventCheckNext: (defaults to function) The function that decides if the conditions to trigger a "next" have been met.
  • eventCheckPrevious: (defaults to function) The function that decides if the conditions to trigger a "previous" have been met.
  • startIndex: (defaults to 0) The starting index for the collection.

Events

  • change: Fired when the index changes.
  • load: Fired every time the class is initialized.
  • next: Fired when the next directive is triggered.
  • previous: Fired when the previous directive is triggered.

Public Methods

  • move: May be passed the Strings "next" or "prev", or a DOM node.

Sample Usage

var np2 = new NextPrev($$('#images img'),{
	eventCheckNext: function(e) {
		if(e) e.stop(e);
		return e.key == 'right';
	},
	eventCheckPrevious: function(e) {
		if(e) e.stop(e);
		return e.key == 'left';
	},
	onNext: function(cur,prev) {
		prev.removeClass('featured');
		cur.addClass('featured');
	},
	onPrevious: function(cur,prev) {
		prev.removeClass('featured');
		cur.addClass('featured');
	},
	onLoad: function(cur) {
		cur.addClass('featured');
	}
});

The code above hijacks the "left" and "right" keys, moving forward and backward in the collection depending on which key was pressed.

NextPrev is extremely simple but also quite flexible. You decide the events, keys, actions, etc -- the plugin simple allows for simple control of position in the list. Have ideas for this class? Know what you'd use it for? Let me know!

Recent Features

  • By
    CSS 3D Folding Animation

    Google Plus provides loads of inspiration for front-end developers, especially when it comes to the CSS and JavaScript wonders they create. Last year I duplicated their incredible PhotoStack effect with both MooTools and pure CSS; this time I'm going to duplicate...

  • By
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

Incredible Demos

  • By
    Spyjax:  Ajax For Evil Using Dojo

    The idea of Spyjax is nothing new. In pasts posts I've covered how you can spy on your user's history with both MooTools and jQuery. Today we'll cover how to check user history using the Dojo Toolkit. The HTML For the sake of this...

  • By
    Redacted Font

    Back when I created client websites, one of the many things that frustrated me was the initial design handoff.  It would always go like this: Work hard to incorporate client's ideas, dream up awesome design. Create said design, using Lorem Ipsum text Send initial design concept to the client...

Discussion

  1. Hey David this is really cool but using keypress instead of keyup is way slicker.

  2. @rickyH: Good thought, I may change that.

  3. I love this David BUT Where is Christina Ricci????
    :)

    @rickyH that’s why it’s an option

  4. Alex

    Yeah, where is CR ????!!!!

    keypress > subscribing.

    I’d also like to keep “down” pressed and see how it keeps going down, not only 1 item.

    Nice.

  5. That’s a cool one again! THX!
    I liked to see this nice use-cases for key-events.

  6. keypress wont work for up and down keys on some browsers.
    You could use this keyrepeat custom event:

    // the key event that repeats when you press a key
    Element.Events.keyrepeat = {
    	base : (Browser.Engine.gecko || Browser.Engine.presto) ? 'keypress' : 'keydown',
    	condition: $lambda(true)
    };
    

    hope its usefull.

  7. I am a jQuery user but I must say this is a GREAT tutorial, any chance of getting this ported over to jQuery in a tutorial for us? Would be very greatful!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!