How to create a scrolling effect that fades a pseudo element using either JavaScript or jQuery

Seeking help on how to fade a pseudo element to opacity:0 on scroll has left me feeling lost and frustrated. I've included my codepen for reference here: http://codepen.io/emilychews/pen/JWyaKr

Unfortunately, Greensock is not an option for this project, and I must use a pseudo element rather than an absolutely positioned div. The desired effect is for the fade to occur after 10px of scrolling from the top, with the element reappearing when the user scrolls back up (part of a navigation element).

HTML

<div id="mydiv">My Div</div>

CSS

#mydiv {
    background: red;
    width: 10%;
}

#mydiv:after {
    content: '';
    position: absolute;
    height: 10%;
    width: 10%;
    top: 30px;
    background: black;
}

I would greatly appreciate any suggestions or assistance. Right now, I feel like I'm at a breaking point and could either burst into tears or devour a bucket of fried chicken in frustration.

Sincerely, Emily

Answer №1

Implement a smooth transition effect for the opacity property on a pseudo element by adding a specific class to the main element when scrolling occurs. This class will be used in the CSS selector to control the opacity of the pseudo element.

var $mydiv = $('#mydiv');
$(window).scroll(function() {
  if ($(this).scrollTop() > 10) {
    $mydiv.addClass('fade');
  } else {
    $mydiv.removeClass('fade');
  }
})
body {
  height: 200vh;
}

#mydiv {
  background: red;
  width: 10%;
}

#mydiv:after {
  content: '';
  position: absolute;
  height: 10%;
  width: 10%;
  top: 30px;
  background: black;
  transition: opacity .25s;
}

#mydiv.fade:after {
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mydiv">My Div</div>

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

Sending data from Vue js 2 to a JSON object

Hey there! I'm diving into Vue.js for the first time and decided to learn by creating a simple blog/article system where users can add articles that are displayed on the page (and stored in a JSON file). Here's what I've accomplished so far ...

Struggling with D3.js Angular CLI where Scope disappears within the tick() function?

Hey everyone, I'm currently in the process of incorporating a D3 visualization network graph into an Angular CLI project (http://bl.ocks.org/mbostock/1153292) using the ng2-nvd3 component. Here's the Angular component: import { Component, OnIn ...

Using Modal Functions in AngularJS Controller

I have been working on a project that utilizes the ui.bootstrap. As per the instructions from the tutorial I followed, my setup looks something like this: 'use strict'; angular.module('academiaUnitateApp') .controller('EntryCtr ...

Managed the double-click event to select Snap.svg text

I am currently utilizing the snapsvg library for a project where I am implementing the dblclick event to trigger a browser window alert. However, when clicking on the svg canvas, not only does the alert pop up but some text on the canvas also gets selected ...

Enhancing the Google Visualization Table with Custom CSS

After creating a table in Google Visualization with the provided code, I am struggling to customize the formatting of the header row and table rows using CSS. Despite going through the Configuration Options, I am unclear on how to proceed and would appreci ...

Creating figures within an elliptical boundary

While this could be phrased as a mathematical problem, I am dealing with a programming challenge. In my case, I am working with a List containing an unknown number of elements (cluster nodes) that is only revealed once I receive the JSON data from the ser ...

Tips for making sure a header is consistently at the top of every page during printing

I need help with my website - I have a table that is quite tall and spans across multiple pages when printing. Is there a way to make the header row appear on top of each page when printing? ...

Using Django in combination with the JQuery $.post() function

I'm currently facing an issue with the $.post() function in relation to my views. Despite receiving no errors, the JavaScript is not returning any output. It seems like it's either failing to reach my view or my view isn't calling the templa ...

Adding text over an image in Tailwind without the need for absolute positioning is achievable

I'm struggling to position text over an image without using absolute positioning. It's working fine on large and small screens, but I'm having trouble with medium screens. I've searched for similar questions on Stack Overflow, but none ...

Transfer PDF file to user's web browser through XMLHttpRequest, utilizing up-to-date HTML5 techniques, all while maintaining the integrity of the document's encoding

I am trying to achieve a similar result as described in this StackOverflow post about handling file download from ajax post. However, I'm working with a dynamically-generated PDF file created using PHP v5.6.10 (with the PDFLib extension, v9.0.5). Unf ...

Struggling to transfer an image to a Linux server using Phonegap and PHP

An application being developed using PhoneGap requires an image to be uploaded to a Linux server. To test this process, a CentOS 7 VM was set up and the LEMP stack was installed and configured on it. A php script named upload.php was created (shown below), ...

Using play() in HTML Audio prevents the IOS music player from playing

When developing a timer app, I am currently using html audio to play a beep sound by utilizing the audioElement.play() function. The user must interact with the screen to enable sound (press the unmute button). This particular web application acts as a fit ...

Every time I hit the refresh button, I find myself forcefully logged out

After switching from using localStorage to cookies in my React JS web app, I am experiencing an issue where I get logged out whenever I refresh the page. Even though the cookies are still stored in the browser, the authentication process seems to be failin ...

Issue with rendering in React was encountered

I am a newbie in react and I want to display each new message, but in the render function I need to set an index in render_mess to show messages from an array. Is there a way to display all the elements without using a loop? class New extends React.Compon ...

Acquire the Information from a Textarea That Has Been Edited by the User

My challenge lies in taking user-entered text from a content editable textarea and sending it via POST request, but the fields edited by the user are returning with an empty textContent. The code snippet below shows that each .entryRow (obj) contains multi ...

I'm having trouble locating the documents I recently saved in my mongoose model

Here is my current challenge: I am populating a collection called "fruits" with some values. Once the data has been inserted, I want to use map to console.log each fruit individually. However, during the first attempt, instead of displaying a single fruit ...

Utilize a fusion of various subjects and observables

I have the below code snippet: readonly todos$: Observable<Todo[]> = combineLatest([this.account$, this.loadTodoPage$]).pipe( concatMap(([account, pageNo]) => this.todoService.getTodos(account.id, pageNo)), scan((acc: Todo[], curr: Todo[ ...

Sending a model from a View to a boolean method using AJAX in ASP.NET MVC

I am looking to pass a model from BeginForm to a boolean method in the Controller. If the boolean method returns TRUE, I want to refresh a partial view using AJAX. Otherwise, if it returns FALSE, I want to display a pop-up with information from the method ...

The columns in Bootstrap 4.00 are staying on the same line and not wrapping to a new line

I'm a bit confused about the behavior I'm seeing. When I try to make the browser window narrower, instead of the columns moving to the next line as expected, they are overlapping each other. What could be causing this issue? I wonder if it has s ...

A guide on incorporating a customized Google map into your website

Recently, I utilized the Google Map editing service from this site: https://developers.google.com/maps/documentation/javascript/styling This link provided me with two things: 1. A JSON code 2. The Google API link However, I am unsure about how to incorpo ...