我该怎么做?
$('#element').animate({ "width": "calc(100% - 278px)" }, 800);
$('#element').animate({ "width": "calc(100% - 78px)" }, 800);
如果它只是%
或仅px
,而不是calc()
,我可以这样做,我可以使用jQuery的其他选项吗?还是其他一些JavaScript技巧?
当用户点击某个元素时,必须进行更改,所以:
$("#otherElement").on("click", FunctionToToggle);
当用户点击$("#otherElement")
时,必须发生切换效果。
答案 0 :(得分:16)
也许这会有所帮助:
$('#element').animate({ "width": "-=278px" }, 800);
每次此脚本将从元素中删除278px
编辑: 试试这个,它会在调整窗口大小时重新计算。如果我理解你应该有所帮助。
$(window).on("resize",function(){
$('#element').css("width",$('#element').width()-275+"px");
});
CSS3选项
由于CSS3具有动画功能,您也可以使用它:
#element{
-webkit-transition:all 500ms ease-out 0.5s;
-moz-transition:all 500ms ease-out 0.5s;
-o-transition:all 500ms ease-out 0.5s;
transition:all 500ms ease-out 0.5s;
}
如果要为元素设置动画。 你可以这样做:
$('#element').css("width","calc(100% - 100px)");
在这种情况下,CSS将执行动画。
请注意,这不适用于旧浏览器
答案 1 :(得分:2)
我不确定使用calc
是否可行。我的答案检查父级的宽度,然后对其执行操作,然后为元素设置动画。
elWidth = $('#element').parent().width(); // Get the parent size instead of using 100%
elWidth -= 20; // Perform any modifications you need here
$('#element').animate({width: elWidth}, 500); //Animate the element
答案 2 :(得分:1)
我在@ jfriend00的帮助下找到了另一种形式。
首先,我创建了CSS规则,但没有过渡。
在切换的功能中:
$('#element').animate({ width: "-=200px" }, 800, function () { $('#element').addClass('acoreonOpened');$('#element').removeAttr("style") });
.acoreonOpened是我使用calc(100% - 278px)的CSS规则的地方。
所以,首先我使用jquery制作动画,当它结束时,我删除了jquery使用的样式(如果没有,css将无法工作),并且在我放入类之后,所以它的行为类似于宽度%。