javascript - Setting inline css transition style with string alters the true value. Bizarre -


I have a stylesheet that sets up a CSS transition property such as (prefix versions for short): < / P>

  Infection: Opacity 1s;   

Then I have several elements on the page, and I want to modify the transition-delay property of each element through javascript, giving a conflict effect for . I am using jQuery like this:

  $ (element). Css ('transition-delay', delay + 's'); However, the above code adds an inline  transit-delay: x  to the  No  element, instead, the result is:   

/ P>

  & lt; Div style = "transition: xs;" & Gt;   

But that's okay, because it works as expected, the browser knows that transition: Xs actually means that just the code To process, set transition-delay and retain the rest.

However:

If I now meet the inline style of that element through the $ (element) .attr ('style') , and After that it is re-applied to the element, $ (element) .attr ('style', style) , HTML exactly looks identical, but the transition is completely identical Overwrites other properties and essentially the transition value of the element to all xs 0s ease .

 Before  // html - working & gt; Div style = "transition: xs" & gt; // so I do this var style = $ (l) .attr ('style'); $ (L) .attr ('style', style); // After HTML - broken! & Lt; Div style = "transition: xs" & gt;   

Demo

One JSDell I've described:

What's going on?

I think that just writing the question and that demo really helped me find answers: < / P>

HTML style attribute not is the actual style we need to use the CSSStyleDeclaration object

although it seems that inline style Which is as simple as style = "... ..." in the HTML attribute (as I had eclipse), it turns out that this is not the case behind the scenes, inline style (And all other genres) are actually defined by an object in this object, > displays string in the attribute, but defines a style in it Not all the information required for is included.

This is the reason that setting "El.style =" width: 100px; " no works by:

Except in Opera, Styles can not be set by specifying the string for the property of the property (read only), as elt.style = "color: blue;" The reason for this is that the style attribute gives a CSSStyleDeclaration object. Instead, you can set the style property of this type:

  elt.style.color = "blue"; // Directional Saint = Elite Style; St.color = "blue"; // indirectly   

then it shows us why $ (el) .attr ('style', 'transition: Xs'); Why no do the required work - and this is what I was walking on. This will be modify the built-in CSSStyleDeclaration object, but the way we want it (hence my basic question) is not always.

The solution is to use the provided API as CSSStyleDeclaration. The following problem proved me important in understanding this problem:

Copying a CSSStyleDeclaration:
  var original style = el.cloneNode (). Style;   

Here we are using the clonnode () method because otherwise (if we just get the L. style) the CSSStyleDeclaration object is copied by reference, which is what I do not want Because I'll change the inline style of the element and then I want to restore the original style later. Before cloning the element, we are allowed to get a "fresh" copy of CSSStleDeclaration which will not be able to change the inline styles of our element L .

Saved CSSStyleDeclaration by changing the existing inline style
  // First we need to remove all style rules / present (var i = el.style.length; I> 0; i--) {var name = el.style [i]; El.style.removeProperty (name); } // Now let us loop through the original CSSStyleDeclaration // object and set each property to its original value (var i = original style.length; i & gt; 0; i--) {var name = original style [I]; El.style.setProperty (name, originalStyle.getPropertyValue (name), priority = original style .getPropertyPriority (name)); }   

Demo

This is an update to my original demo which implements the above methods:

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -