Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webkit transition syntax in javascript?

I'm looking for the webkitTransition object reference here

 function spawnAnimation(what){
    //sets the moving element
    var moveingEl = document.getElementById(what);
    //gives temp transition property
    moveingEl.style.WebkitTransition = "left 2s";
   // moveingEl.style.webkitTransition = "top 500ms";

   var cLeft = moveingEl.style.left
   var cleft = Number(cLeft.slice(0, -2));

    var cTop = moveingEl.style.top
   var cTop = Number(cTop.slice(0, -2));

   moveingEl.style.left = cLeft+200 + "px";

}

This does not work.I would like to give the element a transition property, then make it move to the right. When this code is called it just immediately moves to the right with no animation. bummer :(. I don't want to predefine it in CSS, I would like to dynamically add it and then remove it.

like image 485
Philll_t Avatar asked Sep 14 '11 04:09

Philll_t


People also ask

What is WebKit transition?

-webkit-transition is a non-standard boolean CSS media feature whose value indicates whether vendor-prefixed CSS transition s are supported or not. This media feature is only supported by WebKit. The standards-based alternative is to use a @supports feature query instead.

How do you transition in JavaScript?

The solution is actually quite simple using JavaScript. To trigger an element's transition, toggle a class name on that element that triggers it. To pause an element's transition, use getComputedStyle and getPropertyValue at the point in the transition you want to pause it.

What can I use instead of WebKit transition?

Use the @supports (transition) feature query instead.

What is WebKit transition-duration?

The transition-duration CSS property sets the length of time a transition animation should take to complete. By default, the value is 0s , meaning that no animation will occur.


2 Answers

You can use style.setProperty to modify any property using its CSS name as string, including -moz-* and -webkit-* properties.

const style =  document.getElementById('my-div').style
const prop = (k, v) => style.setProperty(k, v)

function bounce() {
  prop("-webkit-transition", "top .5s ease-in");
  prop("top", "50px");
  setTimeout(() => {
    prop("-webkit-transition", "top .75s cubic-bezier(0.390, 0.575, 0.565, 1.000)");
    prop("top", "0px");
  }, .5 * 1000)
}

prop("-webkit-transition", "top .5s ease-in");
setInterval(bounce, (.75 + .5) * 1000);
#my-div {
  background: red;
  border-radius: 50%;
  width:50px;
  height:50px;
  position:absolute;
  top: 0px;  
}
<div id="my-div"></div>
like image 51
kbtz Avatar answered Oct 04 '22 20:10

kbtz


Allow 1ms for the rendered to get the thread back.

setTimeout(function() {
    myElement.style.height = '200px'; /* or whatever css changes you want to do */
}, 1);​
like image 30
thomas-peter Avatar answered Oct 04 '22 20:10

thomas-peter