gpt4 book ai didi

jquery - 从边缘 :auto . 动画元素

转载 作者:太空宇宙 更新时间:2023-11-04 13:07:30 25 4
gpt4 key购买 nike

我有一个简单的 div 元素,我使用 CSS 动画来创建一个流畅的布局:

div{
-webkit-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}

尽管使用像素特定比率为元素设置动画非常简单 http://jsfiddle.net/yBJ42/1/当元素以 margin:0 auto 居中时失败有什么办法可以解决这种行为吗?不幸的是,我不能使用绝对定位元素,因为我的布局中的主容器使用 CSS calc 来计算它的宽度,我想保留 margin-right:0 作为边栏显示。

此外,使用 JavaScript 计算和调整容器宽度和边距并不是我处理这种情况的首选方式。

提前致谢。

最佳答案

这是您想要的 jQuery 版本: DEMO

var centerMargin=($(window).width()/2)-($('div').width()/2)+'px';
$('div').css('margin-left',centerMargin);
var toggled=false;
$(".animate").click(function(){
if(!toggled){
$('div').animate({marginLeft:'0px'},1000);
toggled=true;
}
else{
$('div').animate({marginLeft:centerMargin},1000);
toggled=false;
}
});

关于jquery - 从边缘 :auto . 动画元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24996656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com