简体   繁体   English

如何管理此背景图片和班级的悬停行为?

[英]How do I manage this background image and the class's hover behavior?

I have an image that loads into a DIV: 我有一张加载到DIV中的图像:

.playerSkipForward
{
    background-image: url("../images/player/forward.png");
    background-repeat: no-repeat;
    cursor: pointer;
}

With hover image set 悬停图像设置

.playerSkipForward:hover
{
    background-image: url("../images/player/forward-hover.png");
}

Then at a certain point I add a class to it that changes the image and makes it look "inactive" 然后在某个时候,我向其添加一个类,该类会更改图像并使它看起来“不活动”

$(track).parents().find('.playerSkipForward').addClass('playerSkipForward_inactive')

The new (inactive) class is declared in my CSS like so 像这样在我的CSS中声明了新的(无效)类

.playerSkipForward_inactive
{
    background-image: url("../images/player/forward_inactive.png");
    background-repeat: no-repeat;
    cursor: default;
}

The image changes to the one in the class above correctly. 图像正确地变为上述类中的一个。 However, it still hovers and changes colors. 但是,它仍然徘徊并改变颜色。 I do not want to add an unbinding like .on('hover') because bindings can be tricky to manage. 我不想添加像.on('hover')这样的取消绑定,因为绑定可能很难管理。 Is there a way to override the hover with just the added css class? 有没有一种方法可以仅使用添加的CSS类覆盖悬停?

Looking at your comments I think this is an issue with CSS specificity . 查看您的评论,我认为这是CSS特定性的问题。

Try using the following CSS class instead of your current .playerSkipForward_inactive : 尝试使用以下CSS类代替当前的.playerSkipForward_inactive

.playerSkipForward.playerSkipForward_inactive,
.playerSkipForward.playerSkipForward_inactive:hover
{
    background-image: url("../images/player/forward_inactive.png");
    cursor: default;
}

Note I've also removed the background-repeat property. 注意,我还删除了background-repeat属性。 You already set the same value for this in your .playerSkipForward rule. 您已经在.playerSkipForward规则中为此设置了相同的值。

.playerSkipForward_inactive, .playerSkipForward_inactive:hover
{
    background-image: url("../images/player/forward_inactive.png");
    background-repeat: no-repeat;
    cursor: default;
}

If you're using two classes at once, try this more specific selector: 如果您一次使用两个类,请尝试使用更具体的选择器:

.playerSkipForward.playerSkipForward_inactive, 
.playerSkipForward.playerSkipForward_inactive:hover
    { 
    ...
    }

or the more common and simple: 或者更常见和更简单:

.playerSkipForward.inactive, 
.playerSkipForward.inactive:hover
    { 
    ...
    }

The following code should do the trick. 以下代码可以解决问题。 Just override the hover functionality for inactive state using important keyword. 只需使用important关键字为非活动状态覆盖悬停功能即可。

.playerSkipForward_inactive:hover
{
    background-image: url("../images/player/forward_inactive.png") !important;
    background-repeat: no-repeat !important;
    cursor: default !important;
}

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

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