The scroll header remains fixed in size despite being responsive

I've been struggling to resize a fixed header when the page is scrolled. Unfortunately, I'm having trouble getting the header to adjust its size as the scrolling happens.

$(document).scroll(function() {
  navbarScroll();
});

function navbarScroll() {
  var y = window.scrollY;
  if (y > 10) {
    $('.Header').addClass('small');
  } else if (y < 10) {
    $('.Header').removeClass('small');
  }
}
.Header {
  width: 100%;
  position: fixed;
  z-index: 1000;
  border-bottom: solid 2px #d8341f;
}

.Header--top {
  display: block;
  background: #fafafa;
  -webkit-font-smoothing: subpixel-antialiased;
  -moz-osx-font-smoothing: auto;
}

.tweak-site-width-option-full-background .Header-inner {
  max-width: 1140px;
  margin-left: auto;
  margin-right: auto;
}

.Header .Header--top {
  position: fixed;
  width: 100%;
  font-weight: bold;
  transition: .3s;
  // header shrinks when .small added to .Header
  &.small {
    height: 70px;
    box-shadow: 0 0 5px rgba(black, .2);
    // when Header.small also change page offset
    &~.offset {
      padding-top: 140px;
    }
    // when Header.small change logo pad & font size
    .header_logo {
      padding-top: 20px;
      font-size: 20px;
      text-shadow: none;
    }
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="Header Header--top">
  <div class="Header-inner Header-inner--top" data-nc-group="top">
    <div data-nc-container="top-left">
      <a href="/" class="Header-branding" data-nc-element="branding" data-content-field="site-title">
        <img src="//static1.squarespace.com/static/5aa819f2aa49a1758e33b803/t/5aa85d9cec212dd645589158/1524261284838/?format=1500w" alt="Red Corp USA" class="Header-branding-logo">
      </a>
    </div>
    <div data-nc-container="top-center"></div>
    <div data-nc-container="top-right">
      <nav class="Header-nav Header-nav--primary" data-nc-element="primary-nav" data-content-field="navigation">
        <div class="Header-nav-inner">
          <a href="/about/" class="Header-nav-item" data-test="template-nav">About</a><a href="/commercial/" class="Header-nav-item" data-test="template-nav">Commercial</a><a href="/residential/" class="Header-nav-item" data-test="template-nav">Residential</a>
          <a href="/hospitality/" class="Header-nav-item" data-test="template-nav">Hospitality</a><a href="/projects/" class="Header-nav-item" data-test="template-nav">Projects</a><a href="/contact/" class="Header-nav-item" data-test="template-nav">Contact</a>
          <a
            href="tel:+1.301.785.2400" target="_blank" class="Header-nav-item">Call Now</a>
        </div>
      </nav>
    </div>
  </div>
</header>

If you want to see an example of what I'm trying to achieve, take a look at . You can find my current site at for reference.

I would greatly appreciate any assistance on this matter.

Answer №1

Your JavaScript is correct, but you can simplify it by removing the anonymous function.

The issue lies in your CSS. The nested &.[class] syntax is specific to LESS, so ensure you are transpiling it correctly. Additionally, you should change .Header .Header--top to .Header.Header--top since these classes are on the same element in your HTML, not nested within each other.

Below is a revised example with minimal changes to your code and some Lorem Ipsum text to create a scrollbar effect. Keep in mind that this example may require further styling improvements:

function navbarScroll() {
  var y = window.scrollY;

  if (y > 10) {
    $('.Header').addClass('small');
  } else if (y < 10) {
    $('.Header').removeClass('small');
  }
}

$(document).scroll(navbarScroll);
.Header {
  width: 100%;
  position: fixed;
  z-index: 1000;
  border-bottom: solid 2px #d8341f;
}
/* Other CSS styles here */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="Header Header--top">
  <div class="Header-inner Header-inner--top" data-nc-group="top">
      /* Your HTML content here */
  </header>
  
  /* More Lorem Ipsum text goes here */

Answer №2

Modify the design file

.NavigationBar .TopNav {
...
}

to: (no spaces)

.NavigationBar.TopNav {}

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

How to use JQuery to inject a variable containing HTML into a specific div element?

Hey there, I have a basic webpage with a div element (let's name it #content) that holds HTML content I want to save to a variable. <div id="content"> <div id="left"> <h1>Awesome heading</h1> <p>text</ ...

In JavaScript, constructors do not have access to variables

Currently, I am attempting to implement Twilio Access Token on Firebase Functions using TypeScript. export const generateTwilioToken = functions.https.onRequest((req, res) => { const twilioAccessToken = twilio.jwt.AccessToken; const envConfig = fun ...

Vanish Dropdown upon clicking Using JavaScript

Hey, I'm new to web development and I'm currently working on creating a web app for iPhone. Everything is going smoothly except for one issue - my dropdown menu works perfectly on desktop Chrome, but on the iPhone's Safari browser, I can&ap ...

What is preventing my code from being recognized as a valid XML file?

Encountering an error indicating that this code snippet is not valid within an XSL file. This is the only feedback provided by the document system. While this code is a part of a larger document, I decided to isolate the issue to determine if it lies in ...

What are the steps to convert this code into Angular syntax?

After realizing that using jQuery alongside Angular is not a good practice, I am determined to modify my code in this Angular App to eliminate the need for jQuery. Currently, I have a page that displays menu items fetched from the server and I loop throug ...

Is there a way to create distance between these icons?

<div id="unique-icons"> <a href="#"> <img class="github-logo1" src="GitHub-Mark-64px.png" alt="first github logo"> </a> <a href="#"> <i ...

Conceal an absolutely positioned element outside its relatively positioned parent

I have a relative position parent element, <div style="position:relative" id="a"> and within that div, I'm creating some absolute positioned elements using jQuery, <div style="position:absolute;right:0" id="b"> These elements are anima ...

How can I enable drag-and-drop functionality for my carousel?

I'm encountering an issue with my carousel. I utilized a bootstrap carousel template and added my own images for the slides. I included jQuery to make it draggable, along with the necessary CDN links. However, while it is now DRAGGABLE, it no longer c ...

Tips for establishing a fixed point at which divs cease to shrink as the browser size decreases

There are numerous dynamically designed websites where divs or images shrink as the browser size decreases. A great example of this is http://en.wikipedia.org/wiki/Main_Page The div containing the text shrinks proportionally to the browser size until it ...

What happens to PHP echos when you send a post request to a web page?

I have a question that may seem silly to some. As someone who is relatively new to PHP, I am attempting to view echo statements from a page that I am posting to but not actually navigating to. Directly accessing the page's URL without the necessary po ...

Retrieving the counter input's value and displaying it in the corresponding span element

Today, I am working on a seemingly simple task. My goal is to retrieve the content from an input counter and showcase it within a <span>. I have successfully implemented similar functionality in other sections of my website, but for some reason, it ...

Using JQuery to swap out all URLs on a webpage?

Seeking guidance and examples on replacing all the links in the head of a document using jQuery. I am currently working with an app that requires my site to have SSL. To address this requirement without purchasing an SSL certificate or a static IP, I am a ...

Can you identify the distinctions between these two lines?

As a beginner in React, I recently learned how to create a timer counter following a tutorial. However, I am slightly confused about the difference between these two lines of code. Can someone please explain what is occurring in the first line below? lin ...

How to Properly Wrap Sub-Menu Elements in WordPress with Div Tags

I am currently working on styling my WordPress sub menu list items using HTML. I have written the HTML code that I want to use, but I am not sure how to incorporate it into the wp_nav_menu() function. Can you assist me with this? <ul class="menu"> ...

Utilizing jQuery to Enable Multi-Checkbox Selection

Looking for the simplest way to obtain the selected checkboxes. This is my script: $(document).ready(function() { $("input[name='chkTextEffects']").change(function() { if ($("#cbSolid").is(':checked') == true) { ...

What could be the reason for Python requests giving me a different text output compared to when I manually visit the website?

I'm in the process of creating a basic 'stock-checker' for a T-shirt that I have my eye on. The link to the product is available here: https://i.stack.imgur.com/usSJN.jpg Upon visiting the page, I noticed that it displays 'Coming Soon ...

Is there a way to include e.preventDefault() within an ajax call?

After the user clicks the submit button on my form, the handleSubmit function is triggered. However, I am having trouble calling e.preventDefault() inside my AJAX call due to its asynchronous nature. How can this issue be resolved? class FormRegister ex ...

Retrieve data from two separate files and store it as a two-dimensional array in JavaScript

Is there a way to read and convert two .txt files into a 2d array? I currently have a code snippet that looks like this : var fs = require('fs') file = './text1.txt' fs.readFile(file,'utf-8', (e,d) => { textByLine = d.s ...

A guide to entering information into an input field with JavaScript for logging in successfully

https://i.stack.imgur.com/TF51Z.gif https://i.stack.imgur.com/HHsax.png https://i.stack.imgur.com/HUztt.png When attempting to input text using document.getelement('').value="" , it doesn't behave as expected. The text disappear ...

Typescript implementation for a website featuring a single overarching file alongside separate files for each individual webpage

Although I've never ventured into the realm of Typescript before, I am intrigued by its concept of "stricter JS". My knowledge on the subject is currently very limited as I am just starting to experiment with it. Essentially, I have developed my own ...