在Javascript中增加转换速度

时间:2019-02-15 12:30:31

标签: javascript jquery transform transition

我想在用Javascript进行的转换中增加一些速度,但是我不知道怎么做。

我的Javascript代码是

$('#cell1').mouseenter(function(){
    $(this).attr('transform', 'scale(1 1) translate(200, 10)');

我的此类在我的html代码中。 我应该如何增加速度?

我尝试过

$(this).attr('transition-duration', '3s');

但效果不佳。

2 个答案:

答案 0 :(得分:2)

您的代码中存在3个问题:

  1. 使用$.css()代替$.attr()
  2. 用逗号分隔CSS scale
  3. 添加翻译单位

这将起作用:

$(this).css('transform', 'scale(1, 1) translate(200px, 10px)');
$(this).css('transition-duration', '3s');

https://jsfiddle.net/781wuxym/

答案 1 :(得分:1)

使用.css()代替translate

此外,您需要确保正确使用了CSS函数。例如,您需要确保px使用scale作为坐标,并且$('#cell1').mouseenter(function() { $(this).css({ "transition-duration": "3s", "transform": "scale(1) translate(200px, 10px)" }); });以逗号分隔(如果h&w相同,则只能指定1个单位)。

请参见下面的工作示例:

#cell1 {
  height: 150px;
  width: 150px;
  border: 1px solid red;
  transform: scale(0.5);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="cell1"></div>
request.AddParameter("application/json", "{\n\t\"lastName\":\"gaurav.sablok@agarwalpackers.com\"\n}", ParameterType.RequestBody);