我正在尝试写一些小js。我的导航将渐变设置为背景。 当我向下滚动一定量时,我希望背景在高度上变化,从渐变变为平面颜色,0.85不透明度(动画)。
这是我的js代码
$(window).scroll(function(){
if ($(window).scrollTop() >= 600){
$('#navigation').css({height: '92px'});
} else {
$('#navigation').css({height: '142px'});
}
});
这是css
#navigation {
height: 142px;
width: 1350px;
position: fixed;
z-index: 2000;
background: -moz-linear-gradient(top, rgba(0,0,0,0.75) 0%, rgba(0,0,0,0) 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.75)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(0,0,0,0.75) 0%,rgba(0,0,0,0) 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(0,0,0,0.75) 0%,rgba(0,0,0,0) 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(0,0,0,0.75) 0%,rgba(0,0,0,0) 100%); /* IE10+ */
background: linear-gradient(to bottom, rgba(0,0,0,0.75) 0%,rgba(0,0,0,0) 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#bf000000', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */
}
希望有人可以帮助我答案 0 :(得分:2)
你的代码很好!!
只需创建两个单独的班级,然后在“其他”中将$('#navigation').css({height: '92px'});
替换为$('#navigation').addClass("flatColored");
和$('#navigation').removeClass("flatColored");
。
而不是在您的CSS中使用#navigation{ ...}
,请使用.gradientColored{...} .flatcolored{...}
并在您的html中:<div id="navigation" class="gradientColored">...</div>
覆盖'flatColored类中的background
属性。
根据您的代码在Chrome上测试。工作正常!