html - How do I fade out a div using pure JavaScript? - Stack Overflow

I have the following code for a preloader on my site:setTimeout(function() {document.getElementById(&qu

I have the following code for a preloader on my site:

setTimeout(function() {
  document.getElementById("loader-wrapper").style.display = 'none';
}, 1250);
<div class="loader-wrapper" id="loader-wrapper">
  <div id="loading">
    <div class="loader"></div>
  </div>
</div>

I have the following code for a preloader on my site:

setTimeout(function() {
  document.getElementById("loader-wrapper").style.display = 'none';
}, 1250);
<div class="loader-wrapper" id="loader-wrapper">
  <div id="loading">
    <div class="loader"></div>
  </div>
</div>

Currently the div disappears after 1.25 seconds, but how do I make the loader-wrapper fade out after those 1.25 seconds (without using CSS or jQuery)?

Share Improve this question edited May 11, 2020 at 7:13 Penny Liu 17.6k5 gold badges86 silver badges108 bronze badges asked Jan 7, 2018 at 23:26 Owen SullivanOwen Sullivan 1431 gold badge2 silver badges10 bronze badges 6
  • 3 Out of curiosity why do you want to avoid using CSS (I fully understand wanting to avoid jQuery), which has support for transitioning visibility, opacity, colours and any other properties with numerical property-values? – David Thomas Commented Jan 7, 2018 at 23:33
  • @DavidThomas mostly because I have a lot of interfering CSS (my fault for borrowing CSS from my other projects) and I'd much rather use javascript then hunt down the interfering code – Owen Sullivan Commented Jan 7, 2018 at 23:36
  • 3 While it's your prerogative to do as you wish, I will point out that that approach is likely to cause you problems with the vast whorl of spaghetti at a later point. You'd be much better off untangling everything at the beginning, rather than the end (or any other point). – David Thomas Commented Jan 7, 2018 at 23:39
  • Probably a duplicate of Fade element in and run a callback, just reverse to fade out instead of in. There is also Fade element from specidied opacity to specified opacity? Once the element is fully faded, remove it (or set display:none). – RobG Commented Jan 7, 2018 at 23:39
  • @DavidThomas if I were to do it using CSS, how would I? I tried using visibility: visible; and opacity: 1;, then transition: visibility 1s, opacity 1s linear; but that didn't change anything – Owen Sullivan Commented Jan 7, 2018 at 23:42
 |  Show 1 more ment

3 Answers 3

Reset to default 6

Look into object.style.transition JavaScript syntax.

Here is a simplified running example of @Owen Sullivan's solution.

setTimeout(function() {
  var loader = document.getElementById("loader-wrapper");
  loader.style.transition = '.5s';
  loader.style.opacity = '0';
  loader.style.visibility = 'hidden';
}, 1250);
<div class="loader-wrapper" id="loader-wrapper">
  <div id="loading">
    <div class="loader">loader</div>
  </div>
</div>

You can do it with using of inline styles on the loader element:

<script>
  setTimeout(function() {
    var el = document.getElementById("loader-wrapper");
    // 1s - time of the animation duration
    // set transition property for webkit browsers only
    el.style.WebkitTransition = 'opacity 1s ease-in-out;'
    el.style.opacity = '0';
  }, 1250);
</script>

JSbin link.

To set transition for all browsers you need to set all transition properties. You can get if from this question.

For more information and animation examples check this question.

What ended up working for me:

<script>
  setTimeout(function() {
    var loader = document.getElementById("loader-wrapper");
    loader.style.WebkitTransition = 'visibility .5s, opacity .5s';
    loader.style.opacity = '0';
    loader.style.visibility = 'hidden';
  }, 1250);
</script>

This sets the visibility to hidden and allows all the content underneath the loader-wrapper div to be interacted with (in my case, buttons and forms).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744348803a4569852.html

相关推荐

  • html - How do I fade out a div using pure JavaScript? - Stack Overflow

    I have the following code for a preloader on my site:setTimeout(function() {document.getElementById(&qu

    8天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信