I get Invalid argument error when try to addRule to stylesheet in ie

Anwar Saiah

I am trying to dynamically remove a rule and then add a new one in it's place, to get a calculated animation for a dynamically changing div innerHTML.

var ss = document.styleSheets ;           
for(j=0;j<ss[0].cssRules.length;j++)
if(ss[0].cssRules[j].name == "slide")
{  
ss[0].deleteRule(j);                              
break;
}  
ss[0].addRule('@keyframes slide', 'from {top:0px;} to {top:-300px;}', 0);

The code above should remove a css animation and insert a new one. The problem is with the '@' character, if I remove it code passes, but then it's not an animation for ie cause it doesn't have the '@keyframes' identifier. The reason I am going about this in such a manner is because you don't have variables in css for ie11, and you cannot alter stylesheet csstext! So I thought delete the whole rule and insert a new one. All I'm trying to do is dynamically change this rule:

@keyframes slide { 
     from {top:0px;}  
     to   {top:-100px;}
}

To this rule for example:

@keyframes slide { 
     from {top:0px;}  
     to   {top:-300px;}
}

It is really a pain in the neck to try and deal with ie's special world!! Thanks for your help.

Jenifer Jiang

I've tested your code and tried to find why it not works in IE. But I could not find a good reason for this. It seems like what you've mentioned that IE could not read the keywork with @. I've just found a workaround for this is to customize a funcition to add the attribute. Here is my working demo.

CSS

<style>
    div {
        margin-top: 500px;
        width: 100px;
        height: 100px;
        background: red;
        position: relative;
        animation: mymove 5s infinite;
    }

    @keyframes mymove {
        from {
            top: 0px;
        }

        to {
            top: 200px;
        }
    }
</style>

HTML

<script>
    function insertStyleSheetRule(ruleText) {
        let sheets = document.styleSheets;
        if (sheets.length == 0) {
            let style = document.createElement('style');
            style.appendChild(document.createTextNode(""));
            document.head.appendChild(style);
        }
        let sheet = sheets[sheets.length - 1];
        sheet.insertRule(ruleText, sheet.rules ? sheet.rules.length : sheet.cssRules.length);
    }
    insertStyleSheetRule("@keyframes mymove{from {top:0px;} to {top:-300px;}}");
    insertStyleSheetRule("div {margin-top: 500px;width: 100px;height: 100px;background: red;position: relative; animation: mymove 5s infinite;- webkit - animation: mymove 5s infinite;}");
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

highcharts - error in IE - Invalid Argument

Why do I get an "invalid values" error when I try to access the network settings on Eclipse Mars?

when try to make database separated comma i get error invalid in the select list because it is not contained in either an aggregate?

I get this error when I try to install

Why do I get an invalid RGBA argument ValueError when defining "capsize" to my error bars?

Why do I get this "invalid 'times' argument" error when using left_join() in R?

"amount is invalid" error when i try to withdraw in Bitmex (ccxt)

I get invalid json error when i try to add read access policy in s3 permission tab

"Error: Invalid argument." on IE11

Why do I get RuntimeError: CUDA error: invalid argument in pytorch?

"An annotation argument must be a compile-time constant" error when I try to add a yesterday date to GET parameters (NASA APOD API)

I get the error when i try get 50% of the image with cropperJS

I get an error when i try apt-get update?

IE 11 giving "invalid calling object" when try to call HTTP get request

invalid argument supplied for foreach() when i try to display the auth user data

Error: "Argument data type float is invalid for argument 1 of parse function." when doing try_parse() in SQL

I get an Error when I try to use Firebase Auth

Why I get error when I try to declare local variable?

Why I get error when I try to create stored procedure?

I get stray '#' in program error when I try to compile this program

Error I get when I try install IDE Anjuta

When I try to use scanf with 2 arrays I get an error

I get 404 error when I try endpoint on localhost

I get an error when I try to initiate the front camera (Swift)

When I try to CURL a website I get SSL error

Why do I get an error for "__CrtGetFileInformationByHandleEx " when I try to compile

When i try to create react project i get error

I get error when i try to add value to defaultdict(str)

When I try to sync my Gradle files I get an error