javascript - 鼠标离开后CSS继续动画

标签 javascript css

有人可以帮我做 css 动画吗? Mb 我也应该使用 js 吗? 我想创建左->右悬停动画,但在鼠标离开后,我想继续左->右而不是右->左动画。

谢谢

.button_sliding_bg {
    color: #31302B;
    background: #FFF;
    padding: 12px 17px;
    margin: 25px;
    font-family: 'OpenSansBold', sans-serif;
    border: 3px solid #31302B;
    font-size: 14px;
    font-weight: bold;
    letter-spacing: 1px;
    text-transform: uppercase;
    border-radius: 2px;
    display: inline-block;
    text-align: center;
    cursor: pointer;
    box-shadow: inset 0 0 0 0 #31302B;
	-webkit-transition: all ease 0.8s;
	-moz-transition: all ease 0.8s;
	transition: all ease 0.8s;
}
.button_sliding_bg:hover {
    box-shadow: inset 100px 0 0 0 #31302B;
    color: #FFF;
}
<button class="button_sliding_bg">
Button
</button>

我想要这样的东西:

enter image description here

解决方法如下:

window.setTimeout(function() {
document.getElementById('btn').style.visibility = 'visible';
}, 400);
.button_sliding_bg {
    visibility:hidden;
    padding: 12px 17px;
    margin: 25px;
    font-family: 'OpenSansBold', sans-serif;
    border: 3px solid #31302B;
    font-size: 14px;
    font-weight: bold;
    letter-spacing: 1px;
    text-transform: uppercase;
    border-radius: 2px;
    display: inline-block;
    text-align: center;
    cursor: pointer;
    animation: animate-out 0.5s 1;
	  animation-fill-mode:forwards;
}
.button_sliding_bg:hover {
    animation: animate-in 0.5s 1;
	animation-fill-mode:forwards;
}

@keyframes animate-in {
	0% {
		box-shadow: inset 0 0 0 0 #31302B;
		color:#31302B;
	    background: #FFF;
	}
	100% {
		box-shadow: inset 100px 0 0 0 #31302B;
		color:#FFF;
	    background: #FFF;
	}
}
@keyframes animate-out {
	0% {
		box-shadow: inset 0 0 0 0 #FFF;	
	    background: #31302B;
		color:#FFF;
	}
	100% {
		box-shadow: inset 100px 0 0 0 #FFF;	
	    background: #31302B;
		color:#31302B;
	}
}
<button id="btn" class="button_sliding_bg">
Button
</button>

最佳答案

如果您不介意它在页面加载时首先运行,您可以使用 CSS3 的 animation 属性来实现您想要的效果。诀窍是在悬停时更改 animation-name 和使用 animation-fill-mode 的组合保留所做的更改。请记住,CSS3 的动画被认为是“实验性的”,但是,它有 almost complete coverage在最新的浏览器版本上。

JSFIDDLE

CSS

.button_sliding_bg {
    padding: 12px 17px;
    margin: 25px;
    font-family: 'OpenSansBold', sans-serif;
    border: 3px solid #31302B;
    font-size: 14px;
    font-weight: bold;
    letter-spacing: 1px;
    text-transform: uppercase;
    border-radius: 2px;
    display: inline-block;
    text-align: center;
    cursor: pointer;
    animation-name: animate-out;
    animation-duration: 0.5s;
    animation-repeat-count: 1;
    animation-fill-mode: forwards;
}
.button_sliding_bg:hover {
    animation-name: animate-in;
}

@keyframes animate-in {
    0% {
        box-shadow: inset 0 0 0 0 #31302B;
        color:#31302B;
        background: #FFF;
    }
    100% {
        box-shadow: inset 100px 0 0 0 #31302B;
        color:#FFF;
        background: #FFF;
    }
}
@keyframes animate-out {
    0% {
        box-shadow: inset 0 0 0 0 #FFF; 
        background: #31302B;
        color:#FFF;
    }
    100% {
        box-shadow: inset 100px 0 0 0 #FFF; 
        background: #31302B;
        color:#31302B;
    }
}

关于javascript - 鼠标离开后CSS继续动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40826683/

相关文章:

javascript - 一次发送一个 $.post() ("adding a pause"但实际上是 : toggling a 'transmit' flag)

html - 为什么我的列表项在列中中断?

html - 在 Internet Explorer 中使用 css 给第二列或第四列右侧边框

javascript - 为什么当我点击打印按钮时@media print 的选择器没有显示正确的格式?

php - 为什么 Javascript sha1 和 PHP5 sha1 为 utf-8 字符串生成不同的结果?

javascript - TypeError : this. props.AddNewHero 不是一个函数

javascript - 在 Webform 的母版页中将 $(document).ready 放在哪里

html - 在输入元素上合并类样式和内联样式?

css - Azure B2C 样式 - 引用处理您的请求覆盖的访问 ID 或类

javascript - 将 JSON 文件加载到 JavaScript 变量中