Skip to content

Instantly share code, notes, and snippets.

View donnervetter's full-sized avatar
🎯
Focusing

Daumantas Jakas donnervetter

🎯
Focusing
View GitHub Profile
@donnervetter
donnervetter / scroll-percentage.js
Last active May 12, 2021 12:38
get percentage of an element scrolled through the viewport
/*
This calculates the position of an element in a viewport in percentage (usable for animations on scroll, etc)
*/
// el is the element we want to to calculate percentage of
// 0 when whole element enters the viewport and 100 when element reaches top of the viewport
let percentage = ((window.innerHeight - el.getBoundingClientRect().top - el.offsetHeight) / (window.innerHeight - el.offsetHeight)) * 100;
// 0 when top of the element enters the viewport and 100 when bottom of the element leaves the viewport
@function em($target, $context: $base-font-size) {
@if $target == 0 { @return 0 }
@return $target / $context + 0em;
}
$base-font-size: 15px;
h1 {
font-size: em(21px, 15px); // Outputs 1.4em
}