0

I have a dial that moves automatically, and I would like to figure out a way to have it move only when the mouse hovers over the div. I have read that it may not be possible to do this in javascript, but I am hoping some one here might have a solution.

Here is what I have so far. https://jsfiddle.net/r0w71wkv/

#logo {
    display: inline-block;
    position: relative;
}
#logo .speedometer {
    width: 80px;
    height: 80px;

}
#logo .needle {
    width: 5px;
    height: 80px;
    background: #324A90;
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    border-top-left-radius: 100%;
    border-top-right-radius: 100%;
    display: inline-block;
    left: 110px;
    position: absolute;
    top: 10px;
    -webkit-animation:move 5s infinite;
    transform:rotate(0deg);
    transform-origin:bottom;
}
@-webkit-keyframes move {
    0% {
        transform:rotate(-180deg);
    }
    50% {
        transform:rotate(90deg);
        }
    100% {
        transform:rotate(-180deg);
    }
}

1 Answer 1

2

:hover Psudeoclass

Only active when the element preceding it is being hovered.

Using :hover:

#logo .needle:hover {
    -webkit-animation:move 5s infinite;
    animation:move 5s infinite;
}

Then get rid of the -webkit-animation that you have currently. This will play the move animation only when you hover over the .needle element in #logo.

Also, you don't need to prefix animation much anymore: http://caniuse.com/#search=css%20animation

Here's a fiddle: https://jsfiddle.net/r0w71wkv/5/.

I might note that you could be better off first setting the width and the height of the #mainwrap div, and then using #mainwrap:hover .needle, because that way the user doesn't have to hover directly over the needle. This would like: https://jsfiddle.net/r0w71wkv/6/.

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried using .needle:hover, but I just lose the animation all together. Same when I lose the webkit.
@user2168066 Try using jsfiddle.net/r0w71wkv/7. I think the problem is that you have to hover exactly over the needle with the other approach, when this in fact defines an 80px by 80px area where you can hover to play the animation.
Ok I see now, the problem was having to hover directly over the specific div, thanks for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.