The sticky navigation bar's background fails to change at the appropriate time following an image slider

I am currently facing an issue with my sticky navigation bar. I want the background color to change to white after the user scrolls past the image slider. The problem is that the white background appears above the image slider instead of in the orange logo section where it should be. I have tried using a pixel offset in the jQuery event, but due to the responsive nature of my site, this approach is not effective across different browser sizes. Any suggestions on how to fix this would be greatly appreciated!

Thank you!

You can view the issue in action on the website I'm working on

Below is the jQuery code for changing the background color and where the ".getsticky" class is applied in the HTML:

$(document).ready(function(){  
    if ( $(window).width() > 769) {     
       var scroll_start = 0;
       var startchange = $('.getsticky');
       var offset = startchange.offset();
       $(document).scroll(function() { 
          scroll_start = $(this).scrollTop() + 0;
          // scroll_start = $(this).scrollTop();
          if(scroll_start > offset.top) {
              $('.main-nav').css('background-color','rgba(255,255,255,0.8)');
           } else {
              $('.main-nav').css('background-color', 'transparent');
           }
       });
    }
 });

<div id="work" class="grid twelve work">
        <div class="container">
        <div class="flexslider">
            <ul class="slides">
                <li>
                    <a href="#"><img src="img/work/photo1.jpg" /></a>
                </li>

                <li>
                    <img src="img/work/photo2.jpg" />
                </li>

                <li>
                    <img src="img/work/photo3.jpg" />

                </li>
            </ul>
        </div>
        </div>
    </div>

    <section class="grid work-logos getsticky">
        <div class="container">
            <div class="grid twelve">                   
                <div>
                    <p>
                        Select clients from our collective experience.
                    </p>
                </div>
            </div>
            <div class="grid twelve logoSquare">
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/ibm_logo.svg" alt="IBM">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/google_logo.svg" alt="Google">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/aiga_logo.svg" alt="AIGA">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/obama_logo.svg" alt="Obama">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/espn_logo.svg" alt="ESPN">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/herman_logo.svg" alt="Herman">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/vaystays_logo.svg" alt="Vaystays">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/nd_logo.svg" alt="ND">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/sd_logo.svg" alt="SD">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/fuzzy_logo.svg" alt="Fuzzy">
                </div>
                <div class="grid threeDesktop threeTablet fourPhone six">
                    <img src="img/about/usaa_logo.svg" alt="USAA">
                </div>
            </div>
        </div>
    </section>

Answer №1

In my opinion, the issue lies with jQuery fetching the offset value at an incorrect timing.

To resolve this, you can re-establish the offset inside the $(document).scroll function, which should fix the problem. The modified code will look like:

$(document).ready(function(){  
    if ( $(window).width() > 769) {     
       var scroll_start = 0;
       var startchange = $('.getsticky');
       var offset = startchange.offset();
       $(document).scroll(function() { 
          scroll_start = $(this).scrollTop() + 0;

          offset = startchange.offset(); // UPDATED THIS LINE !!

          // scroll_start = $(this).scrollTop();
          if(scroll_start > offset.top) {
              $('.main-nav').css('background-color','rgba(255,255,255,0.8)');
           } else {
              $('.main-nav').css('background-color', 'transparent');
           }
       });
    }
 });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to create interactive canvases that respond to multiple clicks using JavaScript?

Within the canvas, the code snippet below aims to generate a grid of 10x10 colored boxes with alternating reddish and bluish shades on a gray background. The intention is for each canvas to only respond to mouse interactions when the mouse is within its bo ...

Issue with uploading files larger than 10MB using Uploadify

Just recently, I decided to try out the new uploadify plugin for handling multiple file uploads. Everything was working perfectly until I attempted to upload a movie file that was about 700MB in size. To my surprise, I discovered that there is a limitation ...

An express.js mishap occurred when attempting to process an HTTP POST request using an AJAX client-side, resulting in a

Encountering a 500 Internal Server Error when attempting to send an HTTP POST request from client-side JavaScript using JQuery AJAX in Express.js. The code resides in the p.js file on the client. function dataHandler(conn,data) { var datalol=data; ...

Display personalized content over images utilizing the jQuery galleria plugin

Hey there, I've been successfully using the jquery galleria plugin, but now I'm looking to add some custom content and links that will display in the center of the image. Is it possible to achieve this with galleria? Your insights would be greatl ...

Having a problem with Node.js and async.eachSeries? Want to figure out how to configure the callback function to generate a JSON file for each element in an array of

Currently, I am utilizing async.eachSeries to repeatedly call a function that generates an array called pointsForFormating. Each time this function is called, I format the pointsForFormating array and store the result in another array named pointsFormate ...

Using Jquery to access the grandparent element

When I have code similar to what is shown below, an element contains within 3 layers: <span class="dropdown test1"> <span class="test2" type="button" data-toggle="dropdown">hello</span> <ul class="dropdown-menu test3" style="m ...

Unable to alter fxFlex property within Component using "setAttribute('fxFlex', '25%')" does not function properly in Angular 6

Currently, I am utilizing the flexLayout module to create responsive divs in my Angular application. You can find more information about flexLayout at https://github.com/angular/flex-layout and also at https://alligator.io/angular/flex-layout/. const nav ...

Is it possible to successfully use the URL and query string with Netty combined with jQuery, yet still not receive any data when using

Here is the HTML code for my website: $.ajax({ type : "Get", url : "http://localhost:9999", data : {"servicename" :"test"}, timeout:100000, beforeSend: function(xhr) { }, success: function(rs) { alert("[success]" + rs); }, com ...

Firebug displays the class name and location twice in the right panel

When examining a div labeled with .mlm-clearfix, I noticed that Firebug was displaying this class name and its URL twice in the right panel. The style declarations given for the Easy Clear Method in relation to this class are as follows: .mlm-clearfix:bef ...

Pass the ID to a PHP script to retrieve the image source tag from the database based on the file

I am facing a challenge that I thought would be simple, but despite my efforts to research and experiment, I can't seem to get it right. I have a database that stores image file names, and I need to retrieve the file name based on the ID specified in ...

Checking the Length using JQuery

My dilemma is with a text field (datepicker) - I want the button to change color when someone selects a date, but it's not working. Can you please assist me? HTML <input type="text" id="datepicker"> <button type="button" onclick="" class=" ...

The React modal window stubbornly refuses to close

import c from '../Profile.module.css'; import { useState } from 'react'; import { createPortal } from 'react-dom'; import Modal from '../Modal/Modal'; const TransactionItem = (props) => { const modalRoot = ...

What is the process behind managing today's Google image of the day?

After coming across the javascript picture control on the Google search page, I became interested in replicating it myself. The feature zooms in on a picture when hovering over it, but I couldn't locate the HTML code they were using for it. Is there ...

Adjusting the alignment of button text in an Angular Kendo grid

I am currently utilizing the grid view feature of Kendo UI for Angular. While I have buttons on the grid, the issue is that the text within the buttons is not centered properly despite applying styles such as text-align:center. Here is the template for my ...

Loading an HTML page using jQuery's .load() method that contains embedded JavaScript

I'm facing an issue with loading .html pages (including JavaScript) into a div element. Below is my div setup: <div class="content"></div> This is my php code: <?php if(!$_POST['page']) die("0"); $page = (int)$_POST[&a ...

Arranging elements in a manner reminiscent of the tiles in Windows 8

Click here to view an example. I'm attempting to create a layout similar to Windows 8 tiles grid, where there are three types of tile sizes available: Normal = 1x1; Wide = 2x1; and Big = 2x2. My goal is to have these tiles displayed without any em ...

What's the issue with the jQuery each() function when selecting options?

Below is the function in question: function setDropdownValue(dropdownId,selectedValue){ $('#'+dropdownId+' option').each(function(){ if ($(this).val().toLowerCase()==selectedValue.toLowerCase()){ ...

Grin schedule module JSON stream

I have integrated a Smile timeline widget on my website and successfully customized it following several tutorials. However, I am struggling to utilize a Json service instead of relying on data stored in a global variable within a JavaScript file. Despite ...

Refresh jquery-ui ASP.NET Core MVC

I am in the process of updating jquery-ui.js within my ASP.NET Core MVC application. Upon inspection, I found that the file is stored in the following directory: https://i.sstatic.net/TMAeVTrJ.png Can anyone guide me on the proper procedure to achieve th ...

Integrating the Google Translate tool onto a website

My goal is to integrate a translation tool like Google Translator or other reliable options onto my website. I am not looking to translate the entire content of my pages into another language, but instead want to add a feature similar to the one found on t ...