简体   繁体   English

如何加强伪CSS规则

[英]How to reinforce pseudo css rules

Is there anyway to reinforce pseudo css rules? 无论如何,有没有加强伪CSS规则? ie: 即:
I have a listing of divs (playlist) which I color with the following rules: 我有一个div(播放列表)列表,并使用以下规则为其着色:

#playlist .playlist_item { 
  background: #d6d6d6;
}

#playlist .playlist_item:nth-child(odd) {
  background: #b3b3b3;
}

Now, when a song is playing, I use setInterval and JQuery's .animate function to pulse the background color. 现在,当播放一首歌曲时,我使用setInterval和JQuery的.animate函数使背景色脉动。 When the song is finished I clear the interval, but of course the song's background remains the last color set in the interval. 歌曲结束后,我清除了间隔,但是当然,歌曲的背景仍然是间隔中最后设置的颜色。 Is there a way to re-set the song's color based on the CSS rule? 有没有一种方法可以根据CSS规则重新设置歌曲的颜色? Otherwise I'll have to keep track of the previous song (which may have changed position, and thus color) or setup distinct background color classes and reset the classes of all the songs in the playlist anytime someone adds, removes, or moves a song in the playlist or a song ends. 否则,我将不得不跟踪上一首歌曲(位置可能会因此改变,从而改变颜色),或者设置明显的背景颜色类别,并在有人添加,删除或移动歌曲时重置播放列表中所有歌曲的类别。在播放列表中或歌曲结束。 I'd much rather use a CSS only approach. 我宁愿使用仅CSS方法。

Thanks in advance, Dan 预先感谢,丹

You're defining a default background-color with lower specificity and half of the cases with higher specificity. 您正在定义具有较低特异性的默认背景色,而有一半情况具有较高的特异性。 That said, here are two CSS solutions: 也就是说,这里有两个CSS解决方案:

First: 第一:

#playlist .playlist_item  { /* only here for compatibility with browsers that don't understand CSS3 selectors */
  background: #d6d6d6;
}

#playlist .playlist_item:nth-child(even) { 
  background: #d6d6d6;
}

#playlist .playlist_item:nth-child(odd) {
  background: #b3b3b3;
}

The first and second rules can't be combined if you must support old browsers, otherwise you can erase the first rule. 如果必须支持旧版浏览器,则不能将第一条规则和第二条规则结合使用,否则可以删除第一条规则。 Even and odd cover every case so it's safe to do it. 偶数和奇数覆盖所有情况,因此这样做是安全的。

Second: 第二:

#playlist .playlist_item  { /* compatibility reasons again */
  background: #d6d6d6;
}

#playlist .playlist_item:nth-child(n) { 
  background: #d6d6d6;
}

#playlist .playlist_item:nth-child(odd) {
  background: #b3b3b3;
}

The second rule has the same effect as the first one, except with a higher priority. 第二个规则与第一个规则具有相同的效果,但优先级更高。 It's the fourth example in CSS3 selectors - Structural pseudo-classes 这是CSS3选择器中的第四个示例-结构化伪类

An alternative that I don't see mentioned above is to use CSS3 animation for your pulsing. 我上面没有提到的另一种方法是使用CSS3动画进行脉动。 Make JS apply a "is-pulsing" class; 使JS应用“ is-pulsing”类; the pulsing animation loops infinitely until the class is removed; 脉冲动画无限循环,直到删除该类为止; then the element's style reverts to its regular, non-pulsing background. 然后元素的样式恢复为其常规的无脉动背景。 I made a CodePen to illustrate: http://codepen.io/MisterGrumpyPants/pen/enGcC 我做了一个CodePen来说明: http ://codepen.io/MisterGrumpyPants/pen/enGcC

In addition to being simple, this solution would also perform better than JS animation. 除了简单之外,此解决方案还将比JS动画表现更好。 Worth a try. 值得一试。

Set the style attribute to "" 将样式属性设置为“”

For example: 例如:

node.style.backgroundColor=""

I'm not 100% sure on this one, but jQuery's animate is changing the style attribute directly on the element, so, after clearing the interval, if you did something like this: 我不确定是否对此有100%的把握,但是jQuery的动画是直接在元素上更改style属性,因此,在清除间隔后,是否进行了以下操作:

$(this).attr("style","");

It should clear anything in the style attribute, and let the CSS property come through again. 它应该清除style属性中的所有内容,并再次让CSS属性通过。

here's an example: 这是一个例子:

$('.playlist_item').click(
        function() {
             var $dv = $(this);
             $dv.data('oldBG', $dv.css('backgroundColor')); // save the original background color
            $dv.animate({backgroundColor: '#ff0000'}, 2000, function() {
                 $(this).css('backgroundColor', $(this).data('oldBG')); // once done, apply old background color
            });                 
       });

Running example 运行示例

If you wanted to get the target background color, you could add a css class and item for that: 如果要获取目标背景色,则可以为此添加一个CSS类和项:

.playlist_item_highlight { display:none; .playlist_item_highlight {display:none; background:#FF0000; 背景:#FF0000; } and add that the document so you could $('#playlist .playlist_item_hightlight').css('backgroundColor'); }并添加该文档,以便您可以$('#playlist .playlist_item_hightlight').css('backgroundColor');

Solution I chose: 我选择的解决方案:
I am keeping track of the previously played song using jQuery ie: 我使用jQuery跟踪以前播放的歌曲,即:

currently_playing = $('#' + currElmId);

This is done after sending the song to the flash. 将歌曲发送到闪存后即可完成。

Earlier in the script (to stop the previous song from flashing) I simply reset the css for the appropriate elements using: 在脚本的前面(以防止上一首歌曲闪烁),我只需使用以下命令为相应的元素重置css:

currently_playing.stop(true, false);
clearInterval(highlight);

Hope this can help someone else. 希望这可以帮助其他人。

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

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