首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动画化div onHover

动画化div onHover
EN

Stack Overflow用户
提问于 2019-04-12 08:52:20
回答 2查看 125关注 0票数 0

当光标在div上时,我想旋转一个div,就像在这段视频中一样。

我想使用关键帧,因为它们更可定制。

代码语言:javascript
复制
div.myElement {
    ...
    animation: mainMenu 2s infinite;
    animation-play-state: initial;
}

div.myElement:hover {
    animation-play-state: running;
}

@keyframes mainMenu {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(45deg);
    }
}

它不工作,但我不知道我需要把什么放在我的div.myElement和div.myElement:hover。在div.Element中,div.Element:hover为0% ~ 100%,100% ~ 0%。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-12 08:58:46

我有一个框的动画,就像你编写示例https://jsfiddle.net/gnox69pv/5/一样

代码语言:javascript
复制
<div class="myElement"></div>
<div class="myElement"></div>
<div class="myElement"></div>

CSS

代码语言:javascript
复制
    div.myElement {

    background-color: red;
    height: 100px;
    width: 100px;
    margin:10px;
    animation: change_width_back 0.7s forwards;
}
div.myElement:hover {
    animation: change_width 0.7s forwards;
}
@keyframes change_width {
     0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(45deg);
    }
}
@keyframes change_width_back {
    0% {
        transform: rotate(45deg);
    }
    100% {
        transform: rotate(0deg);
    }
}
票数 1
EN

Stack Overflow用户

发布于 2019-04-12 09:08:21

试试这个:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<style> 
div.myElement {
    width : 100px;
    height : 100px;
    background-color : red;
    transform: rotate(0deg);
    animation-play-state: initial;
}

div.myElement:hover {
	animation: mainMenu 2s infinite;
    animation-play-state: running;
}

@keyframes mainMenu {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(45deg);
    }
}
</style>
</head>
<body>

<div class="myElement"></div>

</body>
</html>

如果你想让盒子停留在同一阶段,请使用以下命令:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<style> 
div.myElement {
    width : 100px;
    height : 100px;
    background-color : red;
    animation: mainMenu 2s infinite;
    animation-play-state: paused;
}

div.myElement:hover {
    animation-play-state: running;
}

@keyframes mainMenu {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(45deg);
    }
}
</style>
</head>
<body>

<div class="myElement"></div>

</body>
</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55647869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档