What is the best way to reset a CSS background GIF after hovering over it?

Is there a way to create a texture animation with text using CSS background GIFs that reload when hovered again? I've tried some JavaScript/jQuery but can't seem to make it work. Any suggestions?

I attempted the following approach, but it's not resetting the background:

$(document).ready(function() {
  $(".hoverClass").mouseleave(function() {
    $(this).removeClass("hoverClass");
    setTimeout(function() {
      $(this).addClass("hoverClass");
    });
  });
});
.navMenu2 li {
  color: #0e0e0e;
  font-size: 10rem;
  font-weight: 600;
  list-style-type: none;
  transition: background-image 2s ease-in-out;
}

.navMenu2 a {
  text-decoration: none;
  color: #0e0e0e;
}

.hoverClass:hover,
.hoverClass:focus {
  -webkit-text-fill-color: transparent;
  background: -webkit-linear-gradient(transparent, transparent), url(https://media.giphy.com/media/l2QDSTa6UcsRRSM5a/giphy.gif) repeat;
  background: linear-gradient(transparent, transparent), url(https://media.giphy.com/media/l2QDSTa6UcsRRSM5a/giphy.gif) repeat;
  -webkit-background-clip: text;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="navMenu2">
  <ul>
    <li class="liHver hoverClass">
      <a href="index.html">
        <span class="jap">作業</span>
        <br>werk</a>
    </li>
  </ul>
</div>

JSFiddle

Answer №1

Controlling repeat or reset for an img like a gif using js may not be directly possible, but there is a workaround that you can use.

To start, it's recommended to transfer all the styles to js.

Next, consider adding a unique random string at the end of the gif url to indicate to the browser that this particular gif is different from the previous one.

$(".navMenu2 li").mouseover(function() {
  var n = Date.now();
  // or   var n = Math.random();
  $(this).css({
    background: "linear-gradient(transparent, transparent), url(http://gifmaker.org/download/20171105-21-n9B1fxNwBctBCeMo/GIFMaker.org_ptEtRM.gif?ver=" + n + ") repeat",
    webkitTextFillColor: 'transparent',
    webkitBackgroundClip: 'text'
  });
});

$(".navMenu2 li").mouseleave(function() {
  $(this).css({
    background: "",
    webkitTextFillColor: '',
    webkitBackgroundClip: ''
  });
});
.navMenu2 li {
  color: #0e0e0e;
  font-size: 10rem;
  font-weight: 600;
  list-style-type: none;
  transition: background-image 2s ease;
}

.navMenu2 a {
  text-decoration: none;
  color: #0e0e0e;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="navMenu2">
  <ul>
    <li class="liHver"><a href="index.html"><span class="jap">作業</span><br>werk</a></li>
  </ul>
</div>

Answer №2

To enhance the functionality of your website, consider incorporating a DOM reading command between two DOM writes. Here is an example:

$(document).ready(function(){
  $(".hoverClass").mouseleave(function(){
    $(this).removeClass("hoverClass");
    this.offsetWidth;
    $(this).addClass("hoverClass");
  }); 
});

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 a jQuery issue while trying to extend an object

Encountering errors when using some functions of jQuery 2.0.3 after extending the Object prototype... Check out this example on jsFiddle here Object.prototype.GetHashCode = function() { return 1; }; $(document).on("click", "div", function() { }); After ...

What steps can be taken to ensure an animation does not continually reset to its original state when run infinitely?

I am looking to create an animation that continues infinitely without returning to its original state after completion, similar to a sun moving animation. Below is a sample project: .animator3{ -webkit-animation-name:j3; -webkit-animation-delay:0s; -web ...

Loading screen featuring symbol percentage

https://i.sstatic.net/Ksogp.jpg How can I create a loading panel that includes a percentage symbol? I have searched for solutions but most of them suggest using a spinner. However, I specifically need a panel with the % symbol included. Is it possible to ...

Aligning the text in the middle of a button

Currently delving into Front-End development and in the process of coding my maiden site using bootstrap. Encountered a roadblock on something seemingly simple. How can I center text within a button? Here's the button, the default one utilizing an " ...

Integrating a fresh element into the carousel structure will automatically generate a new row within Angular

I'm currently working on an Angular4 application that features a carousel displaying products, their names, and prices. At the moment, there are 6 products organized into two rows of 3 each. The carousel includes buttons to navigate left or right to d ...

Issues with jQuery code functionality within web forms

After creating a jQuery script to alter the CSS of a table row, I tested it on JSFiddle and it worked perfectly. However, when implemented into my web project, it doesn't seem to be functioning as intended. See the code below: HTML: <script src ...

What is the reason that the reduce function is only effective when the final argument is explicitly stated?

I'm having trouble understanding the reduce method. What is the reason why const sumOfCubes = nums => nums.reduce((a,b) => a + Math.pow(b, 3)) does not function properly, while const sumOfCubes = nums => nums.reduce((a,b) => a + Math.po ...

JavaScript Number Conversion with parseInt()

Struggling to execute a calculation that fills a text box based on an onblur event. Unfortunately, I keep getting NaN as the result. Since I am not very familiar with JavaScript, any guidance will be greatly appreciated. Please note that I am mainly focus ...

Adding files dynamically as objects using Leaflet Javascript

I've recently started learning javascript and leaflet, and I'm in need of some advice. My objective is: To send a picture from my phone to my server along with GPS exif data. To store this information in a file: JSON or database? To create a m ...

The use of "runat="server" in the HTML Head tag is preventing C# embedded code blocks from being converted to client-side code

After I included runat="server" in the head tag and added a CSS href with the value of <%= WebAppInstance %>, I noticed that the <%= WebAppInstance %> was not being converted to client-side code. To provide clarity on my question, please refer ...

Exploring the power of "this" in Vue.js 2.0

Seeking help with a VueJS issue. I am trying to create a voice search button that records my voice and prints it out in an input form. <input type="text" name="inputSearch" id="inputSearch" v-model="inputSearch" class="form-control" x-webkit-speech> ...

What is the process for extracting the period value from SMA technical indicators within highcharts?

Can someone assist me in retrieving the period value from SMA indicator series by clicking on the series? series : [{ name: 'AAPL Stock Price', type : 'line', id: 'primary', ...

The alignment of Div elements is off in IE8

I have been struggling to align two divs side by side without any empty spaces on IE 8. Everything looks perfect on Chrome Version 35.0.1916.153 m and Firefox 30.0, but IE 8 seems to be causing some issues. All I want is a main div with two child divs ali ...

What steps are needed to create a transport directly from the Console?

In my current setup, this is the code I have: const logger = new winston.Logger(); logger.add(winston.transports.Console, { level: environment === 'development' ? 'silly' : 'info', colorize: true, prettyPrint: true }); ...

Using jQuery to retrieve information from another webpage and populate form fields with the data

Looking for assistance on retrieving values from test2.php and populating form fields on test1.php. Any guidance is appreciated. test 2 page $empidx="Design-1012-1000"; $countries = mysqli_query($con,"select country from countries"); $query = mysqli_quer ...

Issue with the AngularJS build in Yeoman

After setting up Yeoman and Bootstrap for an AngularJS project, I utilized the grunt server command for debugging and coding. However, when I tried using the 'grunt build' command, it generated a dist folder. Unfortunately, when I attempted to ru ...

Working through JSON arrays in JavaScript

Here is a JSON example: var user = {"id": "1", "name": "Emily"} If I select "Emily," how can I retrieve the value "1"? I attempted the following code snippet: for (key in user) { if (user.hasOwnProperty(key)) { console.log(key + " = " + use ...

Is there a more secure alternative to using the risky eval() function? Do I need to take the lengthier route by implementing a switch case instead?

I've been practicing and honing my Javascript skills by working on a calculator code that initially had lots of repetitive lines. I managed to simplify it, but I am aware that using the eval() method is not generally recommended. let current = 0; f ...

Tips for querying date field in PHP using MySQLi

I am encountering an issue when searching the date field in my MySQL database. I have an HTML form that allows users to search the database in three different ways: by student_id, last name, or date. When I run the program and choose student ID or last nam ...

Is the click count feature malfunctioning?

My goal is to track every mouse click made by the user, so I created a function for this purpose. However, it seems that the function is only counting the first click. To store the count values, I have created a variable. <!DOCTYPE html PUBLIC "-//W3 ...