implementing a delay after hovering over a CSS hover effect before activating it

I'm trying to achieve a specific effect using JavaScript or jQuery, but I'm struggling to figure it out. I have created a simple CSS box with a hover effect that changes the color. What I want is for the hover effect to persist for a set amount of time after the mouse leaves the box, regardless of any further movement. If the user accidentally hovers over the box while the effect is still holding, I want it to continue until the set time has passed.

Here is the HTML and CSS code I'm working with:

#box {
    width: 200px;
    height: 300px;
    background-color: #00CCFF; 
}

#box:hover {
    width: 200px;
    height: 300px;
    background-color: #669933;
}

<!DOCTYPE html>
<html>
<head>
</head>
<body>    
    <div id="box"></div>

</body>
</html>

Answer №1

CSS

.container-normal {
        width: 200px;
        height:300px;
        background-color: #00CCFF; 
}

.container-hover {
        width: 200px;
        height:300px;
        background-color: #669933;
}

HTML

<!DOCTYPE html>
<html>
<head>
</head>
<body>   

   <div id="content" class="container-normal"> Content here </div>

</body>

JQuery

$(function() {
var delayTime = 2000;

$("#content").mouseenter(function(){
    if ($("#content").hasClass('container-normal'))
    {
         $("#content").removeClass('container-normal').addClass('container-hover');
         window.setTimeout(function() {
             $("#content").removeClass('container-hover').addClass('container-normal'); 
         }, delayTime); 
    }     
}); 

}); 

You can adjust the "delayTime" variable. ( 2000 equals to 2 seconds)

See a live example here: http://jsfiddle.net/jqT7d/1/

For a simpler version, check out this suggestion by jfriend00: http://jsfiddle.net/jfriend00/jqT7d/3/

Answer №2

ul.navigator-wrap-inner li.thumbnail_resize:hover {
  border-color:#184ACD;
  -webkit-transition: border-color 0.6s ease-in-out 0s;
}

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

What might be causing my enzyme test for React to fail when calling document.getElementById()?

I am currently facing an issue while trying to render a Snapshot test for a nested React component. The problem lies with the test code failing to handle a document.getElementById() request within the component's relevant code section: componentDid ...

Attempting to automate the process of clicking a button on a webpage using cefsharp

Hello, I am currently working with cefsharp and vb.net to develop a code that will help me navigate to the next page of a specific website. The website link is: https://www.recommendedagencies.com/search#{} My goal is to extract the list of company names ...

I have been having trouble getting the entire background to display in a specific color with Styled Components

It seems like a simple issue, but I just can't get the background to cover the entire screen. Even though I have provided the code I used, only a quarter of the top of the screen appears black when wrapping something in the component. Here is the cod ...

Alter the navigation but keep the URL intact without modifying the view

I have an Angular project that includes a login component. The login component is located in the directory app/main/login. I am trying to navigate to the login component from app.component.html using a button. Below is the code snippet from my app-routi ...

Iterate through each row asynchronously, waiting for each one to complete before moving on to the

Trying to navigate through multiple pages using puppeteer has been successful, except when attempting to parse through them at the same time. The issue seems to stem from the code executing async operations in rapid succession, overwhelming the browser. My ...

The day text function failed to return a value and instead gave back

I am having trouble figuring out where the mistake is in my function below. My goal is to retrieve the day value in string format. function getDayText(date){ var weekday = new Array(7); weekday[0]= "Sunday"; weekday[1] = "Monda ...

Implementing a progress bar in Rails 4 to delete a remote file using Fog and waiting for controller respond_to

I've been searching for solutions to implement a progress bar in my Rails 4 application. I have a scenario where users can delete images associated with a record, and the images div is updated via jQuery after deletion. However, since the deletion pro ...

The error message "Equals is not defined: Selenium IDE Custom Format" indicates that the function "

I've been working on enhancing the functionality of the selenium IDE through custom functions. I successfully added my custom functions to user-extensions.js and they work as expected within the IDE. However, I encountered issues when trying to export ...

The file you are trying to import, bootstrap-sass, cannot be located or is unreadable

Experimenting with Django Shop and following a tutorial that can be found here: (stable version). After starting the server and navigating to localhost, the following error message is displayed: Error: File to import not found or unreadable: bootstrap-s ...

What is the reason behind the length property belonging to an array object?

While there are other instances, let's focus on the length property for now. Why does it appear as if we're copying it here: [].hasOwnProperty("length") //==> true It is common knowledge that an array's length property belongs ...

support for fullscreenchange event across different browsers

I have been exploring how to add an event listener for fullscreen change in my Next.js app, and I noticed that many example codes use the webkit, moz, and ms prefixes together. However, after testing on Edge, Chrome, and Firefox, it seems that using only t ...

Setting up Quasar plugins without using Quasar CLI: A step-by-step guide

I integrated Quasar into my existing Vue CLI project by running vue add quasar. Now I'm attempting to utilize the Loading plugin, but unfortunately, it's not functioning as expected. Here is the configuration setup for Quasar/Vue that I have in ...

Issue with integrating Contact Form 7 HTML into Wordpress

I've revisited this question with a new "input" HTML tag Despite changing everything, CSS still isn't being applied. HTML Code : <form action="#" class="contact-one__form row"> <div class="col-lg-6"> ...

What are some clever ways to handle the transition of the display property?

While transitions for the display property may not work, I am exploring alternatives. I attempted using the visibility property but it didn't quite fit my needs. In my current setup, different text is displayed when hovering over an anchor tag by sett ...

Arrange the array depending on the existence of properties in the objects

Is there a way to efficiently organize an array like the following in cases where certain fields are missing? For instance, consider the current array: const users = [ { id: 1, firstname: 'Jerry' }, { id: 2, firstname: & ...

Using fancybox to send an ajax post request to an iframe

Can you send a variable to the iframe when using fancybox? This is my current setup: function fancyBoxLoad(element) { var elementToEdit = $("#" + $(element).attr("class")); var content = encodeURIComponent($(elementToEdit).oute ...

Tips for creating a two-tier selection filter system

I'm having an issue with this code. My objective is to create two filters for this table. The select element with id="myInput" should determine which rows appear in the table and apply the first filter. Here is the JavaScript code: function myFunctio ...

Is there a way to deactivate a button upon clicking and then substitute it with a new button that serves a different purpose in AngularJS?

Is there a way to deactivate a button once clicked and substitute it with another button that performs a different task using AngularJS? Here is the button in question: <button type="submit" class="btn btn-small btn-default" ng-disabled="isteam==0 || ...

Codeigniter's dynamic auto-complete feature using Ajax

Implementing AJAX Autosearch <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> <script type="text/javascript"> function ajaxSearch() { alert('hello'); var input_data = $(&ap ...

What exactly does "blocking and tackling" refer to in the Angular2 documentation?

As I delved into the online documentation for angular2, I stumbled upon a puzzling term - "blocking and tackling" in the ADVANCED - Angular Module chapter (https://angular.io/docs/ts/latest/guide/ngmodule.html). ... "It's all just basic blocking and t ...