JavaScript add css transition time to toggle class using javascript

I want to add a transition time to my pure javascript toggle class function I knew how to code it but i forgot and now need some advice

<!DOCTYPE html>
<html>
<head>

<style type="text/css">
#div{
    width: 200px;
    height: 150px;
}
.class1 {
    color: #f00;
    background-color: #2fadac;
}

.class2 {
    color: #00f;
    background-color: #c33;
}
</style>
</head>
<body>
<div id="div" class="class1">click here</div>
<script>
function classToggle() {
    this.classList.toggle('class1');
    this.classList.toggle('class2');
    style.cssText ="-webkit-transition: all 0.5s ease-in-out;";
}
document.querySelector('#div').addEventListener('click', classToggle);
</script>
</body>
</html>

any help would be appreciated

Answer:1

Try add the rule for transition in your target class itself.

.class1 {
    color: #f00;
    background-color: #2fadac;
   -webkit-transition: all 0.5s ease-in-out;
}

.class2 {
    color: #00f;
    background-color: #c33;
    -webkit-transition: all 0.5s ease-in-out;

}

Demo

Or just apply the rule in a separate rule:

CSS:-

.class1 {
    color: #f00;
    background-color: #2fadac;
}

.class2 {
    color: #00f;
    background-color: #c33;
}
.transition{
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition:all 0.5s ease-in-out;
  -o-transition:all 0.5s ease-in-out;
  transition:all 0.5s ease-in-out;
}

HTML:

<div id="div" class="class1 transition">click here</div>

Demo

Answer:2

I'm trying to make a countdown that is counting down in milliseconds; however, the countdown actually takes much longer than 7 seconds. Any idea as to why? function countDown(time){ var i = 0; ...

I'm trying to make a countdown that is counting down in milliseconds; however, the countdown actually takes much longer than 7 seconds. Any idea as to why? function countDown(time){ var i = 0; ...

<div class="foo"> <input name="moobar" /> <input name="foobar" /> </div> How do I replace all "oo" in the names found in $('.foo');? e.g. div.replace('oo', 'aa') => moobar ...

<div class="foo"> <input name="moobar" /> <input name="foobar" /> </div> How do I replace all "oo" in the names found in $('.foo');? e.g. div.replace('oo', 'aa') => moobar ...

  1. find replace name manager excel
  2. find replace name
  3. search and replace name
  4. find and replace name of files
  5. find replace file name windows
  6. find and replace name of files linux
  7. find and replace sheet name in excel formula
  8. maya find and replace name
  9. find and replace class name in jquery
  10. find and replace sheet name in excel
  11. bash find replace file name
  12. find and replace folder name
  13. autocad find replace layer name
  14. find and replace file name in unix
  15. find and replace file name mac
  16. find and replace in name manager
  17. intellij find replace file name
  18. find and replace file name in folder
  19. applescript find replace file name
  20. excel find replace file name

I am writing a javascript program with a very long-running foreach loop like the following: for (property in object) { difficultTask(); } I've seen that you can do some things with setTimeout ...

I am writing a javascript program with a very long-running foreach loop like the following: for (property in object) { difficultTask(); } I've seen that you can do some things with setTimeout ...

i am new to javascript. i want to create SVG from base64. i am trying http://jsfiddle.net/XTUmV/28/ but it doesnt show anything. var image="data:image/png;base64,...

i am new to javascript. i want to create SVG from base64. i am trying http://jsfiddle.net/XTUmV/28/ but it doesnt show anything. var image="data:image/png;base64,...

  1. create base64 from image
  2. create base64 from file
  3. create base64 from blob
  4. create base64 from string
  5. create base64 from svg
  6. create base64 from url
  7. create base64 from blob javascript
  8. create base64 from image online
  9. create base64 from jpg
  10. create file from base64 string javascript
  11. create image from base64 string php
  12. create file from base64 string
  13. create image from base64 string laravel
  14. create file from base64 string java
  15. create pdf from base64 string
  16. create file from base64 string c#
  17. create image from base64 javascript
  18. create image from base64 string c#
  19. create image from base64 string javascript
  20. create file from base64 javascript