Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make a image re-visible by display: flex; after user scrolling a fixed height

I have an image (with myContent id) on my HTML page, for which I put display: None in my css.

my code:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
</head>
<body>

    <!-- header start -->
    <div class="container-fluid">
        <div class="jumbotron">
            <h1>Header area</h1>
        </div>
    </div>
    <!-- header end -->

    <!-- Your page contant start -->
    <div class="container-fluid">
        <div class="alert alert-primary" style="height: 800px;">
            <h1>Your page contant area</h1>
            <img src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" id="myContent">
        </div>
    </div>
    <!-- Your page contant end -->

<style>
    #myContent{
        display: none;
    }
</style>

</body>
</html>

When any visitor scrolls down(approximately equal header height) and the header/ jumbotron section goes up(for scrolling), then I want to make the image re-visible by applying display: flex;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Please suggest how can I implement this?

>Solution :

best chance would be javascript, create an eventlisener on scroll, check users current scroll.y position and when higher then x add class else remove class, a class that add display:flex;

could look something like this

let img = document.querySelector('#myContent')
document.addEventListener('scroll', function(e) {
if(window.scrollY > heightofyourelement){
img.classList.add('show')
}else{
img.classList.remove('show')
}
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading