What is the most efficient way to set multiple styling on elements in javascript?
for (i=0;i<=lastSelector;i++) {
var e = mySelector[i],
v = 'opacity 1s';
e.style.WebkitTransition = v;
e.style.MozTransition = v;
e.style.OTransition = v;
e.style.MsTransition = v;
e.style.transition = v;
e.style.opacity = 0;
};
Pretty much that, you could use a stacked assignment:
Since there are several of these properties where we have vendor-specific versions, you might consider a reusable function that does this, e.g.:
Or using the dashed style instead, since we’re already using strings rather than identifiers:
Then:
Side notes:
;after the closing}in aforstatement.varanywhere in a function is function-wide, so declaringvarwithin non-function blocks inside the function is (slightly) misleading to the reader of the code; details: Poor, misunderstoodvar