我的标题设置为 920px 居中的宽度,并且使用标题下方的代码变得固定并在用户滚动时改变不透明度。我很容易达到了我想要的效果,尽管变化发生在瞬间。我想知道我是否可以使更改逐渐消失?我试过使用 CSS3 过渡但没有运气。
<script>
$(function() {
// grab the initial top offset of the navigation
var sticky_navigation_offset_top = $('header').offset().top;
// our function that decides weather the navigation bar should have "fixed" css position or not.
var sticky_navigation = function(){
var scroll_top = $(window).scrollTop(); // our current vertical position from the top
// if we've scrolled more than the navigation, change its position to fixed to stick to top,
// otherwise change it back to relative
if (scroll_top > sticky_navigation_offset_top) {
$('header').css({ 'position': 'fixed', 'position': 'fixed', 'opacity':0.8, 'top':0, 'left':0 });
} else {
$('header').css({ 'position': 'relative' });
}
};
// run our function on load
sticky_navigation();
// and run it again every time you scroll
$(window).scroll(function() {
sticky_navigation();
});
});
</script>
谢谢。