Delay Hover effect on background-size in CSS

1.5k Views Asked by At

I wonder if there is a way to delay the hover effect on background-size that goes from "cover" to "contain"? I have seen transition-delay works on backround-color and other properties but not backround-size. Any help will be appreciated. Thanks!

div{
    display:inline-block;
    width: 100px;
    height: 100px;
    padding:5px;
    margin:10px;
    border:1px solid #ccc;
    background-image: url("https://i.stack.imgur.com/2OrtT.jpg");
    background-size: cover;
    background-repeat: no-repeat;
    background-position: 50% 50%;
    transition: 0s background-size;
    transition-delay:1s;
}
div:hover{
    background-size: contain;
    background-color:red;
}

http://jsfiddle.net/rtku7sqk/

UPDATE Perhaps I should clarify that I want to delay the hover effect to happen but not extend the duration of the animation.

Similar to this... http://dabblet.com/gist/1498443


LATEST UPDATE

I figure out a way to fix this by using a mix of both CSS and javascript.

This way "background-size" works with "contain" and "cover"

$("#tmp").hover(function() {

    /* Mouse enter */
    var thisone = $(this);
    window.mytimeout = setTimeout(function() {
        thisone.addClass("mouseon");
    }, 1000);

}, function() {

    /* Mouse leave */
    clearTimeout(window.mytimeout);
    $(this).removeClass("mouseon");
});

http://jsfiddle.net/rtku7sqk/5/

3

There are 3 best solutions below

0
On BEST ANSWER

So, there's only one small little thing with your CSS, otherwise it'll work exactly as you expect.

You can't transition from non-numerical values

The values contain and cover for background-size aren't transition-able. Since they're non-numerical, you can't operate a CSS calculation on them.

If you change that to a numerical value like in the example below, it'll happen exactly how you expect it to:

Fiddle illustrating this example

Here are the changes to the CSS:

div{
    display:inline-block;
    width: 100px;
    height: 100px;
    padding:5px;
    margin:10px;
    border:1px solid #ccc;
    background-image: url("https://i.stack.imgur.com/2OrtT.jpg");
    background-size: 100%; /* NOT `cover` anymore */
    background-repeat: no-repeat;
    background-position: 50% 50%;
    transition: 0s background-size;
    transition-delay: 1s;
}
div:hover{
    background-size: 20%; /* NOT `contain` anymore */
    background-color:red;
}
0
On

You could do something like this:


DEMO


If you want to work transition should use background-position with px something like:

div{
   background-position: 0px 0px;
}
div:hover{
   background-position: center center;
}
3
On

How about trying jQuery animate function? Without putting an extremely amount of effort, I think this is a good start for your problem. And of course, you can modify as you wish.

note: I understand you requested CSS but in this case, JQuery would be your best bet. However, if you are only requesting CSS, disregard this response. But I posted it because future visitors may be able to use this method and not have your restrictions. Cheers!

$( "div ").hover(function() {
    $( "img" ).animate({
      width: "100%",
    }, 1500 );
});

DEMO