I’m confused about the JavaScript on this code 😣 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

I’m confused about the JavaScript on this code 😣

I’m confused as to how the JavaScript referenced something in css and toggled the animation could someone please explain how I can toggle an amation useing a JavaScript function? https://code.sololearn.com/WLGfO0d739FW/?ref=app (Instead of giving me links is it possible you could copy paste the relevent content from the link you intended to give me? (My parents have it where all websites are locked so I cant view the links))

12th Jul 2018, 2:44 AM
Rtvq
Rtvq - avatar
10 Answers
+ 1
Yes, you can modify CSS with plain JavaScript. It's fairly easy. Here is a demo. It prints "Hello" in red. HTML: <p id="textbox">Hello</p> JavaScript: var textbox = document.getElementById("textbox"); textbox.style.color = "red"; I used an ID on the HTML element. In JavaScript, I then used what we call DOM (Google it) to save a link to the element in a variable. Then adding .style to the variable accesses the CSS. Adding .color opens the CSS property. Red must be in a string, so put quotes around it, "red". I could alternatively do .style.backgroundColor (replacing the hypen with a capital letter). Basically the JavaScript writes this into the CSS... #textbox { color: red; }
12th Jul 2018, 3:26 AM
James
James - avatar
+ 2
D'lite any part lol im just confused as to how one can toggle a css animation with a javascript function
12th Jul 2018, 2:56 AM
Rtvq
Rtvq - avatar
+ 2
James thank you that helps alot
12th Jul 2018, 3:11 AM
Rtvq
Rtvq - avatar
+ 2
Beth Since its a CSS animation, it's easy. You could just put all the animations into a different class. Maybe the className is "animate". The Javascript code would then be something like ============ window.onload =function(){ var elem= document.querySelector("#elem"); var button= document.querySelector("button") button.onclick=function(){ elem.classList.toggle("animated"); } }
12th Jul 2018, 3:31 AM
Dlite
Dlite - avatar
+ 2
I’m not sure, “Rtvq” aka Beth
25th Jul 2018, 10:45 PM
🐺Michael🐺
🐺Michael🐺 - avatar
+ 1
It is quite a complex code. Also, this is jQuery, not just plain JavaScript. It could be done without jQuery, but jQuery makes it easier. And, yes, JavaScript can modify CSS. Basically, a dice is drawn using CSS. The JavaScript chooses a random number to appear on the dice, and also chooses random values for how far and fast it should spin. The jQuery then uses these values to modify the CSS, telling the dice to rotate around on certain axes. If you want to understand each line of the code and what each of the functions mean, you will need to learn CSS, JS and jQuery.
12th Jul 2018, 3:04 AM
James
James - avatar
+ 1
Are you interested in web animations? Want some tips on what to learn?
12th Jul 2018, 3:12 AM
James
James - avatar
+ 1
James yes ives used some in the past but I want to know more about them (ive completed the css and js course) is there a way to toggle simple Css animations using Javascript?
12th Jul 2018, 3:14 AM
Rtvq
Rtvq - avatar
+ 1
James that helps so much thank you 🙂
12th Jul 2018, 3:27 AM
Rtvq
Rtvq - avatar
+ 1
Another cool way to animate is using CSS keyframes. You should search Google for some tutorials. w3schools is a good keyframes reference.
12th Jul 2018, 3:29 AM
James
James - avatar