I am writing a small plugin that lets the user easily add transitions in javascript without jquery:
HTML:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Snippety</title>
<script src="snip.js" type="text/javascript"></script>
<link rel="stylesheet" href="snip.css" type="text/css" />
</head>
<body>
<script>
transition(
transSelector = '.snip',
transTime = '5s',
transDelay = 0000,
transFunc = 'transTo("background-color:red!important;")'
);
</script>
<div class="snip" id="snip">buhhug</div>
</body>
</html>
Js Plugin:
function transition(transSelector,transTime,transDelay,transFunc) {
document.styleSheets[0].insertRule(''+transSelector+' {transition:'+transTime+';-webkit-transition:'+transTime+';}');
setTimeout(transFunc, transDelay)
}
function transTo(rule) {
document.styleSheets[0].insertRule(''+transSelector+' {'+rule+'}');
}
CSS:
.snip {
background-color: green;
}
The problem is that, when the color is changed to red in javascript, I need to add !important
to change .snip
tobackground-color:red;
, but it only works once.
Is there any way to delete the background-color:green;
in the css stylesheet, or override without having to put the !important
in it? (I aim to change the color more than once).
I have tried .addRule
instead of .insertRule
, but I think I am barking up the wrong tree.