我正试图将标题的背景淡入white
但是当我尝试使用一些jQuery来设置背景颜色的淡入淡出时,它就不起作用了。
的jQuery
var windw = this;
$.fn.followTo = function ( pos ) {
var $this = this,
$window = $(windw);
$window.scroll(function(e){
if ($window.scrollTop() > pos) {
$this.animate({'backgroundColor':'rgba(255,255,255,1)'},400);
console.log($this.css('backgroundColor'));
console.log(pos);
} else if ($window.scrollTop() == 0) {
$this.animate({'backgroundColor':'rgba(255,255,255,0)'});
console.log($this.css('backgroundColor'));
console.log(pos + ' at the top');
} else {
$this.animate({'backgroundColor':'rgba(255,255,255,0)'});
console.log($this.css('backgroundColor'));
}
});
};
$('.home-header-main').followTo(86);
感谢帮助
答案 0 :(得分:2)
你可以在plugins的帮助下使用jQuery为rgba制作动画,但我会让CSS3使用CSS过渡处理所有这些。
示例:
body {
background-color: rgba(255,255,255,1);
-webkit-transition: background-color 0.4s linear;
-moz-transition: background-color 0.4s linear;
-o-transition: background-color 0.4s linear;
transition: background-color 0.4s linear;
}
.bg-faded {
background-color: rgba(255,255,255,0);
}
然后,您可以使用Javascript切换课程。
答案 1 :(得分:0)
为了动画背景颜色的不透明度,你真的不需要彩色动画插件,你可以为新的浏览器使用CSS过渡,但如果你必须支持不支持CSS3的浏览器,你可以使用jQuery的animate中的step()方法:
$.fn.followTo = function ( pos ) {
return this.each(function() {
var $this = $(this),
$window = $(window),
flag = true;
$this[0].x = 1;
$window.on('scroll', function(e){
if ($window.scrollTop() > pos && flag) {
flag = !flag
anim($this, 0);
}else if ($window.scrollTop() < pos && !flag){
flag = !flag
anim($this, 1);
}
});
});
function anim(elem, val) {
elem.stop(true, false).animate({
x : val
},
{
step: function(now, fx) {
$(fx.elem).css({backgroundColor : 'rgba(255,255,255,'+now+')'});
},
duration: 4000
});
}
};
$('.home-header-main').followTo(86);