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

Removing a data entry from a PHP-generated MySQL table

In the process of developing a system, I have encountered a need to display database records in an HTML table. The functionality for creating the HTML table and retrieving data is working as expected. However, I am facing an issue with adding a column cont ...

When attempting to access index.html, the Express Server responds with a "Page Not Found"

I have encountered a problem while trying to run my Index.html file through an Express server. Despite referring to previously asked questions, I couldn't resolve the issue. The error message 'Cannot GET /' keeps popping up. Below is the sn ...

What is the best way to handle waiting for a request and user input simultaneously?

Imagine a scenario where a component loads and initiates an asynchronous request. This component also includes a submit button that, when clicked by the user, triggers a function that depends on the result of the initial request. How can I ensure that this ...

A guide on clearing the selected value in a dropdown menu with Angular.js

Can someone assist me with setting the drop-down value to blank after completing an action in Angular.js? Below is my code explanation: <div style="height:270px; overflow-x:hidden; overflow-y:scroll;" ng-show="viewOrderTable"> <div class="table-r ...

Utilizing jQuery to scrape HTML and dynamically manipulate elements

Is there a way to retrieve HTML content from a different subdomain and incorporate it into the current site? HTML snippet: <head> <body> <div id = "items"> <div id = "#one"> <ul> <li><a href = "#">C ...

Issue in Angular: Attempting to access properties of undefined (specifically 'CustomHeaderComponent')

I have encountered a persistent error message while working on a new component for my project. Despite double-checking the injection code and ensuring that the module and component export logic are correct, I am unable to pinpoint the issue. custom-header ...

The radial gradient in d3.js does not properly affect paths

My goal is to create a radial gradient on my path element, but for some reason the radial gradient does not apply correctly. Can anyone help me troubleshoot this issue? Below is my updated code: // Define the canvas / graph dimensions var margin = {top: ...

Prevent dragging events while clicking on a link

Recently, I encountered a drag event over an attached div.image element. Whenever I click and hold the mouse down on the div, the drag event initiates. In order to achieve this functionality, I utilized the nestable.js plugin. However, I am facing a chall ...

Using AngularJS to access JSON files through the $http service

I'm experiencing difficulties reading data from my test.json file using the #http service. I have everything set up on a xampp localhost, but I can't seem to figure out what's going wrong. Here's the JavaScript code. Thank you in advanc ...

Tips for seamlessly incorporating WalletConnect into your decentralized app with the help of web3-react

I have been working on integrating WalletConnect into my project by referring to the documentation provided by web3-react. The configuration settings I am using for the connector are as follows: import { WalletConnectConnector } from '@web3-react/wal ...

Difficulty in dynamically updating custom attributes data in a popover

I am attempting to dynamically insert text into a Bootstrap 3 Popover data-content="" on this demo. My goal is to update the text in the data-content="" attribute by clicking different buttons. I have tried doing this using the following code: $("#poper") ...

Displaying PHP strings with spacesFeel free to let me know

I have two files named "index.php" and "output.php". In the index.php file, there is a dropdown list populated with all Italian city names fetched from a MySQL table. The objective of my test case is to select a city from the dropdown list and send its val ...

Experiencing difficulties with Magento operations

Currently facing an issue while attempting to set up Magento on my server. I have transferred all files from another server to mine, but now encountering an error. Could this be related to the symlinks I generated or is it caused by something else? Encoun ...

Excessive content in Bootstrap row extends outside browser window dimensions

I'm having an issue with the Bootsrtap row overflowing past the desired width, causing a scrollable area on the browser. Does anyone know how to fix this? You can see the site here: www.testing.cpukeychains.comhttps://i.sstatic.net/cDcot.png ...

Writing the success function for a jQuery ajax call involves defining the actions to be taken once

Embarking on my journey to learn jQuery and web development, I am faced with the task of sending user input (username and password through a submit button) to a PHP page using .ajax and success function. Below is the HTML form code: <form id="form1"&g ...

Display or conceal certain HTML form elements based on the selection made in the previous form element

I need assistance with a function that can dynamically show or hide certain HTML form elements based on the user's previous selection using JavaScript. For example, if a user selects "Bleached" from the Dyingtype drop-down menu, there is no need to di ...

One class seems to be causing issues with hover functionality while the other works perfectly fine

HTML: <div class="channellist"></div> Through the use of Ajax, I am able to dynamically retrieve channels when the page loads and then append them within the channellist container. Once appended, my HTML structure appears as follows: <div ...

The HTML link is not directly attached to the text, specifically in Internet Explorer

When viewing my website in Chrome, the menu links in the Header function correctly. However, in Internet Explorer (specifically version 11), hovering over 'HOME,' 'LISTINGS,' or 'AGENTS' reveals that the displayed URL is incor ...

Discovering the presence of line breaks

I have searched extensively on Google, but I am struggling to find a definitive answer. My goal is to create an email simulation where users can input text into a textarea and save it to the database. They should then be able to return to the page later a ...

Limiting zero is ineffective when it comes to pop-up issues

Hey there, I'm looking to prevent users from inputting zero and dot in a specific field, which is currently working fine. However, when the field is within a pop-up, the code below doesn't seem to work. <script> $('#name').keyp ...