Skip to content

Instantly share code, notes, and snippets.

@lgirao
Last active August 29, 2021 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgirao/19798c89e531e7dbc63192bc60946b9b to your computer and use it in GitHub Desktop.
Save lgirao/19798c89e531e7dbc63192bc60946b9b to your computer and use it in GitHub Desktop.
Hero Effect
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Hero Header Effect</title>
</head>
<body>
<div id="hero">
<h2>Hello!</h2>
<p>I am the Hero.</p>
</div>
<div id="main-content">
<h3>Hello!</h3>
<p>I am the main content</p>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="script.js"></script>
</body>
</html>
$(document).on('scroll touchmove touchend', function(e) {
var offTop = $(document).scrollTop();
$('#hero').find('h2').css({
marginTop: - (offTop * 0.2),
opacity: (100 - (offTop * 0.2)) / 100
});
});
* { border: 0; margin: 0; padding: 0; }
body {
font-family: "Arial", sans-serif;
font-size: 14px;
}
p {
margin-top: 2rem;
}
div#hero {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: #8997ff;
height: 300px;
text-align: center;
padding-top: 120px;
color: #fff;
}
div#main-content {
height: 1500px;
background: #fff;
margin-top: 300px;
position: relative;
z-index: 1;
text-align: center;
padding-top: 30px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment