To optimize performance, consider consolidating all images into a single sprite and utilizing background-position
to switch between frames.
Here is an example of achieving this using skrollr in a slightly aggressive manner. Additionally, leveraging a CSS preprocessor can significantly reduce the verbosity of the CSS code.
Check out the jsfiddle demo here
Sass/SCSS:
$image-width: 240;
.container {
position: fixed;
top: 0;
[class^="sprite-"], [class*=" sprite-"] {
background-image: url(http://i.imgur.com/vpjGWKb.png);
background-position: 0 0;
background-repeat: no-repeat;
position: absolute;
width: #{$image-width - 2}px;
height: 200px;
display: none;
}
@for $i from 0 through 17 {
.sprite-#{($i + 1)} {
background-position: unquote(-#{($i * $image-width)}px) 0;
}
}
}
.spacer-div {
margin-bottom: 2000px;
}
HTML:
<div class="container">
<div class="sprite-1" data-0p="display:block" data-5p="display:none"></div>
<div class="sprite-2" data-0p="display:none" data-5p="display:block" data-10p="display:none"></div>
<div class="sprite-3" data-0p="display:none" data-10p="display:block" data-15p="display:none"></div>
<!-- More sprite elements... -->
</div>
<div class="spacer-div">
</div>