移动' p'进入' div' div点击时。 - 动画不起作用

时间:2017-10-01 21:31:26

标签: jquery jquery-animate

这只是我真实代码的演示,但问题是一样的。有没有其他或更好的解决方案。为什么animate()方法不起作用....



$('document').ready(function() {
  $('p').click(function() {
    var x = $('div').offset();
    $(this).animate({
      top: $(this).offset({
        top: x.top,
        left: x.left
      })
    }, 1000);
  });
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p style='position:relative;'>Move This</p>

<div style='height:100px;width:100px;border:1px solid black;margin-top:200px;'></div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

//this is my code:
 import java.util.Scanner;

public class Matrices

{

 public static double[][] multiplyMatrix(double[][] a,double[][] b)

 {

      double c[][]=new double[3][3];

      for(int i=0;i<3;i++)

          for(int j=0;j<3;j++)

               for(int k=0;k<3;k++)

                    c[i][j]=c[i][j]+a[i][k]*b[k][j];

      return c;

  }


  public static void main(String args[])

 {

      //Create Scanner object to read input from user

      Scanner sc=new Scanner(System.in);

      double a[][]=new double[3][3];

      double b[][]=new double[3][3];

      double sum[][]=new double[3][3];

      double mul[][]=new double[3][3];



      //Read the elements of matrix b

      System.out.println("Enter the elements of matrix a:");

      for(int i=0;i<3;i++)

          for(int j=0;j<3;j++)

               a[i][j]=sc.nextInt();


     System.out.print(a[i][j]+"")
      //Read the elements of Matrix b

      System.out.println("Enter the elements of matrix b:");

      for(int i=0;i<3;i++)

          for(int j=0;j<3;j++)

               b[i][j]=sc.nextInt();


      //Call the method multiplyMatrix to multiply a and b

      mul=multiplyMatrix(a,b);


      System.out.println("Multiplication of two matrices:");




      for(int i=0;i<3;i++)

      {

          for(int j=0;j<3;j++)


          {

             System.out.print(mul[i][j]+"");                   

          }

          System.out.println();

          }   

      }

   }
$('document').ready(function() {
  $('p').click(function() {
    var x = $('div').offset();
    $(this).animate({
      top: x.top,
      left: x.left
    }, 1000);
  });
});

答案 1 :(得分:1)

top: $(this).offset({ top: x.top, left: x.let })是一个不正确的构造,也拼写错误。

这是包括克隆在内的修正。注意我必须使div位置绝对,并在动画后将p从相对值更改为静态以将p插入div

$('document').ready(function() {
  $('p').click(function() {
    var $p = $(this),
      $div = $('div'),
      $pc = $p.clone(),
      x = $div.offset();
    $pc.insertAfter($p).animate({
      top: x.top,
      left: x.left
    }, 1000,function() {
      $div.append($pc.css({"position":"static"}))
    });
  });
});
.target {position:absolute; top:100px;height:100px;width:100px;border:1px solid black;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<p style='position:relative;'>Move This</p>

<div class="target"></div>