transition in navigation buttons' colors while scrolling upwards and downwards

Utilizing headroom.js allows me to cleverly hide my header while scrolling down and reveal it again when scrolling up. In the header section, I have placed my logo and navigation buttons.

When I am at the top of the page, I prefer my navigation buttons to be a darker color to contrast with the lighter background color. Conversely, as I scroll down, I want the button colors to change to a lighter shade to complement the darker header background color.

To achieve this effect, I have defined color classes in my CSS file:

.topColor {color: red;}

.scrollColor {color: white;}

Since I am working on this project using angularjs, I made use of the headroom.js angular module along with the specified options below:

angular:

<headroom id="header" tolerance="5" offset="205" classes='{"initial":"animated","pinned":"swingInX","unpinned":"swingOutX","top":"headroom--top","notTop":"headroom--not-top"}' > 

In my attempt to dynamically change the color of my navigation buttons, I experimented with two different approaches:

1.) Initially, I created a directive that checks if 'headroom--not-top' class is present and adds '.scrollColor' class to '#navColor'; otherwise, it adds '.topColor' class.

.directive('headroom', function(){
    return{
        restrict:'E',

        link: function(scope, element, attrs){

            if($(element).hasClass('headroom--not-top'))
            {
                $('#navColor').addClass('.scrollColor');
            }else{
               $('#navColor').addClass('.topColor');
            }
        }
    }
});

2.) Another approach I tried involved setting a variable based on the presence of 'headroom--not-top' class:

.directive('headroom', function(){
        return{
            restrict:'E',

            link: function(scope, element, attrs){

                if($(element).hasClass('headroom--not-top'))
                {
                    $scope.myNavColor = "scrollColor";
                }else{
                    $scope.myNavColor = "topColor";
                }
            }
        }
    }); 

Lastly, I included the navigation setup in HTML:

<li><a id="navColor" ng-class="{current: isCurrentPath('/')}"  active class = "{{myNavColor}}"  ng-href="#/">Home</a></li>

Unfortunately, neither of these attempts yielded the desired result. While the first approach successfully changed the button color to white initially, it did not update as I scrolled down. I suspect the issue lies in the recognition of $(element) or potentially an incorrect approach altogether.

Answer №1

I discovered a clever CSS solution to tackle this "problem"! Instead of using JavaScript, headroom.js relies on two classes to identify whether the header is positioned at the top of the page or if it's scrolling (not at the top). To address this, I implemented the following CSS code.

header.headroom--not-top ul li a{
    color: red;
}

header.headroom--top ul li a{
    color: white;
}

Answer №2

When I implemented this with the nav class from bootstrap, here's what worked for me:

nav.headroom--not-top  { color: red; background-color: lightgrey; opacity: .8 }
nav.headroom--top  { color: white }

It did the trick, but I couldn't help but think it would be even better if the text color changed to a complementary color of the background when the navbar descends. Maybe this could be achieved with jQuery...

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

Send data from input to controller without using $scope

I am encountering an issue with the code below. Typically, I would resolve this problem using $scope, but this time I have been asked to find a solution without utilizing $scope in the controller. I am implementing the "controller as" syntax for managing ...

Resetting suggestions for autocompletion in AngularJS

Currently implementing angularJS with autocomplete-alt. The functionality is working smoothly, but I've encountered an issue when clearing the autocomplete input using an AngularJS function - the suggestion list does not update accordingly. How can I ...

how to change the class of a div when clicking on a <p> element

Check out this code snippet: <p class="faq" onclick="toggleVisibility('answer1');"> How can I restrict email sending on my website? </p> <div id="answer1" class="hide"><sometext></div> these are my CSS styles! ...

Add some pizzazz to your design using jQuery!

I have been working on a web application using asp.net c#. I have a div element where I applied the following style to it at the server side: spanNivelRiesgo1.Attributes.Add("style", "display:none; visibility:hidden"); Now, I have a radiobutton list and ...

Angular Validation displays ng-valid when the form is actually invalid

I am currently working on a wedding RSVP form https://i.stack.imgur.com/Ct8Ux.png My objective is to hide the DONE submit button and only display it when the form is considered valid. <form method="POST" action="http://l.bheng.com:8888/wedding" acce ...

I aspire to deploy vue.js without relying on localhost or a web server

Recently, I completed a project using vue.js and successfully built it. However, upon trying to open the index.html page within the dist folder, all I encountered was a blank screen. After some investigation, I discovered that built projects must be host ...

Safari and Chrome have inconsistent line-height rendering, resulting in gaps between lines of

I have incorporated a <div> tag in my HTML with varying vertical spacing between lines of text. To achieve this, I am utilizing <br /> elements along with a CSS class that indicates the desired amount of spacing. For example, for a 5px gap, I ...

unable to construct a Yeoman Angular application

Having some issues while building an AngularJS web app generated with Yeoman Angular generator. Running grunt serve works just fine, but when trying to build the app with grunt, encountering errors like: Running "concurrent:dist" (concurrent) task Warning ...

Optimizing responsiveness and side margins for high-resolution displays

Struggling with creating a website design with Bootstrap. My goal is to add margins on the sides when the screen size increases, but once I achieved it, the responsiveness of the page got disrupted. To incorporate the side margins, I enclosed the entire b ...

Moving divs to a separate line in mobile display

I currently have a basic image-thumbnails landing page set up like this: <div style="display: inline-block;"><img style="margin: 3px 3px;" src="..." alt="" width="200" height="200" /></div> <div style="display: inline-block;"><i ...

Failure in AngularJS HTTP GET Request

My attempt at using Angular code to make a GET request to different APIs has been met with mixed results. While the code works perfectly when making a request to , it encounters issues when requesting from . The error message "SyntaxError: JSON.parse: une ...

It appears that IE 10 is having trouble displaying modals, possibly due to a compatibility issue with jQuery 1.7.1's animate function

By visiting www.carsense.com and clicking on the login link in the top-right corner, you may notice that the curtain appears but the modal itself does not display, even though it exists: If you locate id="content1" and modify the inline opacity to 1, the ...

What is the best way to toggle the visibility of a side navigation panel using AngularJS?

For my project, I utilized ng-include to insert HTML content. Within the included HTML, there is a side navigation panel that I only want to display in one specific HTML file and not in another. How can I achieve this? This is what I included: <div ng ...

What is the more effective option: utilizing the bootstrap offset feature or inserting an empty div?

<div class="col-xs-3"></div> <div class="col-xs-6 col-sm-6 col-md-6" style="min-width: 320px;"> </div> OR <div class="col-xs-6 col-md-offset-3 col-sm-offset-3 " style="min-width: 320px;">` </div> I would like to remo ...

Can anyone figure out why this code is not functioning properly? It seems to be targeting the body element and all of

Currently, I am utilizing jqtouch to create a mobile website. In addition to this, I am incorporating a gallery image slider into the website. However, I have encountered an issue where the images do not display when placed between <div id="project_name ...

Need help aligning a column class perfectly in bootstrap?

I'm currently learning Bootstrap 3 and I have some HTML code that looks like this: <ul class="row> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"></li> <li class="col-lg-3 col-md-3 col-sm-3 col-xs-6"></li> .. </ul ...

Encountering a 404 error while loading CSS in a Django application

I'm currently in the process of developing a chatbot application. I'm encountering issues with loading the CSS for the frontend using django's static data load feature. Here are the file paths: CSS path: C:\Documents\Django_Works ...

Leveraging the power of Angular's latest router alongside the modern syntax

Currently, I am experimenting with the NG6-starter package to enhance my knowledge of Angular 1.5 component development using ES6. The routing in this package is handled by stateProvider, however, I am eager to transition to using angular-new-router for ro ...

Looking to display or conceal several divs according to the option selected from a dropdown menu?

I've been searching for a solution to my simple dropdown issue, but haven't had any luck in the forums. This is the code for the dropdown: <select id ="category_faq"> <option value="1">item1</option> <option value= ...

Adding a dynamic CSS stylesheet at runtime with the power of Angular and Ionic 2

Currently, I am working on creating a bilingual application using Ionic2. The two languages supported are English and Arabic. As part of this project, I have created separate CSS files for each language - english.css and arabic.css. In order to switch be ...