【问题标题】:CSS Hover Effects Persisting When I Don't Want It ToCSS 悬停效果在我不想要的时候仍然存在
【发布时间】:2012-07-05 23:27:15
【问题描述】:

我有一个 div 样式为圆形,其中包含图像和一些居中的文本。 如果不悬停,则在文本透明时显示圆圈和图像。 悬停时,圆形边框开始闪烁(webkit 动画),图像的不透明度降低,文本变得可见。

在 Firefox 中编写/测试此代码时,一切正常,但在 Chrome 上,悬停效果的更改仍然存在,我不希望它们(即图像不透明度保持降低,文本保持可见) . 然而,继续悬停在 div 上会使边框按预期闪烁。

我已经获得了所有正确的 webkit/moz/ms/o 过渡和动画,但我似乎无法弄清楚出了什么问题(或者这只是使用 Chrome 的缺陷之一)。

我的 div 及其所有元素的代码是:

<div class='players'>
    <div class='row'>
        <div class='span6'>
            <div class='matchup'>
                <p class='team'>SOMETEAMNAME</p>
                <p class='name'>SOMENAME</p>
                <img src='SOMEIMAGE'>
            </div>
        </div>
    </div>
</div>

我的 CSS 代码:

.matchup {
    width: 250px;
    height: 250px;
    background: transparent;
    border: 1px solid #ff6600;
    border-radius: 125px;
    display: block;
    margin-left: auto;
    margin-right: auto;

    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    -ms-box-sizing: border-box;
    -o-box-sizing: border-box;
    box-sizing: border-box;

    -webkit-transition: 0.5s ease;
    -moz-transition: 0.5s ease;
    -ms-transition: 0.5s ease;
    -o-transition: 0.5s ease;
    transition: 0.5s ease;
}
.matchup img {  
    position: static;
    margin-top: -22%;
    opacity: 1;

    -webkit-transition: 0.5s ease;
    -moz-transition: 0.5s ease;
    -ms-transition: 0.5s ease;
    -o-transition: 0.5s ease;
    transition: 0.5s ease;
}
.matchup p {
    font-family: 'Lobster', cursive;
    position: relative;
    text-align: center;
    top: 50%;
    color: transparent; 

    -webkit-transition: 0.5s ease;
    -moz-transition: 0.5s ease;
    -ms-transition: 0.5s ease;
    -o-transition: 0.5s ease;
    transition: 0.5s ease;
}
.team {
    font-size: 25px;
}
.name {
    font-size: 45px;
}
.map {
    font-size: 15px;
    margin-top: -70%;
}
.matchup:hover {
    -webkit-animation: matchup-active 1s infinite;
    -moz-animation: matchup-active 1s infinite;
    -ms-animation: matchup-active 1s infinite;
    -o-animation: matchup-active 1s infinite;
    animation: matchup-active 1s infinite;

    p {
        color: #ff6600;
    }
    img {
        opacity: 0.2;
    }
}

@-webkit-keyframes matchup-active {
    0% {
        border: 1px solid #ff6600;
    }
    50% {
        border: 1px solid transparent;
    }
    100% {
        border: 1px solid #ff6600;
    }
}
@-moz-keyframes matchup-active {
    0% {
        border: 1px solid #ff6600;
    }
    50% {
        border: 1px solid transparent;
    }
    100% {
        border: 1px solid #ff6600;
    }
}
@-o-keyframes matchup-active {
    0% {
        border: 1px solid #ff6600;
    }
    50% {
        border: 1px solid transparent;
    }
    100% {
        border: 1px solid #ff6600;
    }
}
@keyframes matchup-active {
    0% {
        border: 1px solid #ff6600;
    }
    50% {
        border: 1px solid transparent;
    }
    100% {
        border: 1px solid #ff6600;
    }
}

编辑: 用 jsfiddle 更新:http://jsfiddle.net/sicophrenic/qvJ94/ 它的样式不完美(即图像和东西没有居中),但我遇到的问题出现了(在 Chrome 中并且在 Firefox 中运行良好)。

【问题讨论】:

标签: css hover


【解决方案1】:

.matchup 添加color:transparent;

.matchup:hover 添加color: #ff6600;

.matchup p 添加color: inherit;

因为.matchup:hover p 不是有效的选择器。

here is a fiddle

【讨论】:

  • 这仅修复了文本问题,但我认为您可以对其他问题使用类似的技术。
  • 我刚刚注意到您发布的小提琴中的一些内容:“.matchup:hover img {}”是一个有效的选择器吗?您提到“.matchup:hover p {}”不是有效的选择器。
  • 另外,我之前在 Firefox 上按预期工作但在 chrome 上没有工作的具体原因是什么?
  • 我不知道为什么它可以在 Firefox 中工作,而且我很确定您永远无法使用 :pseudo tag {} 进行选择,但我可能是错的。也许 Firefox 可以让你这样做。
  • 我意识到我忘记提及的事情是,我之所以有类似.matchup:hover p {} 的原因是因为我使用的是 LESS,当我在这里发布它时,我使用在线转换器将它转换为 css跨度>
猜你喜欢
  • 2014-02-22
  • 2015-12-07
  • 2021-12-21
  • 2022-01-18
  • 1970-01-01
  • 2016-02-18
  • 1970-01-01
  • 2015-01-21
  • 1970-01-01
相关资源
最近更新 更多