简体   繁体   English

在没有jQuery或其他库的情况下用JavaScript简化动画

[英]Easing animation in JavaScript without jQuery or other libraries

I want to have a <div> come into a webpage from the right when you hover over it. 当您将鼠标悬停在网页上时,我想从右侧进入<div>网页。 So something like this: 所以像这样:

$('div.from_right').hover(function() {
  $(this).animate({right: '-300px'});
});

But I don't want to use jQuery or any other libraries. 但是我不想使用jQuery或其他任何库。 Is there a built-in mechanism within JavaScript to animate the position? JavaScript中是否有内置机制可以对位置进行动画处理? Or do I call requestAnimationFrame() repeatedly until the <div> is where I want it to be? 还是我重复调用requestAnimationFrame()直到<div>是我想要的位置?

I only care about modern browsers. 我只关心现代浏览器。

Use CSS transition. 使用CSS过渡。

div {
    width:100px;
    height:100px;
    background:red;
    -webkit-transition:margin 3s ease-out;
    -moz-transition:margin 2s ease-out;
    -o-transition:margin 2s ease-out;
    -ms-transition:margin 2s ease-out;
    transition:margin 2s ease-out;
}
div:hover {
    margin-left:300px;
}

DEMO. 演示。

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

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