简体   繁体   English

在Scroll上为scrollTop设置动画以向上移动div并将其修复

[英]on Scroll animate scrollTop to move div up and fix it

I am trying to animate a header div (height: 100px;) to move to the top of the screen when the user scrolls down. 我正在尝试设置标题div的动画(高度:100px;),以便当用户向下滚动时移动到屏幕顶部。 when it's at the top I'd like to fix it there. 当它在顶部时,我想在那里修复它。 As the div moves to the top of the screen it passes over a banner div (height: 400px;), I have made the banner div fade out depending on how close the header div is to the top. 当div移至屏幕顶部时,它越过了横幅div (高度:400像素;),我根据标题div与顶部的接近程度使横幅div淡出。

When the header div is fixed to the top and the banner div fully faded out I'd like an upward scroll to re-animate the header div to move back to it's original position (margin-top: 400;) and the banner div to fade back in. 标题div固定在顶部并且横幅div完全消失时,我想向上滚动以重新设置标题div的动画,以移回其原始位置(margin-top:400;),并将横幅div移至淡入。

I have created a JSFiddle of my code: https://jsfiddle.net/xm33Laag/ 我已经创建了我的代码的JSFiddle: https ://jsfiddle.net/xm33Laag/

see JSfiddle

Although it's not working like my local version :S 虽然它不像我的本地版本那样工作:S

I can get the header div to go to the top but I can't get it to come back down. 我可以将标头div移到顶部,但无法将其调低。 I also want my animation to look like this example : http://alvarotrigo.com/fullPage/#firstPage 我也希望我的动画看起来像这个例子: http : //alvarotrigo.com/fullPage/#firstPage

I don't want to simply use fullPage.js because I only want it for this function. 我不想简单地使用fullPage.js,因为我只希望它用于此功能。 I don't need the myriad options that come with including it. 我不需要包含它的众多选择。

with the above ideal example, you can see that a single scroll will initiate the animation, and there's no initial jerking before the animation starts. 在上面的理想示例中,您可以看到单个滚动将启动动画,并且在动画开始之前没有初始的抽动。 I really like this! 我真的很喜欢这个!

My animation will initiate after you scroll, meaning that the initial scroll makes the viewer view 100px of snappy scrolling, then the animation. 我的动画将在您滚动后启动,这意味着初始滚动使查看者观看100px的快速滚动,然后是动画。 I'd like it to be one. 我希望成为一个。

Thanks! 谢谢!

So I had a go at what I get as the gist of it. 因此,我尽力而为。 Slightly changed the CSS, giving the main content below the top margin instead of .header . 稍微更改了CSS,使主要内容位于顶部边缘之下,而不是.header It was a bit easier to code it that way. 这样编码起来要容易一些。

Here's the live example : http://codepen.io/anon/pen/azrwog?editors=001 这是实时示例: http : //codepen.io/anon/pen/azrwog?editors=001

And the JavaScript (changed that a lot) : 和JavaScript(变化很大):

var previous = 0, delta, initial = true, stopped;

$(window).scroll(function() {

clearTimeout(stopped);
var fromtop = $(window).scrollTop();
if (fromtop-previous > 0) delta = -1;
else delta = 1;
previous = fromtop;

if (fromtop < 400) {
var factor = 400-fromtop;
$('.banner').css({'height': factor, 'opacity': factor/400});
$('.header').removeClass('fixed');
if (delta == -1 && initial) firstScroll();
else if (delta == 1) scrollAction();
}
else $('.header').addClass('fixed');
});

function firstScroll() {
initial = false;
$('html, body').animate({scrollTop: 400}, 800);
}

function scrollAction() {stopped = setTimeout(function() {goTop()}, 200)}

function goTop() {
var topoffset = $(window).scrollTop();
$('html, body').animate({scrollTop: 0}, 2*topoffset, function() {
initial = true;
});
}

Another solution update - cross browser smooth, using the mousewheel.js plugin : 另一个解决方案更新-使用mousewheel.js插件可使浏览器流畅运行:

http://codepen.io/anon/pen/jEoGxG?editors=001 http://codepen.io/anon/pen/jEoGxG?editors=001

var fromtop, initial = true, stopped;

$(window).mousewheel(function(turn, delta) {

if (!initial) $('html, body').finish();

$(window).scroll(function() {

clearTimeout(stopped);
fromtop = $(window).scrollTop();

if (fromtop <= 400) {
changeOpacity();
$('.header').removeClass('fixed');
if (delta == 1) scrollAction();
}
else $('.header').addClass('fixed');
});

if (initial) {
if (delta == -1) $('html, body').animate({scrollTop: 400}, 800, function() {
initial = false;
});
return false;
}
});

function scrollAction() {stopped = setTimeout(function() {goTop()}, 200)}

function changeOpacity() {
var factor = 400-fromtop;
$('.banner').css({'height': factor, 'opacity': factor/400});
}

function goTop() {
var topoffset = $(window).scrollTop();
$('html, body').animate({scrollTop: 0}, 2*topoffset, function() {
initial = true;
});
}

Update - one line of code added to prevent 'sticking' after the initial scroll : 更新-添加了一行代码以防止在初始滚动后出现“粘连”:

if (!initial) $('html, body').finish();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM