Scroll Triggered
by Venkatesh Dematti
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
font-family: 'Helvetica', sans;
}
div.wrapper,div.wrapper2 {
margin: 260px auto;
width:600px;
background:#efe;
height: 1500px;
}
div.sticker,div.sticker2 {
padding: 20px;
margin: 20px 0;
background: #ece;
width: 190px;
transition: all 2s;
}
.stick,stick2 {
position:fixed;
top:100px;
background:red !important;
transition: all 2s;
}
.marker {
position: absolute;
top: 1000px;
right: 100px;
font-size: 2em;
}</style>
</head>
<body>
<div class="wrapper">
<div class="sticker">fixed pos until 1000</div>
</div>
<div class="wrapper2">
<div class="sticker2">
Some Text
</div>
</div>
<div class="marker">< 1000</div>
<!-- Updated -->
<!-- Updated -->
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
<!-- Upated -->
var offsets = $('.sticker').offset();
var topPos = offsets.top;
alert(topPos);
var offsets2 = $('.sticker2').offset();
var topPos2 = offsets2.top;
alert(topPos2);
<!-- Upated -->
$(window).scroll(function(){
var windowPosition = $(window).scrollTop();
if(windowPosition > topPos & windowPosition <=topPos +500){
$('.sticker').addClass('stick');
}else{
$('.sticker').removeClass('stick');
}
if(windowPosition >= topPos2 & windowPosition <=topPos2 +500){
$('.sticker2').addClass('stick');
}else{
$('.sticker2').removeClass('stick');
}
});
});
</script>
</body>
</html>