universal-parallax
Advanced tools
Comparing version 1.0.5 to 1.0.6
/** | ||
* @version 1.0.0 | ||
* @version 1.0.6 | ||
* @author Marius Hansen <marius.o.hansen@gmail.com> | ||
@@ -9,3 +9,24 @@ * @license MIT | ||
// determine height | ||
function calculateHeight(parallax, speed) { | ||
for (var i = 0; parallax.length > i; i++) { | ||
var parentHeight = parallax[i].parentElement.scrollHeight; | ||
var elemOffsetTop = (window.innerHeight - parentHeight) / 2; | ||
var bgHeight = parentHeight + ((elemOffsetTop - (elemOffsetTop / speed)) * 2); | ||
parallax[i].style.height = bgHeight + 'px'; | ||
} | ||
} | ||
// set speed | ||
function animateParallax(parallax, speed){ | ||
for (var i = 0; parallax.length > i; i++) { | ||
var parentTopOfElem = parallax[i].parentElement.getBoundingClientRect().top; | ||
var bgScroll = parentTopOfElem / speed; | ||
parallax[i].style.top = bgScroll + 'px'; | ||
} | ||
} | ||
var universalParallax = function() { | ||
@@ -19,22 +40,21 @@ | ||
// Add scroll event listener | ||
window.addEventListener("scroll", function() { | ||
// apply effect to each element | ||
for (var i = 0; parallax.length > i; i++) { | ||
if (/Mobi/.test(navigator.userAgent)) { | ||
pageHeight = screen.height; | ||
}else{ | ||
pageHeight = window.innerHeight; | ||
} | ||
// get measurements | ||
var parallaxContainer = parallax[i].parentElement; | ||
var parentHeight = parallaxContainer.scrollHeight; | ||
var parentTopOfElem = parallaxContainer.getBoundingClientRect().top; | ||
var elemOffsetTop = (window.innerHeight - parentHeight) / 2; | ||
// determine height | ||
var bgHeight = parentHeight + ((elemOffsetTop - (elemOffsetTop / speed)) * 2); | ||
parallax[i].style.height = bgHeight + 'px'; | ||
// determine height | ||
calculateHeight(parallax, speed); | ||
// recalculate on resize | ||
window.addEventListener("resize", function() { | ||
calculateHeight(parallax, speed); | ||
}); | ||
// set speed | ||
var bgScroll = parentTopOfElem / speed; | ||
parallax[i].style.top = bgScroll + 'px'; | ||
} | ||
// Add scroll event listener | ||
window.addEventListener("scroll", function() { | ||
// apply effect to each element | ||
animateParallax(parallax, speed); | ||
}); | ||
@@ -41,0 +61,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
var universalParallax=function(){var e=function(e,a){a<1.2&&(a=0),window.addEventListener("scroll",function(){for(var t=0;e.length>t;t++){var n=e[t].parentElement,l=n.scrollHeight,r=n.getBoundingClientRect().top,s=(window.innerHeight-l)/2,i=l+2*(s-s/a);e[t].style.height=i+"px";var o=r/a;e[t].style.top=o+"px"}})};this.init=function(a){void 0===a&&(a={}),a={speed:void 0!==a.speed?a.speed:4,className:void 0!==a.className?a.className:"parallax"};for(var t=document.getElementsByClassName(a.className),n=0;t.length>n;n++){var l=document.createElement("div");t[n].parentNode.insertBefore(l,t[n]),l.appendChild(t[n]);var r=t[n].parentElement;r.className+="parallax--container","relative"!==window.getComputedStyle(r.parentElement,null).getPropertyValue("position")&&(this.style.position="relative");var s=t[n].dataset.parallaxImage;void 0!==s&&(t[n].style.backgroundImage="url("+s+")",1===t[n].classList.length&&"parallax"===t[n].classList[0]&&Object.assign(t[n].style,{"background-repeat":"no-repeat","background-position":"center","background-size":"cover"}))}e(t,a.speed)}}; | ||
function calculateHeight(e,a){for(var t=0;e.length>t;t++){var n=e[t].parentElement.scrollHeight,i=(window.innerHeight-n)/2,l=n+2*(i-i/a);e[t].style.height=l+"px"}}function animateParallax(e,a){for(var t=0;e.length>t;t++){var n=e[t].parentElement.getBoundingClientRect().top/a;e[t].style.top=n+"px"}}var universalParallax=function(){var e=function(e,a){a<1.2&&(a=0),/Mobi/.test(navigator.userAgent)?pageHeight=screen.height:pageHeight=window.innerHeight,calculateHeight(e,a),window.addEventListener("resize",function(){calculateHeight(e,a)}),window.addEventListener("scroll",function(){animateParallax(e,a)})};this.init=function(a){void 0===a&&(a={}),a={speed:void 0!==a.speed?a.speed:4,className:void 0!==a.className?a.className:"parallax"};for(var t=document.getElementsByClassName(a.className),n=0;t.length>n;n++){var i=document.createElement("div");t[n].parentNode.insertBefore(i,t[n]),i.appendChild(t[n]);var l=t[n].parentElement;l.className+="parallax--container","relative"!==window.getComputedStyle(l.parentElement,null).getPropertyValue("position")&&(this.style.position="relative");var r=t[n].dataset.parallaxImage;void 0!==r&&(t[n].style.backgroundImage="url("+r+")",1===t[n].classList.length&&"parallax"===t[n].classList[0]&&Object.assign(t[n].style,{"background-repeat":"no-repeat","background-position":"center","background-size":"cover"}))}e(t,a.speed)}}; |
{ | ||
"name": "universal-parallax", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Easy parallax plugin using pure javascript. Also works on mobile platforms. Cross browser support.", | ||
@@ -5,0 +5,0 @@ "main": "dist/universal-parallax.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1819595
212