3

I want either remove or reset a style applied on a particular DOM node using JS.

            node.style.webkitTransitionDuration = '5000ms';
            node.style.webkitTransformOrigin = '200px 200px';
            node.style.webkitTransform = 'rotateZ(25rad)';

I want to reset/set webkitTransform time and again on a fire of an event
Tried like this

        node.style.webkitTransform = 'rotateZ(0rad)';
        node.style.webkitTransform = 'rotateZ(25rad)';

But its not working.

P.S. Can not use any framework.

Alexis Abril
  • 6,389
  • 6
  • 33
  • 53
Ashish Agarwal
  • 6,215
  • 12
  • 58
  • 91

3 Answers3

2

If you are looking to remove the rule from the element then this should work:

node.style.webkitTransform = ''; 
Rozwel
  • 1,990
  • 2
  • 20
  • 30
2

Here's an example that should fit your needs. It toggles when you click on the document. Note: it of course only works in Webkit-based browsers.

animateNode(document.getElementById("test"), "5000ms", "200px 200px", "rotateZ(25rad)");
var toggle = toggleValue();
function animateNode(node, duration, origin, transform)
{
    node.style['webkitTransitionDuration'] = duration;  
    node.style['webkitTransformOrigin'] = origin;  
    node.style['webkitTransform'] = transform;
}

function toggleValue() {
    var num = 1;
    return function ()
    {
       return ++num;
    };
}

document.onclick = function()
{
    var toggleNum = toggle();
    if(toggleNum % 2 === 0)
    {
        animateNode(document.getElementById("test"), "5000ms", "200px 200px", "rotateZ(0rad)");  
    }else if(toggleNum % 2 === 1)
    {
        animateNode(document.getElementById("test"), "5000ms", "200px 200px", "rotateZ(25rad)"); 
    }
}

http://jsfiddle.net/GdGw7/3/

0

You say you don't want to use a framework. but if it is OK (according to your comment) to do so:

You can use jQuery:

so you can do in jQuery:

$('selector').css({
   webkitTransitionDuration = '5000ms',
   webkitTransformOrigin = '200px 200px',
   webkitTransform = 'rotateZ(25rad)'
})

you can clear out the style element with:

$('selector').attr('style','');

add class:

$('selector').addClass('newClass');

remove class:

$('selector').removeClass('rClass');
Naftali
  • 144,921
  • 39
  • 244
  • 303