var animationSettings = {
first: {
minTop: 130,
maxTop: 350,
// animates rotation from -55 deg to 0
valAtMin: -55,
valAtMax: 0,
transformTemplate: "rotatez({X}deg) rotate(-45deg) translate(0,0)"
},
second: {
minTop: 20,
maxTop: 160,
// animates rotation from 175 deg to -30deg
valAtMin: 175,
valAtMax: -30,
transformTemplate: "rotatez({X}deg) rotate(-45deg) translate(0,0)"
},
third: {
minTop: 50,
maxTop: 180,
// this time, animates the horizontal shift from 0 to 90px
valAtMin: 0,
valAtMax: 90,
transformTemplate: "rotate(-45deg) translate({X}px,0)"
}
}
// Define window element
var $window = $(window);
// Get viewport offset function
function getViewportOffset($element) {
var scrollLeft = $window.scrollLeft(),
scrollTop = $window.scrollTop(),
offset = $element.offset(),
rect1 = { x1: scrollLeft, y1: scrollTop, x2: scrollLeft + $window.width(), y2: scrollTop + $window.height() },
rect2 = { x1: offset.left, y1: offset.top, x2: offset.left + $element.width(), y2: offset.top + $element.height() };
return {
left: offset.left - scrollLeft,
top: offset.top - scrollTop,
insideViewport: rect1.x1 < rect2.x2 && rect1.x2 > rect2.x1 && rect1.y1 < rect2.y2 && rect1.y2 > rect2.y1
};
}
$(window).on("load scroll resize", function() {
for (var id in animationSettings) {
var element = $('#'+id);
if (!element) continue; // Skip if element with this id doesn't exist
var settings = animationSettings[id];
var offsetTop = getViewportOffset(element).top;
var value;
// Calculate the value proportionally to scrolling position between limits
if (offsetTop <= settings.minTop) value = settings.valAtMin;
else if (offsetTop >= settings.maxTop) value = settings.valAtMax;
else value = settings.valAtMin + (settings.valAtMax - settings.valAtMin) * (offsetTop - settings.minTop) / (settings.maxTop - settings.minTop);
// Store the current value in the same object
settings.value = value;
}
// If this is the first call, start the animation
if (!animationStarted) requestAnimationFrame(doAnimation);
});
// Use requestAnimationFrame for animation to optimize performance
function doAnimation() {
for (var id in animationSettings) {
var element = $('#'+id);
if (!element) continue; // Skip if element with this id doesn't exist
var settings = animationSettings[id];
element.css('transform', settings.transformTemplate.replace('{X}', settings.value));
}
requestAnimationFrame(doAnimation);
}
animationStarted = false;
body {height: 2048px}
.element {position: absolute;
top: 400px; left: 30px;
width: 107px;
height:107px;
background-color: #707070}
#secondo {position: absolute;
top: 450px; left: 150px;
}
#terzo {position: absolute;
top: 550px; left: 270px;
}
#quarto {position: absolute;
top: 650px; left: 150px;
}
#quinto {position: absolute;
top: 450px; left: 510px;
}
#sesto {position: absolute;
top: 350px; left: 630px;
}
#settimo {position: absolute;
top: 650px; left: 450px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="primo" class="element" name="element"></div>
<div id="secondo" class="element" name="element"></div>
<div id="terzo" class="element" name="element"></div>
<div id="quarto" class="element" name="element"></div>
<div id="quinto" class="element" name="element"></div>
<div id="sesto" class="element" name="element"></div>
<div id="settimo" class="element" name="element"></div>