【发布时间】:2011-09-12 22:03:48
【问题描述】:
我想通过首先在 mouseenter 上使其边框变厚 5px,然后在 mouseleave 上将边框减小 5px 来为 div 设置动画,棘手的部分是我不希望 div 看起来像在移动(如果你只是为边框设置动画,整个 div 看起来会发生变化,而不仅仅是边框变厚/变薄)。我非常接近,但我被困在最后一部分:mouseleave。到目前为止我所拥有的是:
$("#thumbdiv<%=s.id.to_s%>").bind({
mouseenter: function(){
$(this).animate({
borderRightWidth: "25px",
borderTopWidth: "25px",
borderLeftWidth: "25px",
borderBottomWidth: "25px",
margin: "-5px"
}, 500);
},
mouseleave: function(){
$(this).animate({
borderRightWidth: "20px",
borderTopWidth: "20px",
borderLeftWidth: "20px",
borderBottomWidth: "20px",
margin: "0px"
}, 500);
}
});
我在这之前的某处设置了边框为20px,而margin没有设置,所以是0px。 div 在 mouseenter 上的动画效果很好,我可以使边框更厚而 div 不会实际移出位置,但是当触发 mouseleave 时,div 将首先将自身重新定位到该位置,就好像从未调用过“margin -5px”一样,然后慢慢减小它的边界,似乎实际上并没有调用“magin:'0px'”。
我不确定我的描述是否有道理,如果需要,我可以制作一个原型。
【问题讨论】:
-
我根本无法使用它;它只是疯了:-(
-
我找到了答案,我们还不能在 JQuery 中为速记值设置动画,所以边距必须是 marginTop、marginRight、marginBottom 和 marginLeft。会尽可能发布我自己的答案。
-
是的 :-) jsfiddle.net/25EsV
-
我发布的小提琴在类定义中使用速记。
标签: jquery jquery-animate mouseenter