I have personally implemented a similar feature before and found it to be surprisingly easy
To start, you can include CSS code like the following for smooth animated transitions without relying on jQuery:
.logo {
height: 100px;
-webkit-transition: height .4s ease,opacity .3s ease,background-color .3s ease;
-o-transition: height .4s ease,opacity .3s ease,background-color .3s ease;
transition: height .4s ease,opacity .3s ease,background-color .3s ease
}
Next, utilize JavaScript to add a class to the image after a certain amount of scrolling:
$( document ).ready(function() {
"use strict"; // Utilizing strict mode
var scrollInit = $(window).scrollTop();
var windowWInitial = $(window).width();
if (scrollInit >= 2) {// Adjust this value based on your scrolling needs
$("#header").addClass("sticky-header");
}else{
$("#header").removeClass("sticky-header");
}
}
Simply adjust the height by setting another CSS variable for the sticky-header class:
.logo .sticky-header {
height: 60px;
}
By implementing this, the image will adjust its height when reaching your specified scroll length.
It's recommended to use Chrome Devtools to inspect elements and learn from others' implementations. Feel free to explore my website TechGorilla for a real-life example.
Note: I used jQuery in this solution, which can save time and is commonly used on websites not utilizing frameworks like React or Angular