简体   繁体   English

内容根据滚动位置淡出

[英]Content fade based on scroll position

I have the below function set in place to fade out/in certain content based on its proximity to the top/bottom of the page. 我已经设置了以下功能,根据其与页面顶部/底部的接近程度淡出/显示某些内容。 The desired effect is pretty visible and obvious if you click on the ABOUT link on the top, but if you use the WORK button to scroll down the content just pops in at the right position but the fading does not occur as it does with the top. 如果单击顶部的ABOUT链接,则所需的效果非常明显且显而易见,但是,如果使用WORK按钮向下滚动,则内容会在正确的位置弹出,但不会像顶部那样出现褪色。

I'm sure it has something to do with the scrollBottom equivalent I've created but not sure how to go about fixing this 我确定它与我创建的等效的scrollBottom有关,但不确定如何解决此问题

http://coreytegeler.com/new http://coreytegeler.com/new

 $(window).load(function(){
        $(window).scroll(function() {
        var st = $(window).scrollTop();
        var sb = $(document).height() - $(window).height() - $(window).scrollTop();
        $('#about .content').css({'opacity' : (1 - st/450)});
        $('#work .content').css({'opacity' : (450 + sb*-1)});

        $('#home .content').css({'opacity' : (-(450 + sb*-1))});
        $('#home .content').css({'opacity' : (-(1 - st/450))});
        });
    });

In my browser, your page loaded with a document height of 2892 and a window height of 952. Plugging those dimensions into your equations gives the opacity of #work varying from -1478 to 450. This has it starting to display when the window is 450px from the bottom, fully fading in when the window is 451px from the bottom - probably a little faster than you intended. 在我的浏览器中,页面加载的文档高度为2892,窗口高度为952。将这些尺寸插入方程式中后,#work的不透明度从-1478到450不等。当窗口为450px时,它开始显示从底部开始,当窗口距离底部451px时完全消失-可能比您预期的快一点。

If you change the work line to 如果您将工作线更改为

$('#work .content').css({'opacity' : ((450 + sb*-1)/450)});

then your opacity will vary from -3.28 to 1, crossing 0 and starting to fade in when the window has scrolled to 450px from the bottom, reaching 1.00 (100% fade in) when the window has scrolled all the way down. 那么您的不透明度将在-3.28到1之间变化,越过0并在窗口从底部滚动到450px时开始淡入,而在窗口一直向下滚动时达到1.00(淡入100%)。

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

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