Manipulating pseudo-classes using jQuery

I am struggling with a CSS issue

div.borderYtoX a:before,
div.borderYtoX a:after {
  position: absolute;
  opacity: 0.5;
  height: 100%;
  width: 2px;
  content: '';
  background: #FFF;
  transition: all 0.3s;
}

I'm trying to dynamically change the background color when scrolling down, but it seems to be affecting my navigation menus as well. I have included some jQuery code below that I thought would work, but it's not functioning as expected. Is there another solution that could help me achieve this?

$(document).ready(function(){
   var scroll_start = 0;
   var startchange = $('#startchange');
   var offset = startchange.offset();
   $(document).scroll(function() {
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
          $('.header').css('background-color', 'rgb(255,255,255)');
          $('div.container a').css('color', '#063193');
          $('.borderYtoX a:before, .borderYtoX a:after').css('background', '#063193');
       } else {
          $('.header').css('background-color', 'rgba(255,255,255,0.3)');
          $('div.container a').css('color', '#fff');
          $('div.borderYtoX a:before, div.borderYtoX a:after').css('background', '#fff');
       }
   });
});

Answer №1

If you ever need to manipulate pseudo elements using JavaScript, remember that they do not truly exist within the DOM. To work around this limitation, consider using jQuery to add an override class to the element and then apply a CSS override for that specific class.

CSS

div.borderYtoX a.active:before,
div.borderYtoX a.active:after {

  background: #063193;
}

Script

$(document).ready(function(){
   var scroll_start = 0;
   var startchange = $('#startchange');
   var offset = startchange.offset();
   $(document).scroll(function() {
      scroll_start = $(this).scrollTop();
      if(scroll_start > offset.top) {
          $('.header').css('background-color', 'rgb(255,255,255)');
          $('div.container a').css('color', '#063193');
          $('.borderYtoX a, .borderYtoX a').toggleClass('active');
       } else {
          $('.header').css('background-color', 'rgba(255,255,255,0.3)');
          $('div.container a').css('color', '#fff');
          $('div.borderYtoX a:before, div.borderYtoX a:after').css('background', '#fff');
       }
   });
});

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

Encountering the error "fn.dataTable.moment is not a function" when trying to use momentjs with DataTables in Angular

Feeling a bit frustrated as I struggle with what seems like a simple issue to fix. Using a DataTable that is working fine, but now I want to make a column of dates sortable in the German date format dd.MM.yyyy. Found the documentation on the official dat ...

IE8 is having trouble with JavaScript errors

I recently downloaded a tooltip from the following website: . It worked perfectly on all updated browsers except for Internet Explorer 8. In IE 8, only the first tooltip loads and the others do not display. Below is the code snippet that I used: <img ...

Issue with Bootstrap 5: bg-customcolor and text-customcolor classes are not functioning as expected

Recently, I decided to update an old bootstrap page to the latest Bootstrap 5.3.1 version. Thanks to this amazing guide, I managed to configure my custom colors successfully: How to extend/modify (customize) Bootstrap with SASS However, I encountered a pr ...

`how to extract the href attribute value from the hyperlink that triggered a modal in jQuery UI`

Recently, I began diving into Jquery and javascript coding. As a beginner, it feels odd to refer to myself as a noob due to my age. Here's the scenario: I have a hyperlink that triggers a dialogue box and sets a cookie. The dialog asks the user, "Are ...

The text should be wrapped to overlap with an image positioned above it

Currently, I am in the process of enhancing a webpage that contains a significant amount of old legacy code. One issue I have encountered pertains to text wrapping within a fixed-height and width container, as illustrated in the image below: https://i.sst ...

Is your Chrome DevTools changing CSS Link files to "Constructed Stylesheet" after you edit the CSS using Inspect Element? Find out how to fix this issue!

This issue relates to CSS files that are initially not identified as constructed stylesheets but end up being displayed as such after editing, rendering the file inaccessible. Specifically in Google Chrome DevTools (last observed in Chrome 86): Whenever ...

Display a loading spinner while the search bar makes an API request in Angular

I'm struggling with incorporating a loading spinner display when a user enters a search term in the search bar. When there is a change detected in the search term property, an API request is made to populate the lists in the view with the relevant dat ...

Retrieve information from the index resource using AJAX

I feel like I might be overcomplicating things, but basically, I'm trying to retrieve all the data from an index resource and have it load when a button is clicked using AJAX. I've been using a serializer to tidy up the JSON. Both "/categories" ...

Incorporate a JavaScript file into a JSON document

Can I import a js file into a json file? I have a js file called config.js module.exports = {id : 'test'} I would like to utilize the id key in my json file, config.json const {id } = require('./config.js') {user_id : id} ...

The addEventListener function seems to encounter issues in IE11

There is a javascript function below that uploads the selected file and updates the grid. It works perfectly in Firefox, but there seems to be an issue with IE11. The "ESignature/Registration" function within the addEventListener does not seem to execute ...

Looking to add a JSON object to the Angular.JS controller's scope

Knowledge of Java Server Pages is not necessary to address the issue at hand; it simply provides context for the challenge I am currently encountering. I am in the process of developing an application in Adobe AEM that utilizes Java Server Pages to displa ...

The div element disappears upon calling the load() function

Currently, I am working to implement sorting criteria (such as by price or author) on my jsp page. I am utilizing ajax to refresh the content of a div when a new sorting option is selected. However, upon calling the reload function, the div just disappears ...

Creating a standalone module using webpack that can be accessed through a script element

When setting the library target to 'umd' in the package.json file, the expectation was that the outputs would be usable via a <script> tag and if no module system was available it would be attached to the window. However, this does not seem ...

Modifying the href in Django according to the current page URL: a step-by-step guide

Like the title suggests, I'm curious about the proper way to dynamically change a link's href based on the current URL that the user is visiting. I attempted a solution, but it proved unsuccessful. {% if url == '' %} href='/cooki ...

What is the process for assigning a background color to a specific option?

Trying to create a dropdown menu with various options and colors. Managed to set background colors for each option, but once an option is selected, the background color disappears. Is there a way to fix this issue? See my HTML example below: <select> ...

New solution for Java applet requiring communication with browser using JavaScript

Within our web platform, we have been utilizing a Java applet to interact with the MS Word application using jacob jar. This allows users to open, edit, and automatically upload files to the server upon saving. However, due to Google Chrome discontinuing ...

Invoking a HTTP request in a web service using the link method

Upon the initial page load, a successful RESTful call is made to retrieve data. However, when clicking on the left navigation links that display this data, another RESTful call needs to be made. I am attempting to achieve this functionality within the link ...

What is the reason for this JavaScript code not activating the input field (aspx)?

Thank you for taking the time to review this. I understand that for most of you client side scripting experts, this task is a breeze. However, all I am trying to achieve is to display a message at the top notifying the user that their click has been regist ...

Maintain the functionality of an object even when it is created using the "bind" method

I'm working with a function that has the following structure: var tempFun = function() { return 'something'; } tempFun.priority = 100; My goal is to store this function in an array and bind another object to it simultaneously, like so ...

Easily generate a hierarchical layout in HTML with this straightforward method

I'm currently working on implementing a hierarchical tree structure for a website. I need to organize a user's projects, tasks, and sub-tasks in a visually appealing manner using HTML elements. Any suggestions or creative ideas are welcome! ...