Skip to content

Instantly share code, notes, and snippets.

@cassc
Created August 26, 2021 10:01
Show Gist options
  • Select an option

  • Save cassc/73679a3a4f4fbe33c9f645fb4d4a2636 to your computer and use it in GitHub Desktop.

Select an option

Save cassc/73679a3a4f4fbe33c9f645fb4d4a2636 to your computer and use it in GitHub Desktop.
Parallax scroll animation with lax.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/lax.js" ></script>
</head>
<body>
<div class="container">
<div class="holder ">
<div class="object o1 lax lax_preset_slideY:190:-380"></div>
</div>
<div class="holder ">
<div class="object o2 lax lax_preset_slideY:200:-110"></div>
</div>
<div class="holder ">
<div class="object o3 lax lax_preset_slideY:500:-70"></div>
</div>
</div>
</body>
<style>
.holder{
width: 90vw;
height: 400px;
background-color: lightgrey;
margin-bottom: 100px;
position: relative;
display:flex;
flex-direction:column;
border-radius: 1rem;
justify-content: center;
position: relative;
}
.object{
position: absolute;
left: 2rem;
width: 30vw;
height: 380px;
background-color: blue;
border-radius: 0.4rem;
background-image: url('sample.webp');
background-position: center;
}
.o1{
top: 300px;
}
.o2{
top: 120px;
}
.o3{
top: 80px;
}
.container {
padding: 4rem;
width: 100%;
display:flex;
flex-direction: column;
align-items:center;
/* padding-top: 20vh; */
}
body {
padding: 0;
margin: 0;
padding: 0;
margin: 0;
height: 380vh;
}
</style>
<script type="application/javascript">
window.onload = function () {
lax.init()
// Add a driver that we use to control our animations
lax.addDriver('scrollY', function () {
return window.scrollY
})
/* console.log(`pos: ${position}`) */
/* lax.addElements('.o1',{
* scrollY: {
* translateY: [
* [0, 20, 40],
* [-80, 0, 10],
* ]
* }
* }) */
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment