Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix position change with JavaScript

I am trying to achive a fixed position after a certain point of the page is passed using CSS JS and HTML. Also I don't know the bet aproach in loading the function into the html doc, I was thinking on using the onload...

Here is what I have done until now:

<!DOCTYPE html>
<html>
    <head>
        <script>
            var left1 = document.getElementById("left1");
            var origOffsetY = left1.offsetTop;

            function onScroll(e) {
                window.scrollY >= origOffsetY ? left1.style.position = "fixed":
                left1.style.position="absolute";
            }

            document.addEventListener('scroll', onScroll);
        </script language="JavaScript">
        <style>
            #main {
                position: relative;
                width: 620px;
                margin: 0 auto;
                height: 1800px;
            }
            #left1{
                position: absolute;
                font-family: sans-serif;
                left: 0px;
                top: 10px;
                height: 200px;
                width: 300px;
                background-color: #F6D565;
            }
            #right1{
                position:absolute; 
                font-family: sans-serif;
                top: 10px;
                right: 0px;
                height: 300px;
                width: 300px;
                background-color: #DFFCC2;
            }
            #right2{
                position:absolute;  
                top: 320px;
                right: 0px;
                font-family: sans-serif;
                height:300px; 
                width: 300px;
                background-color: #DFFCC2;
            }
            #right3{
                position:absolute;
                top: 630px;
                right: 0px;
                font-family: sans-serif;
                height: 300px;
                width: 300px;
                background-color: #DFFCC2;
            }
        </style>
    </head>
    <body >
        <div id="main">
            <div id="left1">aaaaaaaaaaaaaaaaaaaa</div>
            <div id="right1">bbb</div>
            <div id="right2">cccccccccccccccccccccc</div>
            <div id="right3">ddd</div>
        </div>
    </body>
</html>
like image 610
Laur Stefan Avatar asked Mar 20 '26 16:03

Laur Stefan


1 Answers

DEMO

The DOM is not available when you are trying to access div with id left1.

So your first line var left1 = document.getElementById("left1"); will give error.

Instead Wrap your current code within window.onload

<script>
       window.onload = function() {     
            var left1 = document.getElementById("left1");
            var origOffsetY = left1.offsetTop;

            function onScroll(e) {
               console.log("calling scroll")
                window.scrollY >= origOffsetY ? left1.style.position = "fixed":
                left1.style.position="absolute";
            }

            document.addEventListener('scroll', onScroll);
                                  }
  </script>

Else place your javascript just above the </body> tag

like image 130
mohamedrias Avatar answered Mar 23 '26 04:03

mohamedrias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!