[英]Applying css property in jquery function
我的代码有一些错误。 也许有人可以帮助我。 我收到此错误:
语法错误:缺少形式参数:$(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>
您以错误的方式使用 API,将函数用于animate的第二个参数,它会起作用。
试试这个:
$('#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");
});
jQuery.fn.animate 的最后一个参数应该是一个函数。 如果你需要一个。
$(selector).animate({...styles...}, time, easing, function(){ /* callback */})
另外,您可以使用对象同时设置所有 css 属性。
$(this).css({ position: 'relative', marginTop: '100px', width: '30%', backgroundColor: 'tomato', marginLeft: '20%', alignContent: 'center' })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.