Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Assistance with two sticky headers.

Ahmad Saleh

New Coder
Hello,

I am self-teaching myself how to code HTML, CSS and Javascript and I am super new to this (this is my first project.) There are lots of resources online that I am using and I've managed to figure things out as I go.
However, I am encountering two issues:
1. I had a sticky header "portable digital format" (page title) and then a paragraph underneath that would disappear under the header (which is great because that is what I wanted to happen) and then I wanted to add another sticky header under the paragraph that is the title of the exhibition "but I am not an artist" so that once the paragraph above is scrolled away, the exhibition title sticks under the page title, kind of stacking over each other. But since I added the exhibition title sticky header, the page title stopped working and it just scrolls away and only the exhibition title sticks.

2. this issue is a bit more trivial and might get fixed with fixing issue 1 (not sure) but when the exhibition title scrolls all up, the text reaches the top border of the page and then kinda jumps a bit lower into a stationary position. The text background also becomes bigger somehow. (I'll attach screenshots)

I hope I made sense. I am attaching my code. I'd appreciate any help or direction on how to resolve this. And as I am very very new to this, please explain it step by step as I am not familiar with any of the terminologies.

Thanks in advance! :D

Screen Shot 2022-02-24 at 10.14.23 PM.png Screen Shot 2022-02-24 at 10.14.33 PM.png

HTML:
<!DOCTYPE html>
<html>
<head>
    <title>PDF</title>
</head>

<style>

    body {
        height: 2000px;
        background: linear-gradient(0deg, rgb(255, 255, 255) 0%, rgb(179, 179, 179) 50%, rgb(0, 0, 0) 85%);
        margin-left: 0px;
    }
    
    .header {
        padding-left: 35px;
        padding-top: 0px;
        text-align: left;
        font-family: sans-serif;
        font-size: 15px;
        color: rgb(255, 255, 255);
        background:rgb(0, 0, 0);
    }
    .exhibition {
        padding-left: 0px;
        padding-top: 0px;
        text-align: center;
        font-family: sans-serif;
        font-size: 15px;
        color: rgb(255, 255, 255);
        background:rgb(0, 0, 0);
    }
    
    .content {
        padding: 16px;
        padding-top: 60px;
        padding-left: 35px;
        text-align: left;
        font-family: sans-serif;
        font-size: 20px;
        color: rgb(255, 255, 255);
        background: transparent;
    }
    
    .sticky {
        position: fixed;
        top: 0;
        width: 100%;
    }

    .sticky + .content {
        padding-top: 102px;
    }


    
</style>


<body>
     <div class="header" id="myHeader">
        <h2>Portable<br>Digital<br>Format</h2>
      </div>

<p class="content">Portable Digital Format is a curated virtual space founded by Ahmad Saleh to showcase creative talent within the SWANA region. This platform will serve as an easy-to-consume, scroll-to-explore, portable exhibition that can be accessed anytime, anywhere.</p>
<p class="content">Our first exhibition titled "But I am not an Artist" focuses on creative works done by people who have not been classically trained in any art field. The showcased works are from product designers, financial analysts, marketing managers, amongst many others. The goal of this edition is to highlight creativity in the most unexpected places as these inidividuals have expressed themselves through various media such as acrylics, photography, digital art using Adobe Illustrator and other avenues highlighted in the exhibition.</p>
<div class="exhibition" id="exhibition">
    <h2>BUT I AM NOT AN ARTIST</h2>
  </div>

<script>
    window.onscroll = function () {myFunction()}
    var header = document.getElementById("myHeader")
    var sticky = header.offsetTop
    function myFunction() {
        if (window.pageYOffset > sticky) {
            header.classList.add("sticky");
        } else {
            header.classList.remove("sticky");
        }
    }

    window.onscroll = function () {myFunction()}
    var header = document.getElementById("exhibition")
    var sticky = header.offsetTop
    function myFunction() {
        if (window.pageYOffset > sticky) {
            header.classList.add("sticky");
        } else {
            header.classList.remove("sticky");
        }
    }

</script>


</body>

</html>
 
Back
Top Bottom