0

this is the javascript code i've been using. and i keep getting the error

    '''
    cannot set property 'animationplaystate' of undefined.
    '''

    '''
    window.setTimeout(function time(){
      var run=document.getElementsByClassName('logo');
      run.style.animationplaystate='paused';
     }, 3000);
    '''

below is the style of the element

'''
.logo{
color: rgb(0, 158, 150);
float:right;
padding-right:0px;
font-weight: 600;
padding-top:5px;
margin-right: 20px;
font-size: 2em;
padding-left: 30px;
animation-name:logo;
animation-duration: 1.5s;
animation-play-state: running; 
}

'''
  • is this code below the tag or above the tag ? reason I am asking your code is running before the tag is actually rendered, use jQuery document ready event and set the CSS – Dickens A S Mar 29 '20 at 02:58
  • those are two seperate code in a seperate file – Joel Joseph Mar 29 '20 at 11:09
  • that doesn't matter, if you include ` – Dickens A S Mar 29 '20 at 11:14
  • anyway, I gave the answer below, I hope it should work – Dickens A S Mar 29 '20 at 11:15

3 Answers3

0
run.style['animation-play-state'] = 'paused';
double-beep
  • 5,031
  • 17
  • 33
  • 41
Ron
  • 5,900
  • 2
  • 20
  • 30
  • Please don't post only code as answer, but also include an explanation what your code does and how it solves the problem of the question. Answers with an explanation are generally of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Mar 29 '20 at 08:43
0

As per the documentation Mozilla Developer Network

The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s)

so techniclly getElementsByClassName always return an array
You need use array index to access the elements
Also the style attribute is camel case animationPlayState
in your code all are in small letters

Try this code

run[0].style.animationPlayState = 'paused'

To start animation and stop animation purely using CSS you can try

.logo {
  animation-play-state: paused; 
}

.logo:hover {
  animation-play-state: running;
}

so, for your case, you don't even need javascript

Dickens A S
  • 3,824
  • 2
  • 22
  • 45
0

From what I know:

The DOM is not ready yet for the browser API to tell JavaScript more. Window setTimeout is executed before DOM is ready (before window.onload).

You are facing this p.b: Javascript document.getElementsByClassName returning undefined

my answer is to wrap in window.load (or document.ready):

window.addEventListener("load", function() 
{
    // code here
});

Consider document ready too. window.onload vs $(document).ready()

AymericFi
  • 140
  • 1
  • 8