简体   繁体   English

使用 jQuery 切换类时,CSS 样式不会立即应用

[英]CSS styles not applying immediately when class toggled with jQuery

I am trying to change the style of an element while some javascript is running in the background, to indicate that the page is 'busy' or 'loading'.我正在尝试在后台运行某些 javascript 时更改元素的样式,以指示页面“忙碌”或“正在加载”。 I've been attempting to do this by toggling a class on at the start of the JS script with jQuery's .toggleClass() , and toggling it off at the end, with some suitable CSS styling attached to the class.我一直在尝试通过在 JS 脚本开始时使用 jQuery 的.toggleClass()切换一个类来实现这一点,并在最后将其关闭,并在类中附加一些合适的 CSS 样式。

Although the class is toggled immediately, the CSS styling attached to it doesn't apply until after the JS has finished executing, however.尽管该类会立即切换,但附加到它的 CSS 样式在 JS 完成执行后才会应用,但是。 So if the class is toggled both on and off, the user does not see any change in style.因此,如果该类同时打开和关闭,用户将看不到任何样式变化。

I've included a simple example below.我在下面包含了一个简单的例子。 How can I force the change in CSS styling to apply immediately, before the rest of the JS code executes?在其余的 JS 代码执行之前,如何强制立即应用 CSS 样式的更改?

 $(function() { $('#box').click(function() { // Toggle class 'red' on. $(this).toggleClass('red'); // Do something that takes time. for (i = 0; i < 10000; i++) { console.log(i); } // Toggle class 'red' off. $(this).toggleClass('red'); }); });
 .wrapper { margin: 15px; text-align: center; color: #000; } #box { margin: 15px 0; width: 100px; height: 100px; line-height: 100px; cursor: pointer; background: #ccc; border: solid 3px #ccc; -webkit-transition: all .3s linear 0s; transition: all .3s linear 0s; } #box.red { background: #f43059; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <div id="box">Click me.</div> </div>

The problem is that your "something that takes time" is synchronous and blocking - while it's operating, browser repainting will be disabled.问题是您的“需要时间的东西”是同步和阻塞的- 在它运行时,浏览器重绘将被禁用。

One option would be to listen to a transitionend event, to ensure that the animation to red completes before the resource-intensive operation runs.一种选择是监听transitionend事件,以确保在资源密集型操作运行之前完成红色动画。

So that the removal of .red animates properly too, you could set a setTimeout right after the heavy operations finish.为了使.red的移除也能正确设置动画,您可以在繁重的操作完成后立即设置setTimeout Note that your code will a bit be clearer if you use addClass and removeClass instead of toggleClass :请注意,如果您使用addClassremoveClass而不是toggleClass ,您的代码会更清晰一些:

 $('#box').click(function() { $(this).one('transitionend', () => { // Do something that takes time. for (i = 0; i < 1000; i++) { console.log(i); } // Toggle class 'red' off. setTimeout(() => { $(this).removeClass('red'); }); }); $(this).addClass('red'); });
 .wrapper { margin: 15px; text-align: center; color: #000; } #box { margin: 15px 0; width: 100px; height: 100px; line-height: 100px; cursor: pointer; background: #ccc; border: solid 3px #ccc; -webkit-transition: all .3s linear 0s; transition: all .3s linear 0s; } #box.red { background: #f43059; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <div id="box">Click me.</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM