/*!
* Name : Just Another Parallax [Jarallax]
* Version : 1.1.0
* Author : _nK http://nkdev.info
* GitHub : https://github.com/nk-o/jarallax
*/
(function(factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory(require('jquery'));
} else {
factory(jQuery);
}
}(function($) {
// Adapted from https://gist.github.com/paulirish/1579671
if (!Date.now)
Date.now = function() { return new Date().getTime(); };
if(!window.requestAnimationFrame)
(function() {
'use strict';
var vendors = ['webkit', 'moz'];
for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
var vp = vendors[i];
window.requestAnimationFrame = window[vp+'RequestAnimationFrame'];
window.cancelAnimationFrame = (window[vp+'CancelAnimationFrame']
|| window[vp+'CancelRequestAnimationFrame']);
}
if (/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) // iOS6 is buggy
|| !window.requestAnimationFrame || !window.cancelAnimationFrame) {
var lastTime = 0;
window.requestAnimationFrame = function(callback) {
var now = Date.now();
var nextTime = Math.max(lastTime + 16, now);
return setTimeout(function() { callback(lastTime = nextTime); },
nextTime - now);
};
window.cancelAnimationFrame = clearTimeout;
}
}());
var supportTransform = (function() {
var prefixes = 'transform WebkitTransform MozTransform OTransform msTransform'.split(' ');
var div = document.createElement('div');
for(var i = 0; i < prefixes.length; i++) {
if(div && div.style[prefixes[i]] !== undefined) {
return prefixes[i];
}
}
return false;
}());
var support3dtransform = (function() {
if (!window.getComputedStyle) {
return false;
}
var el = document.createElement('p'),
has3d,
transforms = {
'webkitTransform':'-webkit-transform',
'OTransform':'-o-transform',
'msTransform':'-ms-transform',
'MozTransform':'-moz-transform',
'transform':'transform'
};
// Add it to the body to get the computed style.
(document.body || document.documentElement).insertBefore(el, null);
for (var t in transforms) {
if (el.style[t] !== undefined) {
el.style[t] = "translate3d(1px,1px,1px)";
has3d = window.getComputedStyle(el).getPropertyValue(transforms[t]);
}
}
(document.body || document.documentElement).removeChild(el);
return (has3d !== undefined && has3d.length > 0 && has3d !== "none");
}());
var isAndroid = navigator.userAgent.toLowerCase().indexOf('android') > -1;
var isOperaOld = !!window.opera;
// list with all jarallax instances
// need to render all in one scroll/resize event
var jarallaxList = [];
// Jarallax instance
var Jarallax = (function() {
var instanceID = 0;
function Jarallax(item, userOptions) {
var _this = this,
dataOptions;
_this.$item = $(item);
_this.defaults = {
speed : 0.5,
imgSrc : null,
imgWidth : null,
imgHeight : null,
enableTransform : true,
zIndex : -100
};
dataOptions = _this.$item.data('jarallax') || {};
_this.options = $.extend({}, _this.defaults, dataOptions, userOptions);
// fix speed option [0.0, 1.0]
_this.options.speed = Math.min(1, Math.max(0, parseFloat(_this.options.speed)));
_this.instanceID = instanceID++;
_this.image = {
src : _this.options.imgSrc || null,
$container : null,
$item : null,
width : _this.options.imgWidth || null,
height : _this.options.imgHeight || null,
// fix for Android devices
// use instead background image - more smoothly
useImgTag : isAndroid || isOperaOld
}
if(_this.initImg()) {
_this.init();
jarallaxList.push(_this);
}
}
return Jarallax;
}());
Jarallax.prototype.initImg = function() {
var _this = this;
// get image src
if(_this.image.src === null) {
_this.image.src = _this.$item.css('background-image').replace(/^url\(['"]?/g,'').replace(/['"]?\)$/g,'');
}
if(!_this.image.src || _this.image.src === 'none') {
return false;
}
return true;
}
Jarallax.prototype.init = function() {
var _this = this,
containerStyles = {
position : 'absolute',
top : 0,
left : 0,
width : '100%',
height : '100%',
overflow : 'hidden',
'pointer-events' : 'none',
'transition' : 'transform linear -1ms, -webkit-transform linear -1ms'
},
imageStyles = {
position : 'fixed'
};
// container for parallax image
_this.image.$container = $('