我试图将标题的背景淡入到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);我们很感谢你的帮助
发布于 2013-05-15 15:16:16
您可以在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来切换类。
发布于 2013-05-15 15:22:52
仅仅为了动画背景颜色的不透明度,您实际上并不需要彩色动画插件,您可以为新浏览器使用CSS转换,但是如果您必须支持不支持CSS3的浏览器,则可以在jQuery的动画中使用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);https://stackoverflow.com/questions/16568722
复制相似问题