简体   繁体   English

jquery图片动画

[英]Jquery image animation

I'm newbie of Javascript and Jquery.我是 Javascript 和 Jquery 的新手。

I am learning img animation and I have a question.我正在学习 img 动画,我有一个问题。

If I want to move the image from bottom left to the top right in window.如果我想在窗口中将图像从左下角移动到右上角。 Is there any better way than my code?有没有比我的代码更好的方法?

My code doesn't' work then I expected.我的代码没有像我预期的那样工作。

here is my code这是我的代码

$(document).ready(function () {


  function startMoving() {
    var img = $("#imageId");
    var imgWidth = img.width();
    var imgHeight = img.height();
    var screenWidth = $(window).innerWeight();
    var screenHeight = $(window).innerHeight();
    var c = Math.sqrt((screenWidth*screenWidth+screenHeight*screenWidth));
    var movement = c/10 // This is for the step of movement
    var zScale = (screenWidth+screenHeight)/2;
    var imgZScale = (imgHeight+imgWidth)/2;
    console.log(zScale);
    console.log(imgHeight);
    
    img.animate({
      "left": "+="+movement,
      "top": "-="+movement
      
    },"slow");
   }
  setInterval(function(){
    startMoving();
  },1000)
});

If the image at the corner how can I restart image movement again from bottom left?如果图像在角落,如何从左下角重新开始图像移动?

Thank you in advance!先感谢您!

You can simplify things using CSS transitions instead of JQuery animate() .您可以使用 CSS transitions 而不是 JQuery animate()来简化事情。 This feature is easy to manage and with less code.此功能易于管理且代码较少。

I've just created an initial CSS state and a final stage ( .end ), using jQuery to toggle the class to switch from initial/final position.我刚刚创建了一个初始 CSS 状态和一个最终阶段 ( .end ),使用 jQuery 来切换类以从初始/最终位置切换。

 $(document).ready(function () { setInterval(function(){ $("#imageId").toggleClass("end"); },2000); });
 img{ position: absolute; width: 5vw; height: 5vw; top: 95vh; left: 0; transition: linear 1s; } img.end{ top: 0; left: 95vw; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img id="imageId" class="start" width="48" height="48">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM