我有这个脚本在向下滚动120px时得到修复。效果很好。
但我还需要让我的侧边栏成为默认位置:绝对;保证金:0; 当向下滚动120px时,侧边栏类需要位置:固定;和margin-top:50px;我不认为这很难,但我不是编码员。有人能帮帮我吗?
这不起作用,因为它不会定位,但它不能定位,因为它不能动画?我还找不到任何解决方案..
$('.sidebar').animate({'position':'fixed'});
我的剧本:
$(document).ready(function(){
var startY = 120;
$(window).scroll(function(){
checkY();
});
function checkY(){
if($(window).scrollTop() > startY ){
$('.fixedDiv').slideDown(400, function(){});
}else{
$('.fixedDiv').slideUp(400, function(){});
}
}
checkY();
});
答案 0 :(得分:2)
您可能只想$(".sidebar").css("position","fixed")
来设置CSS。 position
不是可以设置动画的属性,因为尝试这样做是没有意义的。
答案 1 :(得分:0)
我们无法设置Dom的位置使用我们需要使用的动画 dom.css('property',value);
答案 2 :(得分:0)
最后我是这样做的,因为有一些响应的东西在继续:
$('.sidebar').addClass('gofix');
并添加了css:
<style>
.gofix{position:fixed; top:55px;}
@media (max-width: 480px) {
.gofix{position:relative; top:auto;}
}
</style>