1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
(function ($) {
$(document).ready(function() {
// hauteur de chaque colonne
var heightcol1 = $("#scroll1").innerHeight();
var heightcol2 = $("#scroll2").innerHeight();
var heightcol3 = $("#scroll3").innerHeight();
// la colonne la plus haute
var heightmax = 0;
$('#scroll1, #scroll2, #scroll3').each(function(){
heightmax = Math.max(heightcol1, heightcol2, heightcol3);
});
//Calcul du ratio de chaque colonne
var largeColRatio = heightcol1 / heightmax,
mediumColRatio = heightcol2 / heightmax,
smallColRatio = heightcol3 / heightmax;
var currentScroll1 = heightcol1 / heightmax;
var currentScroll2 = heightcol2 / heightmax;
var currentScroll3 = heightcol3 / heightmax;
// calcul du nbr de colonnes
var parallaxElements = $('.col'),
parallaxQuantity = parallaxElements.length;
// création de la fonction scroll
$(document).scroll(function() {
var _$contentWrapper = $(".parallax");
var _$scrollContainer = $("#main");
var _contentTop = _$contentWrapper.offset().top;
var _bottom = _$scrollContainer.height();
var currentScrollRatio = -(_contentTop - 100) / (_bottom - _contentTop);
var t = (heightmax - heightcol1) * currentScrollRatio;
var e = (heightmax - heightcol2) * currentScrollRatio;
var p = (heightmax - heightcol1) * currentScrollRatio;
var $w = $(this),
scroll_top = $w.scrollTop(),
scroll_percent = Math.round((scroll_top) / heightmax * 100);
// Pour chaque colonne
for (var i = 0; i < parallaxQuantity; i++) {
if ($(heightcol1 < heightmax)) {
$(".large-translate").css("transform", "translate3d(0," + -(scroll_percent) + "px, 0");
}
if ($(heightcol2 < heightmax)) {
$(".medium-translate").css("transform", "translate3d(0," + -(e*scroll_percent) + "px, 0");
$(".small-translate").css("transform", "translate3d(0," + -(p*scroll_percent) + "px, 0");
}
}
});
});
}(jQuery)); |
Partager