css - Remove Body Class After Window Load with Javascript - Stack Overflow

I'm using a CSS class on the body to stop transitions running when the document loads. How can I r

I'm using a CSS class on the body to stop transitions running when the document loads. How can I remove it once the Window has loaded with JavaScript (no jQuery)?

HTML:

<body class="preload">

css:

.preload * {
  -webkit-transition: none !important;
  -moz-transition: none !important;
  -ms-transition: none !important;
  -o-transition: none !important;
}

This would be the jQuery equivalent *except I'm not using jQuery:

$("window").load(function() {
  $("body").removeClass("preload");
});

I'm using a CSS class on the body to stop transitions running when the document loads. How can I remove it once the Window has loaded with JavaScript (no jQuery)?

HTML:

<body class="preload">

css:

.preload * {
  -webkit-transition: none !important;
  -moz-transition: none !important;
  -ms-transition: none !important;
  -o-transition: none !important;
}

This would be the jQuery equivalent *except I'm not using jQuery:

$("window").load(function() {
  $("body").removeClass("preload");
});
Share Improve this question asked Mar 30, 2014 at 13:17 user3143218user3143218 1,8165 gold badges34 silver badges48 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

This should do the trick:

window.addEventListener(
    'load',
    function load()
    {
        window.removeEventListener('load', load, false);
        document.body.classList.remove('preload');
    },
    false);

Using addEventListener means that it won't interfere with any other event listeners that may be attached by other libs, etc.

The above code will remove the event listener once the event has fired too.

I would have thought the event listener should be on document but apparently that doesn't work.

Something like this should work in all browsers:

window.onload = function () { document.body.className = ""; }

Drew's answer is good, but classList is not supported in older versions of IE as well as window.addEventListener, if that's important.

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

相关推荐

  • css - Remove Body Class After Window Load with Javascript - Stack Overflow

    I'm using a CSS class on the body to stop transitions running when the document loads. How can I r

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信