What is the best way to update my logo and incorporate a colored border at the bottom of my fixed header while the user is scrolling down?

After spending countless hours researching online, I've been struggling to implement header effects on scroll without using JavaScript. My goal is to achieve a simple effect where the header reduces in height, the logo changes, and a colored border is added at the bottom. Despite my efforts to make it work by altering the header's height on scroll using JS, I couldn't get the desired response from the header. I understand that these ambitions might be challenging for someone new to web design like me, but I'm determined to learn how to accomplish this. Any guidance or recommendations for useful online resources would be greatly appreciated.

To avoid clutter, I'm only including my HTML and CSS here. I can provide additional details from my JavaScript file if necessary. I've also removed some CSS related to the unsuccessful JS attempts. Thank you in advance for your assistance.

 <header>
        <div id="nav">
            <div id="nav_left">
                <a href="index.html">HOME</a>
                <a href="services.html">SERVICES</a>
                <a href="portfolio.html">PORTFOLIO</a>
            </div>
            <a href="index.html" id="logo" class="noHover"><img src="images/logo_6_small.png" alt="Claire Crawford"
                    id="logo_Claire" /></a>
            <div id="nav_right">
                <a href="blog.html">BLOG</a>
                <a href="about.html">ABOUT</a>
                <a href="contact.html">GET IN TOUCH</a>
            </div>
        </div>
    </header>
header {
  height: 160px;
  position: fixed;
  top: 0;
  width: 100%;
  background: white;
  /* border-bottom: 15px solid rgb(197, 179, 55) */
}

/* header border on scroll
header.fixed.scrolled .header_bottom .container_inner {
  border-bottom: 1px solid #0a0a0a;
} */

/* header.sticky {
  height: 120px;
} */

/* header {
  transition: padding 300ms ease;
} */

Answer №1

Give this code a try in your project. It may suit your needs

$(function() {
  $(window).scroll(function() {
    if ($(this).scrollTop() > 200) {
      $('#logo img').attr('src', 'http://placehold.it/220?text=Original+Logo!');
      $("#nav").css("border-bottom", "none");
    }
    if ($(this).scrollTop() < 200) {
      $('#logo img').attr('src', 'https://cdn-images-1.medium.com/max/1600/1*4mdh6im57oFHSNY4syD_2Q.png');
      $("#nav").css("border-bottom", "2px solid red");
    }
  })
});
header {
  height: 160px;
  position: fixed;
  top: 0;
  width: 100%;
  background: white;
}

.wrapper {
  height: 1000px;
}

img {
  width: 100px;
}

#nav {
  width: 100%;
  display: flex;
  height: 110px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <header>
    <div id="nav">
      <div id="nav_left">
        <a href="index.html">HOME</a>
        <a href="services.html">SERVICES</a>
        <a href="portfolio.html">PORTFOLIO</a>
      </div>
      <a href="index.html" id="logo" class="noHover"><img src="https://cdn-images-1.medium.com/max/1600/1*4mdh6im57oFHSNY4syD_2Q.png" alt="Claire Crawford" id="logo_Claire" /></a>
      <div id="nav_right">
        <a href="blog.html">BLOG</a>
        <a href="about.html">ABOUT</a>
        <a href="contact.html">GET IN TOUCH</a>
      </div>
    </div>
  </header>
</div>

Here are the steps to implement this: 1. Include this in your index.html file. Remember to add jQuery for it to function properly.

<script src="code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
2. Copy and paste the provided code into your JavaScript file, then customize it according to your preferences.
3. I have set up a scroll function that triggers when you reach 200 pixels scrolled down. There are two conditions defined.

if ($(this).scrollTop() > 200) {
      $('#logo img').attr('src', 'http://placehold.it/220?text=Original+Logo!');
      $("#nav").css("border-bottom", "none");
    }

The above script changes the logo and navigation style when you scroll past 200 pixels from the top.

  1. $('#logo img').attr('src', 'http://placehold.it/220?text=Original+Logo!');
    This line targets the logo image within the specified div by its ID.

4.

$("#nav").css("border-bottom", "none");
Here, the border styling is applied to the navigation bar using CSS properties.

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

JavaScript Date behaving oddly

While working with Javascript, I encountered a puzzling issue involving the Date object. date1 = new Date(1970, 1, 1); date2 = new Date("1970-01-01T13:00:00.000Z"); console.log(date1.getYear()); //70 console.log(date1.getMonth()); //1 console.log(date1.g ...

Implementing ngFor to Iterate Through a JSON Object in Angular 6

Iterate through the object list retrieved from a JSON object Here is the JSON object that I have fetched: { "0": { "0": null, "1": "Consolidated Statements of Changes in Stockholders\u2019 Deficit", "2": null, "3": "", "4": "" ...

Creating a multilingual website with Nextjs 13 App Router using internalization, all without the need

As I develop a web app that requires user authentication to access, I am currently using Next.js 13 with the App Route (not Pages Route). My goal is to implement internationalization without having to create sub-routes like /en, or use domains such as mywe ...

Interact with a webpage element using Selenium

My goal is to extract information from the following page: I want to interact with each blue stats icon on the page (one for every match of the tournament). Below is the code I am using: from selenium import webdriver from selenium.webdriver.common.by im ...

How can I toggle input disablement in Bootstrap depending on switch selection?

I have been exploring the Bootstrap 5 documentation in search of a way to disable other input fields when a toggle switch input is set to 'off'. The parent element for this scenario is #member_form, and the switch itself is identified as 'to ...

What is the best way to display a header element in front of an article element?

I'm struggling with making my header element sticky and appear in front of my article. Modifying the z-index hasn't given me the desired result so far. Is the z-index ineffective when dealing with floating elements? Or is there a workaround to m ...

AngularJS: The delay in updating variable bindings following a REST API call

When utilizing a REST service, I wanted to implement a variable to show whether the service is currently loading or not. Controller $scope.loading = true; $http.get('/Something'). success(function(data, status, headers, config) ...

Using Ajax to dynamically generate column headers in Datatables

Is there a way to create a header title from an ajax file? I've been trying my best with the following code: $('#btntrack').on('click', function() { var KPNo = $('#KPNo').val(); var dataString = & ...

`Troubleshooting Firebase Cloud Functions and Cloud Firestore integration`

I previously used the following Firebase Database code in a project: const getDeviceUser = admin.database().ref(`/users/${notification.to}/`).once('value'); Now, I am attempting to convert it for Firestore. My goal is to retrieve my users' ...

"Endowed with improper dimensions, the BootStrap collapse feature

Yesterday, I posted about an issue with BootStrap and panel collapsables causing graph sizes to become distorted. The post was locked because there was no accompanying code. I have now created a code snippet for you all to see the exact problem I am facing ...

Exploring how to retrieve the input value from an element with ReactJs

Is there a way to determine if an input field contains a value by referencing another element? Here is my approach: <div className='input-item'> <input ref="accessKey" name="username" className="lci-text" type="text"/> & ...

What are the steps to integrate dynamic data into chartjs?

Can you assist me in understanding how to dynamically populate Chartjs with data from a json file? Specifically, I am looking to dynamically fill the labels and data fields. Sample JSON File <<< [ { "EFICAZ_TAB_ITEM_ID":1, " ...

Is there a way to identify when a radio button's value has changed without having to submit the form again?

Within this form, I have 2 radio buttons: <form action='' method='post' onsubmit='return checkForm(this, event)'> <input type = 'radio' name='allow' value='allow' checked>Allow ...

How can we efficiently add a new node to a polyline link in gojs while maintaining the original positions of points in the links adjacent to the inserted node

After posting a question on StackOverflow (Seeking Javascript library for displaying and editing networks of nodes and edges), I was directed to the gojs splice sample. The sample has been helpful, but I've hit a roadblock trying to achieve the speci ...

Is there a sleek way to create a JavaScript function that can provide two string values as output?

I am looking to store values in a specific format: "somekey": "value1", "value2" My goal is to create a function that takes in "somekey" and returns "value1", "value2". Alternatively, I could initialize a collection in my JavaScript file and reference i ...

Retrieve the child element that is being clicked

Alright, I'm facing a little issue here (it seems simple, but I just can't seem to crack it)... Let me paint the picture with a snippet of HTML code below: <!-- New Website #1 --> <!DOCTYPE html> <html style='min-height:0px; ...

Removing characters from a string with regular expressions

I need to eliminate instances of << any words #_ from the given text. stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ "; The d ...

What happens to the npm package if I transfer ownership of a github repository to a different user?

I was considering transferring a GitHub repository to another user or organization, but I have concerns about what will happen to older versions of the npm package associated with it. Let's say my Node.js package is named node-awesome-package. Versi ...

Developing UIs in React that change dynamically according to the radio button chosen

Problem Statement I am currently developing a web application feature that computes the heat insulation factor for a specific area. You can view the live demonstration on Codesandbox

Using Rails to Pass Front-End JavaScript Data from an External API to the Controller

I need to retrieve coordinates from an external API, specifically the Google Maps API, and then send it to my controller. However, I am encountering a 500 internal server error when using jQuery/Ajax for this task. Researching the issue online suggests tha ...