【发布时间】:2023-03-10 20:45:01
【问题描述】:
这里有很多非常相似的问题,但我找不到我的问题的确切答案。
我是使用 jQuery .animate() 方法的新手,我想知道是否有反转动画的常用或最佳方法?
这是我想出的一个例子: Fiddle
基本上它所做的只是使用 if 语句来检查一个 CSS 属性,然后将其设置为一种状态或返回另一种状态。
$(document).ready(function () {
$("[name=toggle]").on("click", function () {
if ($('.box').width() < 125) {
$(".box").animate({
opacity: .3,
width: "125px",
height: "125px"
}, 250);
} else {
$(".box").animate({
opacity: 1,
width: "100px",
height: "100px"
}, 250)
}
});
});
.box {
background-color: lightblue;
box-shadow: 5px 5px 10px #000000;
width: 100px;
height: 100px;
display: block;
margin: 20px auto 0 auto;
border-radius: 25px;
}
[name="toggle"] {
display: block;
margin: 0 auto;
margin-top: 15px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button name="toggle">Animate</button>
<div class="box"></div>
这是我第一次接触 .animate() 时能够想到的,但我认为可能有更好的方法。 假设您想在悬停时制作动画,并在鼠标移离元素时反转动画。有没有办法轻松地轻松反转 jQuery 动画?我不在,我可以在 CSS 中使用 :hover 状态,但这是关于 jQuery .animate() 的假设问题。
【问题讨论】:
-
据我所知,
animate()没有reverse函数,但我更多地使用 VelocityJS(它支持reverse),值得一试跨度> -
看起来很酷,我要开始玩了
-
$(".yourelem").removeAttr("style"); -
可能没有更好的方法。普通的
.css()允许您传递一个空字符串 ('') 以从元素中删除规则(因此恢复到样式表),但这似乎不适用于.animate()
标签: javascript jquery html css animation