[英]Applying css property in jquery function
I have some errors with my code.我的代码有一些错误。 Maybe someone could help me.
也许有人可以帮助我。 I am getting this error:
我收到此错误:
SyntaxError: missing formal parameter: $(this).css("position" , "relative");
语法错误:缺少形式参数:$(this).css("position" , "relative");
$(document).keydown(function(e) { var count = 0; if (e.keyCode == 40) { count++; } if (count == 1) { $('#box').animate({ borderSpacing: -90 }, { step: function(now, fx) { $(this).css('-webkit-transform', 'rotate(' + now + ' deg)'); $(this).css('-moz-transform', 'rotate(' + now + ' deg)'); $(this).css('transform', 'rotate(' + now + ' deg)'); }, duration: 'slow' }, 'linear'); } else { $('#box').animate({ height: '10%' }, { $(this).css("position", "relative"); $(this).css("margin-top", "100px"); $(this).css("width", "30%"); $(this).css("background-color", "tomato"); $(this).css("margin-left", "20%"); $(this).css("align-content", "center"); }); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="box"> <div id="box2"></div> </div>
You are using the API the wrong way, use a function for the second argument of animate and it will work.您以错误的方式使用 API,将函数用于animate的第二个参数,它会起作用。
Try this instead:试试这个:
$('#box').animate({
height: '10%'
}, function() {
$(this).css("position", "relative");
$(this).css("margin-top", "100px");
$(this).css("width", "30%");
$(this).css("background-color", "tomato");
$(this).css("margin-left", "20%");
$(this).css("align-content", "center");
});
the last argument of jQuery.fn.animate should be a function. jQuery.fn.animate 的最后一个参数应该是一个函数。 if you require one.
如果你需要一个。
$(selector).animate({...styles...}, time, easing, function(){ /* callback */})
Also as an aside you can set all your css properties at the same time using an object.另外,您可以使用对象同时设置所有 css 属性。
$(this).css({ position: 'relative', marginTop: '100px', width: '30%', backgroundColor: 'tomato', marginLeft: '20%', alignContent: 'center' })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.