如果你知道如何使用的開發工具,那麼你可以在裏面http://riotindustries.com/
看到header
股利分配的fixedtop
類一旦用戶從上向下滾動的350px
。
你可以做到這一點使用jQuery
,我已經迅速創建了一個示例jsFiddle爲您檢查,雖然你將需要進一步修改樣式,根據您的需求,因爲它很簡單。在我的jsFiddle中發生的事情是,當您滾過250px
時,header
被分配了fixedtop
類。
HTML:
<header>This is a nice header, bro!</header>
<div id="longcontent"></div>
CSS:
header {
position: fixed;
background: gray;
color: black;
width: 500px;
text-align: center;
padding: 50px;
left: 50%;
transform: translate(-50%, 0);
top: 30px;
}
.fixedtop {
width: 100%;
background: black;
color: white;
padding: 50px 0px;
text-align: center;
box-shadow: 1px 1px 1px red;
top: 0px;
}
#longcontent {
height: 1000px;
}
的jQuery:
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 250) {
$("header").addClass("fixedtop");
} else {
$("header").removeClass("fixedtop");
}
});
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 250) {
$("header").addClass("fixedtop");
} else {
$("header").removeClass("fixedtop");
}
});
header {
position: fixed;
background: gray;
color: black;
width: 500px;
text-align: center;
padding: 50px;
left: 50%;
transform: translate(-50%, 0);
top: 30px;
}
.fixedtop {
width: 100%;
background: black;
color: white;
padding: 50px 0px;
text-align: center;
box-shadow: 1px 1px 1px red;
top: 0px;
}
#longcontent {
height: 1000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>This is a nice header, bro!</header>
<div id="longcontent"></div>
謝謝你downvote ...我希望你會需要一個像我一些關鍵詞..我真的想看看你會做什麼在這種情況下.. – m1clee 2014-10-18 23:07:21
使用滾動事件更改類 – charlietfl 2014-10-18 23:14:03
事件?像
...? – m1clee 2014-10-18 23:16:02