Created
March 30, 2012 05:24
-
-
Save x86ed/2246806 to your computer and use it in GitHub Desktop.
a parallax navigation jQuery plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function( $ ){ | |
var constants = {//static vars don't change this unless you're rewriting the plugin. | |
defaultMethod: 'init', | |
version: '0.0.5', | |
name: 'parallax.js', | |
author: 'Adam Siegel' | |
}; | |
var settings = {//default settigns overidden by arguments given during instantiation. | |
debug: false, | |
lazyLoad: true, | |
target: 'default', | |
index: 0, | |
mediaIndex: 0, | |
locationHashes: ["#home",["#media-gallery","#media-gallery/video/video-1","#media-gallery/video/video-2","#media-gallery/video/video-3","#media-gallery/video/video-4","#media-gallery/video/video-5","#media-gallery/image/screenshot-1","#media-gallery/image/screenshot-2","#media-gallery/image/screenshot-3","#media-gallery/image/screenshot-4", "#media-gallery/image/screenshot-5", "#media-gallery/image/screenshot-6"],"#game-info","#characters","#characters-2","#characters-3","#characters-4","#characters-5","#characters-5"], | |
timing: 3000, | |
varSpeed: true, | |
negMargin: 110 // number in px to pull in margin per parallaxed layer | |
}; | |
var methods = { | |
info: function info(){// version info and author info for debugging | |
return constants.name + " ver: " + constants.version + " by " + constants.author; | |
}, | |
debugMsg: function debugMessage(massage,caller){// prevents console.logs from ending up in the code when debugging, YAY! | |
caller = typeof(caller) != 'undefined' ? caller : debugMessage.caller; | |
if(settings.debug){ | |
var outputName = caller.name?caller.name + "::":""; | |
output = constants.name + ":" + outputName + massage; | |
try { | |
console.log(output); | |
} | |
catch(e) { | |
try { | |
opera.postError(output); | |
} | |
catch(e){ | |
alert(output); | |
} | |
} | |
if(typeof(console) === 'undefined') { | |
console = {} | |
console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {}; | |
} | |
} | |
}, | |
init : function init( options ) {//init method | |
return this.each(function(){ | |
$instance = this; | |
$instance.options = $.extend({}, settings, options); | |
if(location.hash.length){//assigns the current location.hash to the current index | |
$.inArray(location.hash,$instance.options.locationHashes)>-1?$instance.options.index=$.inArray(location.hash,$instance.options.locationHashes):$instance.options.index; | |
} | |
if(location.hash.length){//assigns the current location.hash to the current index | |
$.inArray(location.hash,$instance.options.locationHashes[1])>-1?$instance.options.index=1:$instance.options.index; | |
} | |
$instance.properties = { | |
$zfix: $(".videoplayer , #info div.plane-4", $instance), | |
$next: $(".parallax-next",$instance), | |
$prev: $(".parallax-prev",$instance), | |
$moveTo: $("li.nav-item"), | |
$preOrder: $("li.nav-item:last-child"), | |
$layers: $("ul.layer", $instance), | |
$firstLayer: $("ul.layer:first-child li.mainelement",$instance), | |
length: $("ul.layer:first-child li.mainelement",$instance).length | |
}; | |
var scrollerScreenOffset = (0-$(".transition").width()) + "px"; | |
methods.debugMsg("scrollerScreenOffset == " +scrollerScreenOffset, methods.init); | |
$instance.properties.$layers.css("left",scrollerScreenOffset); | |
$instance.properties.$next.click(function(e){ | |
e.preventDefault(); | |
methods.next() | |
}); | |
$instance.properties.$prev.click(function(e){ | |
e.preventDefault(); | |
methods.prev() | |
}); | |
$instance.properties.$moveTo.click(function(e){ | |
e.preventDefault(); | |
methods.moveTo($instance.options.index,$(this).index()); | |
}); | |
$instance.properties.$moveTo.click(function(e){ | |
e.preventDefault(); | |
window.location = $("a", this).attr("href"); | |
}); | |
$("body").keydown(function(event){ | |
if(event.keyCode== '37' && $instance.options.index){ | |
event.preventDefault(); | |
methods.prev(); | |
} | |
if(event.keyCode== '39'){ | |
event.preventDefault(); | |
methods.next(); | |
} | |
}); | |
if(location.hash){ | |
if($.inArray(location.hash,$instance.options.locationHashes[1])>-1){ | |
$instance.options.mediaIndex=$.inArray(location.hash,$instance.options.locationHashes[1]) | |
} | |
} | |
methods.moveTo($instance.options.index,$instance.options.index); | |
//diagnostic info | |
methods.debugMsg(methods.info()); | |
methods.debugMsg("$instance.properties.length:: " + $instance.properties.length); | |
}); | |
}, | |
destroy : function destroy() {//unbinds the modal listeners. (useful for optimization) | |
return this.each(function(){ | |
$(window).unbind('.parallax'); | |
}) | |
}, | |
next : function moveNext(){ | |
methods.move(1,1); | |
}, | |
prev : function movePrev(){ | |
methods.move(-1,1); | |
}, | |
/** | |
* @params action One of 'next' or 'prev' | |
*/ | |
gesture: function gesture(action) { | |
if(action == 'prev') { | |
if($instance.options.index > 0){methods.prev();} | |
} | |
else methods.next(); | |
return; | |
}, | |
move: function move(direction,amount,instant){ | |
if($instance.options.lazyLoad){ | |
methods.lazyload(direction,amount); | |
}; | |
instant = typeof(instant) != 'undefined' ? instant : false;//default argument in javascript | |
methods.debugMsg("$instance.properties.length::" + $instance.properties.length); | |
methods.debugMsg("$instance.options.index::" + $instance.options.index); | |
$instance.properties.$layers.each(function(index) { | |
if($instance.options.index + (direction * amount)>-1 && $instance.options.index + (direction * amount) < $instance.properties.length){ | |
methods.debugMsg("left::" + $($(this).find("li.mainelement")[$instance.options.index + (direction * amount)]).position().left); | |
methods.debugMsg($instance.options.index + (direction * amount),methods.move); | |
if($instance.options.negMargin){ | |
$(this).find("li.mainelement, li.transition").css("margin-left", "-" + $instance.options.negMargin*$(this).attr("rel") + "px") | |
} | |
if(instant){ | |
$(this).css({ left : -$($(this).find("li.mainelement")[$instance.options.index + (direction * amount)]).position().left + $instance.options.negMargin*$(this).attr("rel") }, $instance.options.timing).dequeue(); | |
}else{ | |
$("#HomeVideoPlayer").videoplayer("pause"); | |
$("#MediaVideoPlayer").videoplayer("pause"); | |
$(this).animate({ left : -$($(this).find("li.mainelement")[$instance.options.index + (direction * amount)]).position().left + $instance.options.negMargin*$(this).attr("rel") }, $instance.options.timing).dequeue(); | |
} | |
}else if ($instance.options.index + (direction * amount) >= $instance.properties.length){ | |
methods.debugMsg("second case",methods.move) | |
if($instance.options.negMargin){ | |
$(this).find("li.mainelement, li.transition").css("margin-left", "-" + $instance.options.negMargin*$(this).attr("rel") + "px") | |
} | |
methods.debugMsg("pass",methods.move) | |
if(instant){ | |
$(this).css({ left : -$($(this).find("li.mainelement")[$instance.properties.length -1]).next().position().left + $instance.options.negMargin*$(this).attr("rel") }, $instance.options.timing).dequeue(); | |
}else{ | |
$("#HomeVideoPlayer").videoplayer("pause"); | |
$("#MediaVideoPlayer").videoplayer("pause"); | |
$(this).animate({ left : -$($(this).find("li.mainelement")[$instance.properties.length -1]).next().position().left + $instance.options.negMargin*$(this).attr("rel") }, $instance.options.timing).dequeue(); | |
} | |
} | |
}); | |
if($instance.options.index >= 0){ | |
$instance.options.index += (direction*amount); | |
} | |
methods.updateNav(); | |
}, | |
moveTo: function moveTo(startAt,endAt){// this triggers methods.move to and from a destination | |
var amt = Math.abs(endAt-startAt); | |
var dir = endAt-startAt>0?1:-1; | |
if(endAt-startAt){ | |
methods.move(dir,amt); | |
}else{ | |
methods.move(dir,amt,true); | |
} | |
}, | |
updateNav: function updateNav(){//makes sure the navigation buttons are displaying properly after a scroll, updates slider and sets link to preorder page when nessicary. | |
$instance.properties.$zfix.css("z-index", "8"); | |
if($instance.options.index>-1){ | |
if($instance.options.index == 1){ | |
location.hash = $instance.options.locationHashes[1][$instance.options.mediaIndex] | |
}else{ | |
location.hash = $instance.options.locationHashes[$instance.options.index]; | |
} | |
} | |
sectCount = $instance.options.index; | |
//animcount(); | |
$instance.properties.$moveTo.find("a").removeClass("selected"); | |
if(!$instance.options.index){ | |
$($instance.properties.$moveTo[0]).find("a").addClass("selected"); | |
$(".nav-anime").animate({left : "60px" }); | |
$instance.properties.$prev.css("display","none"); | |
}else if($instance.options.index > $instance.properties.$moveTo.length + 2 ){ | |
$($instance.properties.$moveTo[$instance.properties.$moveTo.length-1]).find("a").addClass("selected"); | |
window.location = $instance.properties.$next.attr("href"); | |
$(".nav-anime").animate({left : "523px" }); | |
}else{ | |
var pOffset = $instance.options.index > $instance.properties.$moveTo.length-2?$instance.properties.$moveTo.length-2:$instance.options.index; | |
$($instance.properties.$moveTo[pOffset]).find("a").addClass("selected"); | |
var destination = $($instance.properties.$moveTo[pOffset]).position().left + 20 + "px"; | |
$(".nav-anime").animate({left : destination }); | |
$instance.properties.$next.css("display","block"); | |
$instance.properties.$prev.css("display","block"); | |
} | |
}, | |
lazyload: function lazyLoad(direction,amount){// loads images with the class lazy on demand. | |
$instance.properties.$layers.each(function(index) { | |
$($(this).find("li.mainelement").slice(0,$instance.options.index + 2 + (direction * amount))) | |
.find("img.lazy").each(function(index){ | |
$(this).attr("src",$(this).attr("rel")).removeClass("lazy"); | |
}) | |
}) | |
} | |
}; | |
$.fn.parallax = function(params){ | |
params = Array.prototype.slice.call(arguments); | |
if ( !params ){// stupid anon function opt arg hack. | |
params = [] | |
} | |
if ( params[1] ){// extends the default settings with the parameters fed to the function | |
$.extend( settings, params[1] ); | |
} | |
if (settings.target == 'default'){// hacky way to ensure this pulls this out of the invoked scope and not the whole DOM. | |
settings.target = this | |
} | |
if ( !params[0] ){// assigns a default method if there is no param value | |
params = constants.defaultMethod; | |
} | |
if( typeof params === 'string' || params === 'function'){// assigns method based on params type | |
method = params; | |
}else if ( typeof params === 'object'){ | |
method = params[0]; | |
}else{ | |
method = constants.defaultMethod; | |
} | |
if ( methods[method] ) {// applies methods listed above | |
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 )); | |
} else if ( typeof method === 'object' || ! method ) { | |
return methods.init.apply( this, arguments ); | |
} else { | |
$.error( method + '?! What the heck is ' + method + '?!!! ' + constants.name + ' doesn\'t have a ' + method +' method.' ); | |
} | |
}; | |
})( jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment