How can I set CSS using javascript (I don't have access to the CSS file)?
#fade div {
-webkit-transition: opacity 1s;
-moz-transition: opacity 1s;
-o-transition: opacity 1s;
-ms-transition: opacity 1s;
transition: opacity 1s;
opacity: 0;
position: absolute;
height: 500px;
width: 960px;
}
For example:
document.getElementById('fade').HOW-TO-TYPE-webkit-transition = 'opacity 1s';
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. Then set those CSS properties of that element equal to those values you just got.
To make the transition occur, you must specify at least two things — the name of the CSS property to which you want to apply the transition effect using the transition-property CSS property, and the duration of the transition effect (greater than 0) using the transition-duration CSS property.
Triggering transitions You can trigger CSS transitions directly with pseudo classes like :hover (activates when the mouse goes over an element), :focus (activates when a user tabs onto an element, or when a user clicks into an input element), or :active (activates when user clicks on the element).
CSS animations make it possible to do simple animations without JavaScript at all. JavaScript can be used to control CSS animations and make them even better, with little code.
You should look here: http://www.javascriptkit.com/javatutors/setcss3properties.shtml
As you can see setting CSS Properties with "-" just results in the next character to be capital:
document.getElementById('fade').style.WebkitTransition = 'opacity 1s'; document.getElementById('fade').style.MozTransition = 'opacity 1s';
you should use the camelCase notation like so:
document.getElementById('fade').style.webkitTransition = 'opacity 1s';
like every property composed by more than one word and hyphen-separated (e.g. css background-position
turns into js backgroundPosition
.
Probably at this time not every browser adopted this notation in properties involving browser specific prefixes, so there are some browser like firefox still accepting Moz
instead of moz
(see https://bugzilla.mozilla.org/show_bug.cgi?id=607381)
var vendors = [
'-webkit-',
'-o-',
'-moz-',
'-ms-',
''
];
function toCamelCase(str) {
return str.toLowerCase().replace(/(\-[a-z])/g, function($1) {
return $1.toUpperCase().replace('-', '');
});
}
function setCss3Style(el, prop, val) {
vendors.forEach(function(vendor) {
var property = toCamelCase(vendor + prop);
if(p in el.style) {
el.style[p] = val;
}
});
}
usage :
setCss3Style(someElement, 'transition', 'opacity 1s');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With