简体   繁体   English

强制CSS重新渲染

[英]Force the css to re-render

I have some articles that are styled is sass as such 我有些文章的风格是这样

padding-bottom: 125px
padding-top: 125px
border-bottom: 1px solid rgba(0,0,0,0.1)
&:first-of-type
    padding-top: 0
    &:last-of-type
        padding-bottom: 0
            border-bottom: none

I have a script that causes all articles that do not have a similar class to fadeout when a category is clicked like so 我有一个脚本,当单击一个类别时,该脚本会导致所有没有相似类的文章淡出

// Category Switching
$('.cats span').click(function(){
    console.log('click');
    var cat = $(this).attr('class');
    if(cat == 'all'){
        $('.articles').find('article').fadeIn();
    } else {
        $('.articles').find('article:not(.'+cat+')').fadeOut();
    }
});

What happens, obviously, is that what was the first article may no longer be the first article, therefore that padding at the top of the post, and even so for the last post with the padding on the bottom, needs to be taken off, but since the CSS is already rendered it does not know that there has been a change in the DOM. 很明显,发生的事情是第一篇文章可能不再是第一篇文章,因此,帖子顶部的填充,甚至对于底部填充的最后一篇帖子,也需要删除,但是由于CSS已经渲染,所以它不知道DOM发生了变化。

How can I get the CSS to reevaluate the DOM and see these changes after this script runs? 运行此脚本后,如何获取CSS重新评估DOM并查看这些更改?

You can select all the visible elements, So after you do your fadeIn and fadeOut you can select all .articles which are :visible. 您可以选择所有可见的元素,因此,在执行fadeInfadeOut ,可以选择所有可见的.articles

docs : docs:

http://api.jquery.com/visible-selector/ http://api.jquery.com/visible-selector/

You can then select the first and last dom elements and give them a .last_article and .first_article` css classes. 然后,您可以选择第一个和最后一个dom元素,并为它们提供.last_article and .first_article` css类。

(those classes should have your top and bottom padding for example) (例如,这些类应有顶部和底部填充)

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

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