.mover {
position: absolute;
width: 100px;
height: 100px;
border-radius: 50px;
background: #b00;
left: 90%;
-webkit-transition: left 600ms ease-out;
-moz-transition: left 600ms ease-out;
-ms-transition: left 600ms ease-out;
-o-transition: left 600ms ease-out;
transition: left 600ms ease-out;
}
.mover.left {
left: 10%;
}
JavaScript
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var effUpdate = debounce(function(){
lastScrollY = window.scrollY;
update();
}, 100);
var movers = document.querySelectorAll('.mover'),
lastScrollY = 0,
ticking = false;
/**
* Set everthing up and position all the DOM elements
* - normally done with the CSS but, hey, there's heaps
* of them so we're doing it here!
*/
(function init() {
for(var m = 0; m < movers.length; m++) {
movers[m].style.top = (m * 10) + 'px';
}
})();
/**
* Callback for our scroll event - just
* keeps track of the last scroll value
*/
function onScroll() {
lastScrollY = window.scrollY;
requestTick();
}
/**
* Calls rAF if it's not already
* been done already
*/
function requestTick() {
if(!ticking) {
requestAnimationFrame(update);
ticking = true;
}
}
/**
* Our animation callback
*/
function update() {
var mover = null,
moverTop = [],
halfWindowHeight = window.innerHeight * 0.5,
offset = 0;
// first loop is going to do all
// the reflows (since we use offsetTop)
for(var m = 0; m < movers.length; m++) {
mover = movers[m];
moverTop[m] = mover.offsetTop;
}
// second loop is going to go through
// the movers and add the left class
// to the elements' classlist
for(var m = 0; m < movers.length; m++) {
mover = movers[m];
if(lastScrollY > moverTop[m] - halfWindowHeight) {
mover.classList.add('left');
} else {
mover.classList.remove('left');
}
}
// allow further...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.