简体   繁体   English

javascript中2点之间的过渡图片

[英]Transition image in javascript between 2 points

I'm trying to do an image transition in javascript between 2 points. 我正在尝试用JavaScript在2点之间进行图像过渡。 First coordonates are the image initial x and y and second are the x and y of the "click" event. 第一个坐标是图像的初始x和y,第二个是“ click”事件的x和y。 How can I simulate a transition in Javascript? 如何模拟Javascript中的过渡?

Your code will just move the image from one position to another in a jiffy (not like a smooth animation). 您的代码只需将图像从一个位置移动到另一个位置(不像平滑动画)。

If this is what you are doing, then just make position of your img relative . 如果这是您要执行的操作,则只需将img relative position确定即可。

<img id="player" src = "images/volley.png" style="postion:relative"></img>

You can use CSS3 transitions your JavaScript seems correct: 您可以使用CSS3转换来使JavaScript看起来正确:

transition: all 1s ease;

You can change ease to linear depending on the effect you want. 您可以根据需要的效果将ease更改为linear You can also adjust 1s for how long it takes to move. 您还可以调整1s来移动多长时间。 You also need position: relative so left and top can actually do something 您还需要position: relative所以lefttop其实可以做一些事情

 var x_click; var y_click; var player = document.getElementById("player"); document.addEventListener("click", move_player) function move_player(e) { x_click = e.clientX; y_click = e.clientY; player.style.left = x_click - player.clientWidth / 2 + "px"; player.style.top = y_click - player.clientHeight / 2 + "px"; } 
 .animateable { position: relative; transition: all 1s ease; } 
 <!--Some image I found--> <img id="player" class="animateable" src="https://jardinsoli.files.wordpress.com/2011/10/person.png" /> 

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

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