这里有些棘手的情况,我有以下课程:
<li><a href="test.html" class="hvr-overline-from-left">Test</a></li>
这是它的CSS:
.hvr-overline-from-left {
display: inline-block;
vertical-align: middle;
box-shadow: 0 0 1px transparent;
position: relative;
overflow: hidden;
}
.hvr-overline-from-left:before {
content: "";
position: absolute;
z-index: -1;
left: 0;
right: 100%;
top: 0%;
background: #47953F;
height: 4px;
-webkit-transition-property: right;
transition-property: right;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-timing-function: ease-out;
transition-timing-function: ease-out;
}
向下滚动时应更改以下内容:
.hvr-overline-from-leftsmall:before {
content: "";
position: absolute;
z-index: -1;
left: 0;
right: 100%;
top: 15%;
background: #47953F;
height: 4px;
-webkit-transition-property: right;
transition-property: right;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-timing-function: ease-out;
transition-timing-function: ease-out;
}
这是我尝试的:
$(document).ready(function($){
var line = $('.hvr-overline-from-left:before');
$(window).scroll(function () {
if ($(this).scrollTop() > 13.35) {
line.addClass("hvr-overline-from-leftsmall:before");
} else if($(this).scrollTop() >= 0) {
line.removeClass("hvr-overline-from-leftsmall:before");
}
});
});
当然这是行不通的,我的问题是如何使此工作正常进行?我不知道应该更改哪个类来更改伪元素。
最佳答案
您只需添加类hvr-overline-from-leftsmall
,而无需:before
选择器;例如。:
line.addClass("hvr-overline-from-leftsmall");
// No :before here -----------------------^
(当然,以后的
removeClass
也一样。)只要是在
hvr-overline-from-left
类规则之后定义的,它将在所有样式冲突中优先。:before
是规则选择器而不是类的一部分。