jQuery无法在“元素”上执行“动画”:不支持部分关键帧

时间:2018-11-25 07:11:44

标签: jquery jquery-animate

我有以下代码。我正在尝试为png设置动画,以便它看起来可以拉伸。但是,当代码运行时,我收到错误消息: 无法在“元素”上执行“动画”:不支持部分关键帧。

jQuery在文档上被初始化,因为它在其他函数中也被使用。

任何帮助将不胜感激

$("#light-switch")[0].animate(
    {height: "30%"} ,500, function(){
      console.log("moved")
 });
#light-switch{
   z-index: 5;
   width: 10%;
   height: 25%;
   position: absolute;
   top:63%;
   right:20%;
 }
<div class="clickable" id="light-switch-link" >
      <img id="light-switch" src="./images/switch-on.png" alt="light_switch"  />
</div>
<!-- Scripts for bootstraps -->
      <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
      <script src="./js/main.js" type="text/javascript"></script>

1 个答案:

答案 0 :(得分:1)

您正在$(“#light-switch”)[0]的索引中搜索,这是错误的。您不需要定义它。这将直接起作用$(“#light-switch”)。animate(。

这将为您工作。

$("#light-switch").animate(
    {height: "30%"} ,500, function(){
      console.log("moved")
 });
#light-switch{
   z-index: 5;
   width: 10%;
   height: 25%;
   position: absolute;
   top:63%;
   right:20%;
 }
<div class="clickable" id="light-switch-link" >
      <img id="light-switch" src="./images/switch-on.png" alt="light_switch"  />
</div>
<!-- Scripts for bootstraps -->
      <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
      <script src="./js/main.js" type="text/javascript"></script>