Incorporating specialized pseudo CSS styling to a class

Having a bit of a tricky situation here, I am dealing with the following class:

<li><a href="test.html" class="hvr-overline-from-left">Test</a></li>

Here is the corresponding CSS:

.hvr-overline-from-left {
  display: inline-block;
  vertical-align: middle;
  box-shadow: 0 0 1px transparent;
  position: relative;
  overflow: hidden;
}
.hvr-overline-from-left:before {
  content: "";
  position: absolute;
  z-index: -1;
  left: 0;
  right: 100%;
  top: 0%;
  background: #47953F;
  height: 4px;
  -webkit-transition-property: right;
  transition-property: right;
  -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
}

Desired change when scrolling down:

.hvr-overline-from-leftsmall:before {
  content: "";
  position: absolute;
  z-index: -1;
  left: 0;
  right: 100%;
  top: 15%;
  background: #47953F;
  height: 4px;
  -webkit-transition-property: right;
  transition-property: right;
  -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
}

Attempted solution:

$(document).ready(function($){
    var line = $('.hvr-overline-from-left:before');

    $(window).scroll(function () {
        if ($(this).scrollTop() > 13.35) {
            line.addClass("hvr-overline-from-leftsmall:before");
        } else if($(this).scrollTop() >= 0) {
            line.removeClass("hvr-overline-from-leftsmall:before");
        }
    });
});

Unfortunately, this does not work as intended. My question is how can I make this work correctly? I am unsure about which class I should use to modify pseudo elements.

Answer №1

To apply the effect, simply include the class called hvr-overline-from-leftsmall, without the need for the :before selector. Here is an example:

line.addClass("hvr-overline-from-leftsmall");
// Exclude :before in this instance -----------------------^

(Remember to also remove the class using removeClass when needed.)

Make sure that the hvr-overline-from-leftsmall rules are defined after the hvr-overline-from-left class to ensure that it overrides any conflicting styles.

It is important to note that the :before element is part of the rule selector, not the class itself.

Answer №2

If you want to update the class without using the pseudo-element, you can do so easily:

if ($(this).scrollTop() > 13.35) {
    line.addClass("hvr-overline-from-leftsmall");
} else if($(this).scrollTop() >= 0) {
    line.removeClass("hvr-overline-from-leftsmall");
}

When you change the class, the ::before will automatically be applied, overriding any existing hvr-overline-from-left styles.

Answer №3

You don't need to overhaul all the styles in this scenario. Modify your CSS as shown below:

.hvr-overline-from-left {
  display: inline-block;
  vertical-align: middle;
  box-shadow: 0 0 1px transparent;
  position: relative;
  overflow: hidden;
}
.hvr-overline-from-left:before {
  content: "";
  position: absolute;
  z-index: -1;
  left: 0;
  right: 100%;
  top: 0%;
  background: #47953F;
  height: 4px;
  -webkit-transition-property: right;
  transition-property: right;
  -webkit-transition-duration: 0.3s;
  transition-duration: 0.3s;
  -webkit-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
}
.hvr-overline-from-left.small:before {
  top: 15%;
}

Then, simply add or remove the small class in your script:

if ($(this).scrollTop() > 13.35) {
    line.addClass("small");
} else if($(this).scrollTop() >= 0) {
    line.removeClass("small");
}

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

I am looking to expand the width of the Bootstrap container specifically for the Max width

I need to adjust the width of the Bootstrap container to be responsive on all devices, especially maximizing the width on larger screens. Can anyone provide guidance on how to achieve this? Thank you in advance. To view my website, please follow this link ...

Switching between multiple images using Jquery on a click event

Hi there, I am currently working on a project where I need to use jQuery to switch between three images when clicked. Once the third image is clicked, it should cycle back to the first picture. I was wondering if there is a way to modify the code below so ...

Why is Django saving Model B and C data in Model A incorrectly?

I'm encountering an issue with the Django models CustomerDetail, CarrierForm, and InfluencerModel. Whenever I attempt to save data in the CarrierForm or InfluencerModel through different forms on various pages, the data is getting saved in the Custome ...

Saving a user with a BLOB avatar in Angular 5: Tips and Tricks for Success

As a newcomer to Angular, I am trying to figure out how to properly save a new user with an avatar. Can someone help me understand how to pass the Blob value of the avatar to my user Model for successful saving? Below is the code snippet I am working with ...

The functionality for determining whether the value isset as 'Yes' or 'No' is not functioning properly in my PHP form

I'm currently working on creating a combined 'Order Calculator / Order Form' using one php form. The calculator part of the form is functioning perfectly and accurately calculates the total for all 5 order options both on the live page and i ...

jQuery is failing to properly render dynamic content data identifiers

Need help with a dynamic HTML div <a data-id="17" onclick="getcustomer();"> <div class="note note-success"> <h4 class="block">A</h4> <p>Email : <a href="/cdn-cgi/l/email-protection" class="__cf_email__" ...

Increase jQuery value by X each time it is clicked

I'm trying to create a custom next/prev navigation where clicking the "next" button will animate the margin left by X on each click until reaching the end (-320px), with a similar method for the back button. Here is the JavaScript code I've been ...

How to dynamically add table rows and cells with JavaScript using a single selection input

I am working on a project that involves a selection input with around 5 options. Additionally, there is an empty table that follows this format: <table> <tr> <td>X (for deletion)</td> <td>Option name</td> ...

employing personalized CSS styles

When I implement the following code: <div class="metanav"> ... </div> Every element inside that div will inherit the styling of .metanav. But if I duplicate .metanav in the CSS and rename it to A; then update the div's class to: <d ...

Gradient in Half Circle With JavaScript

How can I achieve a gradient effect within a circle using the following initial code: HTML: <div class="progresss"> <div class="barOverflow"> <div class="bar"></div> </div> <span>10</span>% </d ...

Generate a new Div dynamically, positioned higher than the rest

Once a second, this script will utilize AJAX to open a new page and display the contents in a dynamically created div on this page. However, the issue is that the new divs are stacking under each other, and I would prefer them to be added at the top instea ...

What is the best way to pass a model bound to a view from an ajax request triggered by clicking a button to a controller

Below is the code snippet for my AJAX call: $('#addNominationForm').on("submit", function (e) { debugger; e.preventDefault(); $.ajax({ type: 'post', url: '@Url.Action("AddNominat ...

How can CSS grid be used to move a particular <td> element?

Currently, I am working on creating a dynamic table using CSS grid and I am looking to slightly adjust the left-most <th> and <td>. I am uncertain about how to move a specific element within a <table>. My goal is to achieve a layout simi ...

Encountered JSON array, unable to retrieve certain attributes in AngularJS

I have developed a web service that organizes my information into a list and then converts it to JSON format. Below is the JSON output: { GetEventLTResult: [ { eventID: 1, location: "Place A", type: "Communi ...

What is the process for creating a personalized <a> tag using CSS?

On my website, I am looking to differentiate between two types of <a> tags - one for inline links and the other for standalone links. This means that I want these two variations of <a> tags to have distinct colors and styles. For inline links, ...

The click event will only be activated upon refresh

Here's my issue: I've set up a page with some buttons and a jQuery script that looks like this: $( document ).bind( 'mobileinit', function(){ $.mobile.loader.prototype.options.text = "loading"; $.mobile.loader.prototype.option ...

Retrieve the count of ng-if directives that match a specific ng-repeat content

Imagine a scenario where HTML code looks like this: <md-grid-tile class="gray" ng-repeat="carto in cartoList" ng-if="search(carto)"> <md-button ng-click="changeSVG(carto.fileName)" aria-label="carto.displayName"> <img ...

Adjust the height attribute of a DIV element within a webpage that contains a "footer"

I'm hoping to achieve a scrollable middle section without the bottom scrollbar, and I prefer a CSS solution over using JavaScript for this. On my website, I have a 150px high div filled with icons and images that needs to be fixed while allowing the r ...

obtaining the controller output in JavaScript

Scenario: Retrieving id in javascript following an ajax post I'm trying to figure out how to access the id sent from my controller's ActionResult in javascript. I've experimented with both Content Result and JSON Result on the controller, b ...

Coordinate the timing of CSS animation with the loading of the page

Despite the abundance of similar questions, none have quite addressed my specific query. I've created a preloader using CSS animation and I want it to synchronize perfectly with the page load. While I can trigger the animation on page load, I'm s ...