我正在尝试:CSS change color on scroll / cut text - overflow z-index 当有人滚动时,使用剪辑来改变按钮的颜色。我希望在从标题移动到正文时进行更改,但它对我不起作用。
body {
background:#F7FE2E;
margin: 0;
}
#header{
padding:200px 50px;
background-color: #6495ED;
}
/*HERE IS THE HEADER AND BODY CLIP*/
#header,
#content {
clip: rect(auto, auto, auto, auto);
box-sizing: border-box;
}
/********** MENU ***********/
.menu{
top: 15px;
right: 40px;
font-size: 16px;
position:fixed;
}
.black {
color: #000000;
}
.white {
color: #FFFFFF;
}
<div id="wrapper">
<div id="header">
<!--HERE IS THE FIXED MENU WHITE-->
<div class="menu white">MENU</div>
</div>
<div id="content">
<!--HERE IS THE FIXED MENU BLACK-->
<div class="menu black">MENU</div>
CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>
</div>
</div>
为什么它不起作用?
答案 0 :(得分:3)
这里有你需要的答案,你错过了一些规则。 请查看下面的代码段以获取评论......
body {
background: some-arbitrary-color; /* clipping doesn't use THIS color => */
margin: 0;
}
#header {
top: 0; /* [OPTIONAL] add */
padding: 200px 50px;
background-color: #6495ED;
}
/* [MANDATORY] add */
#content {
top: 400px; /* (2 x 200px) px, vw, %, whatever works best*/
background-color: #F7FE2E; /* => but NEEDS this bg-color to mix */
}
/*******************/
/*HERE IS THE HEADER AND BODY CLIP*/
#header,
#content {
position: absolute; /* [MANDATORY] add, clipping needs absolute positioning */
width: 100%; /* [MANDATORY] add, and position needs width */
clip: rect(auto, auto, auto, auto);
box-sizing: border-box;
}
/********** MENU ***********/
.menu {
top: 15px;
right: 40px;
font-size: 16px;
position: fixed;
}
.black {
color: #000000;
}
.white {
color: #FFFFFF;
}
&#13;
<div id="header">
<!--HERE IS THE FIXED MENU WHITE-->
<div class="menu white">MENU</div>
</div>
<div id="content">
<!--HERE IS THE FIXED MENU BLACK-->
<div class="menu black">MENU</div>
CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>CONTENT
<br>
</div>
</div>
&#13;
答案 1 :(得分:0)
您应该使用 jquery 来更改滚动菜单的颜色,如下所示:
$(window).scroll(function() {
if ($(this).scrollTop() > 380) {
$(".menu").removeClass("white").addClass("black");
} else {
$(".menu").removeClass("black").addClass("white");
}
});
&#13;
body {
background: #F7FE2E;
margin: 0;
}
#header {
padding: 200px 50px;
background-color: #6495ED;
}
/*HERE IS THE HEADER AND BODY CLIP*/
#header,
#content {
clip: rect(auto, auto, auto, auto);
box-sizing: border-box;
}
/********** MENU ***********/
.menu {
top: 15px;
right: 40px;
font-size: 16px;
position: fixed;
}
.black {
color: #000000;
}
.white {
color: #FFFFFF;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">
<div id="header">
<!--HERE IS THE FIXED MENU WHITE-->
<div class="menu white">MENU</div>
</div>
<div id="content">
CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>CONTENT<br>
</div>
</div>
&#13;