Created
November 3, 2021 11:04
-
-
Save kartick14/14bb789083ee7cb3b9abe74de893a14a to your computer and use it in GitHub Desktop.
Calculate percentage ratio on scroll ( with Jquery )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="note" ></div> | |
<br><br><br><br><br><br><br><br><br><br><br> | |
<div class="scrollratio" ></div> | |
<br><br><br><br><br><br><br><br><br><br><br> | |
<br><br><br><br><br><br><br><br><br><br><br> | |
<br><br><br><br><br><br><br><br><br><br><br> | |
<br><br><br><br><br><br><br><br><br><br><br> | |
<style> | |
body { | |
background:#171717; | |
} | |
.scrollratio { | |
height: 2000px; | |
background:red; | |
} | |
#note { | |
position: fixed; | |
top: 0; | |
left: 0; | |
color: #FFFFFF; | |
margin: 2em; | |
} | |
</style> | |
<script> | |
$(document).ready(function(){ | |
var initY = $('.scrollratio').offset().top; | |
var height = $('.scrollratio').height(); | |
var endY = initY + $('.scrollratio').height(); | |
$(window).scroll(function(){ | |
var scroll = $(window).scrollTop(); | |
if(scroll > initY && scroll < endY){ | |
var diff = scroll - initY; | |
var ratio = Math.round((diff / height) * 100); | |
$('#note').text(ratio); | |
} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://codepen.io/lemagus/pen/RWxEYz