Effortlessly switching classes in JavaScript for seamless transitions

I'm currently working on an animation project where I want the title to be visible on the page initially, and then slowly fade away as you scroll down, with a subtitle fading in right after. While I've managed to get the title part working, I'm struggling to smoothly transition the appearance of the subtitle. Initially, the subtitle is set to "visibility:hidden," but when I scroll and the JavaScript adds the transition-in class, it seems to abruptly appear without honoring the transition property I assigned. You can find the fiddle I created here. Below, you'll see the JavaScript and CSS code I'm using for this animation. If there are any easier methods to achieve this effect, please feel free to share. Any advice or assistance would be greatly appreciated as I've been experimenting and researching with no success.

Javascript

const element = document.getElementById('title');
const element2 = document.getElementById('subtitle');

window.onscroll = function() {

  console.log("document element");
  console.log(document.documentElement.scrollTop);
  console.log("scrolling elemnent");

  if (window.scrollY > 0) {
    element.classList.add('fadeout');
    element2.classList.add('fadein');
    console.log("hello");
  }
}
.fadeout {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 2s, opacity 2s linear;
}

.two {
  visibility: hidden;
}

#subtitle {
  transition: opacity 2s linear;
}

.fadein {
  visibility: visible;
  opacity: 1;
  transition: opacity 2s linear;
}

Answer №1

At the moment, your subtitle appears at full opacity as it fades in (This is because the visibility property does not control opacity, it simply hides the element)

To achieve a fading effect, add opacity:0; to the .two CSS.

Check out the updated fiddle https://jsfiddle.net/s2cban6q (line 32 of CSS has been modified)

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

The PKIJS digital signature does not align with the verification process

Explore the code snippet below const data = await Deno.readFile("./README.md"); const certificate = (await loadPEM("./playground/domain.pem"))[0] as Certificate; const privateKey = (await loadPEM("./playground/domain-pk ...

Can you remind me of the specific CSS class used for Internet Explorer in Twitter Bootstrap?

Currently utilizing Twitter Bootstrap and curious if there is a specific CSS class to designate IE browsers within the BODY or HTML tags. Interested in writing CSS specifically for all IE browsers. Aware that Bootstrap offers some CSS classes for IE brows ...

Enhancing readability in a vertical CSS menu with proper spacing

I'm looking to increase the spacing between each menu item as they appear too close together. Can someone help me with managing this? The menu names are currently right under one another, almost touching. What is the best way to create more space betw ...

Prevent the SnackBar from extending to full width in case of a smaller window size

Is there a solution to prevent the SnackBar from spanning the entire width of the screen when the window is narrow? ...

Take away the follow option on the embedded tweet

I need to customize the appearance of embedded tweets on a website by removing the follow button as well as the reply, favorite, and retweet buttons in the footer. Here is a simple HTML example: <blockquote class="twitter-tweet"> <a href="htt ...

Eavesdrop on outbound requests on a web page using JavaScript

As I develop a single-page application that integrates a third-party analytics tool (such as Heap, Segment, Mixpanel, etc.), I am looking to actively monitor the outgoing HTTPS requests from my app and take necessary actions, such as logging. I am curious ...

Is the jquery autocomeplete plugin malfunctioning when using numbers in the input?

I encountered a requirement to display stock number suggestions within a search box. To achieve this, I decided to implement the Jquery autocomplete plugin. Through an ajax call to a function in my cfc, I was able to retrieve all the stock numbers and stor ...

Three.js tutorial: Rotating items on each axis individually

Looking to create an interactive 3D item that users can rotate with their mouse freely? I experimented with using three quaternions, one for each axis, and then multiplying them together to determine the final position of the item. // Initializing xQuat, y ...

Reveal a concealed div in a modal form depending on the value present in the input field

After spending the past 4 hours scouring through Google and SO, I finally stumbled upon an answer HERE that seemed to be somewhat close to what I was looking for. However, in my scenario, the <div class="fhlbinfo"> elements are not displaying or hi ...

What makes Angular date pickers sluggish?

Have you ever noticed that Angular JS date pickers consume a lot of CPU? When multiple date pickers are present on a page, they can noticeably reduce the site's speed. Is there a way to minimize this issue? Take for example the official Angular for ...

Is it possible to utilize WebAssembly in JavaScript to access the memory of a C struct directly?

Looking at the C code snippet below, there is a struct defined as follows typedef struct { ValueType type; union { bool boolean; double number; Obj* obj; } as; } Value; The ValueType enum used in the struct is defined a ...

Guide to dynamically incorporating items from an array into the filter operation

How can I dynamically insert items from an array into the filter on the wizard.html page, so that when the templateUrl in the route is called, it can be rendered on the wizard.html page? Controller angular.module('tareasApp') .controller(&apo ...

Issue with Django Form not showing up on HTML after encountering an error

I have a question regarding my Django project. When a user sends a GET request, I need to display a form on the page. Here is the initial code snippet: form = SignUpForm() if request.method == 'POST': .... else: return render(request, & ...

I am looking to remove all white spaces from my website's HTML code

I am looking to remove all white spaces from my website in html. The more I align my text to the right, the more whitespace it creates on the right side of the page. As seen in the images, there are a lot of whitespaces and I suspect that elements are caus ...

PHP code that removes a table row from a database

Can anyone help me troubleshoot my code? My PHP seems to be functioning, but for some reason the delete button is not working! if(isset($_POST['delete'])) { $ID = $_POST['value']; $delete = "DELETE FROM tbl_document WHERE ID = ...

Create a new array by dynamically generating a key while comparing two existing arrays

One of the features in my app involves retrieving data from an API and storing it in $scope.newz. The previous user activity is loaded from LocalStorage as bookmarkData. I am facing a challenge with comparing the contentId values in arrays $scope.newz an ...

Safari experiences a decrease in speed when multiple checkbox elements are present

While using Safari, I have encountered a problem. The performance of interacting with a text input is significantly affected when there are numerous type="checkbox" elements on the page. This issue appears to be more pronounced in Safari compared to Chrom ...

Firestore data displaying as null values

Recently, I encountered CORS errors while polling the weather every 30 seconds in my program. Upon investigating, I discovered that the city and country were being interpreted as undefined. To fetch user data from my users' table, I utilize an Axios ...

What is the best way to manage files in Vue.js?

I am faced with a challenge in storing image files and video files as Blob data. One thing I like is that the uploaded video and image files display instantly, which is pretty cool. I'm not entirely sure if the code below is correct - how can I navi ...

Is it possible to clear a div using jQuery and then restore its contents?

In my setup, I have a video player within a div where clicking the play button (.video-play-container) fades in the video, and clicking the close button (.close-video) fades it out. The issue arose when the faded-out video continued playing in the backgr ...