Having trouble getting CSS animations to work in Safari when triggered by JavaScript

By utilizing a basic CSS animation, I've managed to create a fade-in effect that relies on opacity. To kickstart the animation, I use JavaScript to ensure it waits until the browser finishes loading. While this method works seamlessly on Firefox and Chrome, it encounters issues with Safari. Strangely enough, the animation functions properly on Safari when accessed locally, meaning from data stored on my computer! However, the problem arises when attempting to load website data from an external server using Safari.

I discovered that the animation also initiates correctly with an external server in play if I prompt Safari to display "web information" or activate the "responsive design" mode!

Despite testing on both Safari 9 and 11 (with cache cleared), I couldn't locate any existing discussions addressing this peculiar occurrence. Any insights would be greatly appreciated!

Here is the code snippet:

<!DOCTYPE html>
<head>
   <meta charset="utf-8">
   <title>test</title>
   <link rel="stylesheet" href="stylesheet.css"/>
   <script src="js-script.js"></script>
</head>

<body>
<main>
   <div id="fading">
   Test
   </div>
</main>
</body>
</html>

The CSS for the animation:

#fading {
   /* shorthand notation not always working with Safari */
   -webkit-animation-name: fadein;
   -webkit-animation-duration: 5s;
   -webkit-animation-timing-function: ease-in;
   -webkit-animation-play-state: paused;
   -moz-animation: fadein 5s ease-in;
   -moz-animation-play-state: paused;
   -o-animation: fadein 5s ease-in;
   -o-animation-play-state: paused;
   animation: fadein 5s ease-in;
   animation-play-state: paused;
}

@-webkit-keyframes fadein {
  from { opacity: 0}
  to   { opacity: 1}
}
@-moz-keyframes fadein {
  from { opacity: 0}
  to   { opacity: 1}
}
@-o-keyframes fadein {
  from { opacity: 0}
  to   { opacity: 1}
}
@keyframes fadein {
  from { opacity: 0}
  to   { opacity: 1}
}

And finally, the JS code:

"use strict";

function start() {
   var fading = document.getElementById("fading");
   fading.style.webkitAnimationPlayState = "running";
   fading.style.mozAnimationPlayState = "running";
   fading.style.oAnimationPlayState = "running";
   fading.style.animationPlayState = "running";
}

window.onload = start;

Answer №1

After much consideration, I have chosen to take a different approach by utilizing CSS transitions. This method seems more suitable for fading effects initiated by JavaScript and eliminates the Safari issue that was mentioned earlier. Additionally, combining JS and CSS requires fewer lines of code.

CSS:

#fading {
   opacity: 0;
   -webkit-transition: opacity 5s ease-in;
   -moz-transition: opacity 5s ease-in;
   -o-transition: opacity 5s ease-in;
   transition: opacity 5s ease-in;
}

JS:

"use strict";

function start() {
   var fading = document.getElementById("fading");
   fading.style.opacity = 1;
}

window.onload = start;

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

There seems to be a problem with completing the Axios post request in the Javascript Front

My front-end, built with React, is having trouble retrieving data from a third-party API through my Node.js backend. Despite using axios.post request, the response remains undefined and I'm stuck at handling the asynchronous request. It appears that t ...

In what way can data be retrieved from within a useEffect block externally?

Essentially, I'm facing an issue with retrieving data from the DateView component in my ChooseCalendar component. The code that retrieves this data is located within a useEffect block due to passing a dateType variable as part of a useState to the com ...

Is it necessary for me to install node and npm in order to use bower, even if I am not a node programmer?

As a Java programmer focusing on a web application, I'm finding that managing JavaScript can be quite challenging. I recently discovered Bower, a JavaScript dependency manager, but found out that it requires both Node and npm to be installed first. ...

Adding an onload event in a function-based component without using a fetch call

Looking at the code snippet below for a React component: import React from "react"; import { Carousel } from "react-bootstrap"; function CarouselHome() { return ( <> ...

View real-time data in Vuejs 3 as it executes

I am currently working on a form that populates a table with data retrieved from a Laravel API. I am using Vue.js 3 and Composition API to build my entire application. When the button is clicked, I want the table to be filled with data from the form. The b ...

Mouse over the image link and update the CSS text effect

I need help with a WordPress plugin and I'm trying to avoid making too many changes to the HTML output. Is there a way to make both the image and text change color when hovered over? You can see what I'm working on here - http://jsfiddle.net/3JE ...

Is there a way to alter the text color using JavaScript on the client side?

Is there a way to change the color of a list item in a ul based on whether it is a palindrome or not? Here is the JavaScript file I am working with: function isPalindrome(text){ return text == text.split('').reverse().join(''); } c ...

Contrasting the Javascript onload event with plain script within an html page

Can you identify the distinction between these two code snippets: Sample 1: <script type="text/javascript> function myfunc () { alert('hi'); } window.onload = myfunc; </script> Sample 2: & ...

Controller receiving empty object array from FormData

I am encountering an issue with my ajax call to the controller, where I am passing FormData() containing an array of objects and other properties. The list array that I pass seems to have 0 elements in the controller. Can anyone assist me with this problem ...

The React DevTools display components with the message "Currently Loading."

I am currently facing an issue with debugging some props used in my React application. When I try to inspect certain components, they display as "Loading..." instead of showing the normal props list: https://i.sstatic.net/RtTJ9.png Despite this, I can con ...

"Visualizing data with Highcharts: Utilizing percentages in a basic bar chart

I am working on a basic 1-series bar chart with nominal values for each bar. While I am able to plot it with data labels and axis showing the value of each bar, my goal is to display the percentage of the total series on these labels while keeping the nomi ...

Exploring the Potential of CSS Styling within Vue.js

I am in the process of creating a website and I am looking for a way to manage my styles through Vue. I want to be able to utilize CSS with Vue, as the style of .skill-bar serves as the background of the bar, while .skill-bar-fill represents the green fil ...

Navigate the JSON object at predetermined intervals, such as in the case of movie subtitles

Apologies if the title is not specific enough, open to any suggestions for improvement. Here's my issue: I have a JSON file (presented here as a JavaScript object) that contains subtitles for a movie. My goal is to display the text exactly as it appea ...

What steps should I take to ensure that the array yields the correct output?

Why is my code not creating an array of [0, 1, 2] when I pass the number 3 as a parameter? const array = [0]; const increment = (num) => { if (num > 0) { increment(num - 1); array.push(num); } return; }; console.log(array); incremen ...

Utilize Axios to send data in real-time to update any changes made to an input field in

Just wanted to write about this topic because I have a burning question. (Please note that the code in this post is just an example). I'm struggling with sending the input content of name_contact on each change without using a button. Is it even poss ...

Delete the click event listener that was previously assigned by a different script

After extensive investigation using the Chrome developer tools, I have identified a click event listener that needs to be removed: https://i.sstatic.net/dnB3Q.png I successfully removed the listener using the developer tools. Further analysis revealed th ...

Steps for adding an npm dependency as a peer dependency:

Is there a way in npm to install dependencies as peer-dependencies similar to the yarn option --yarn, rather than manually adding them? For instance: "peerDependencies": { "@angular/core": "^7.0.0" } Update: Further clarifi ...

The express js backend (mongodb) is returning {error:{ }}

I'm learning how to POST Data to a Mongo DB using Express.js. Recently, I picked up Express.js for backend server development and this is my app.js const express = require("express"); const mongoose = require("mongoose"); require("dotenv/config") con ...

Three.JS: Utilizing a wireframe material for spheres with the EdgesHelper control

I'm exploring Three.JS for the first time and I have some doubts about whether it's the best tool for my current project. My goal is to create a wireframe material on a simple spherical geometry that looks like this: https://i.sstatic.net/WPfAh ...

Determine the number of times a user has accessed a page on my website

Is there a way to track how many times a user loads pages on my website using JavaScript? Could it be done like this? var pageLoads = 1; $(document).ready(function(){ var pageLoads ++; }); Should I store this information in a cookie? ...