'Make nav text colour change after passing first section

I've been trying to figure out how to change the font colour in my nav to change after scrolling past the blue section into the orange section.

https://tom-odell-v2.vercel.app/

enter image description here

This is what I'm working with for my javascript but the issue is that the scroll point is different depending on the screen size and I need something that is going to be responsive to mobile styling.

$(document).ready(function(){       
var scroll_pos = 0;
$(document).scroll(function() { 
    scroll_pos = $(this).scrollTop();
    if(scroll_pos > 750) {
        $('.change').css('color', '#244093');
    } else {
        $('.change').css('color', '#ffbd75');
    }
}); });

I'm not very good with jquery/javacript and would appreciate some assistance or resources!



Solution 1:[1]

using the same method you are trying, you can add multiple if statements to achieve this. Check the code snippet below. You can do this using intersection observer.

window.onload = function(){
let homePage = document.querySelector("#homepage");
window.onscroll = function () {
       
        var pos = window.pageYOffset;
        if(pos<300){
        homePage.style.color = "red";
        }
        if (pos > 300 && pos<400) {
         homePage.style.color = "blue";
        }
        if(pos>600){
            homePage.style.color = "green";
        }
}

}
nav{
position:sticky;
top:0;
}
nav a{
color:red;
}
<nav>
<a href="#" id="homepage">Home Page</a>

</nav>
<p>
fixture can be a string path (or array of those), or object (or array of those) that represents your local fixture file and contains following properties:

{string} filePath - file path (with extension)
{string} fileName - the name of the file to be attached, this allows to override the name provided by filePath
{Blob} fileContent - the binary content of the file to be attached
{string} mimeType - file MIME type. By default, it gets resolved automatically based on file extension. Learn more about mime
{string} encoding - normally cy.fixture resolves encoding automatically, but in case it cannot be determined you can provide it manually. For a list of allowed encodings, see here
{number} lastModified - The unix timestamp of the lastModified value for the file. Defaults to current time. Can be generated from new Date().getTime() or Date.now()
</p>
<p>
fixture can be a string path (or array of those), or object (or array of those) that represents your local fixture file and contains following properties:

{string} filePath - file path (with extension)
{string} fileName - the name of the file to be attached, this allows to override the name provided by filePath
{Blob} fileContent - the binary content of the file to be attached
{string} mimeType - file MIME type. By default, it gets resolved automatically based on file extension. Learn more about mime
{string} encoding - normally cy.fixture resolves encoding automatically, but in case it cannot be determined you can provide it manually. For a list of allowed encodings, see here
{number} lastModified - The unix timestamp of the lastModified value for the file. Defaults to current time. Can be generated from new Date().getTime() or Date.now()
</p>
<p>
fixture can be a string path (or array of those), or object (or array of those) that represents your local fixture file and contains following properties:

{string} filePath - file path (with extension)
{string} fileName - the name of the file to be attached, this allows to override the name provided by filePath
{Blob} fileContent - the binary content of the file to be attached
{string} mimeType - file MIME type. By default, it gets resolved automatically based on file extension. Learn more about mime
{string} encoding - normally cy.fixture resolves encoding automatically, but in case it cannot be determined you can provide it manually. For a list of allowed encodings, see here
{number} lastModified - The unix timestamp of the lastModified value for the file. Defaults to current time. Can be generated from new Date().getTime() or Date.now()
</p><p>
fixture can be a string path (or array of those), or object (or array of those) that represents your local fixture file and contains following properties:

{string} filePath - file path (with extension)
{string} fileName - the name of the file to be attached, this allows to override the name provided by filePath
{Blob} fileContent - the binary content of the file to be attached
{string} mimeType - file MIME type. By default, it gets resolved automatically based on file extension. Learn more about mime
{string} encoding - normally cy.fixture resolves encoding automatically, but in case it cannot be determined you can provide it manually. For a list of allowed encodings, see here
{number} lastModified - The unix timestamp of the lastModified value for the file. Defaults to current time. Can be generated from new Date().getTime() or Date.now()
</p>
<p>
fixture can be a string path (or array of those), or object (or array of those) that represents your local fixture file and contains following properties:

{string} filePath - file path (with extension)
{string} fileName - the name of the file to be attached, this allows to override the name provided by filePath
{Blob} fileContent - the binary content of the file to be attached
{string} mimeType - file MIME type. By default, it gets resolved automatically based on file extension. Learn more about mime
{string} encoding - normally cy.fixture resolves encoding automatically, but in case it cannot be determined you can provide it manually. For a list of allowed encodings, see here
{number} lastModified - The unix timestamp of the lastModified value for the file. Defaults to current time. Can be generated from new Date().getTime() or Date.now()
</p><p>
fixture can be a string path (or array of those), or object (or array of those) that represents your local fixture file and contains following properties:

{string} filePath - file path (with extension)
{string} fileName - the name of the file to be attached, this allows to override the name provided by filePath
{Blob} fileContent - the binary content of the file to be attached
{string} mimeType - file MIME type. By default, it gets resolved automatically based on file extension. Learn more about mime
{string} encoding - normally cy.fixture resolves encoding automatically, but in case it cannot be determined you can provide it manually. For a list of allowed encodings, see here
{number} lastModified - The unix timestamp of the lastModified value for the file. Defaults to current time. Can be generated from new Date().getTime() or Date.now()
</p>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sanan Ali