简体   繁体   English

使用JavaScript / jQuery向下滚动效果

[英]Slide scroll down effect with JavaScript / jQuery

I'm looking for some guidance on how I can recreate this feature. 我正在寻找有关如何重新创建此功能的指导。 As you can see if you scroll down the opacity changes and the title fades away as the div below comes up. 如您所见,如果您向下滚动,则不透明度会发生变化,并且标题会随着以下div的出现而消失。 Any ideas or tutorials which can help me? 有什么想法或教程可以帮助我吗? http://davegamache.com/ http://davegamache.com/

Till now I have tried only the $(window).scroll(function(){…}); 到目前为止,我只尝试了$(window).scroll(function(){…}); where I can scroll down to a certain trigger and pop up a small div for example. 例如,我可以向下滚动到某个触发器并弹出一个小div。 I guess I have to play also with the opacity now. 我想我现在也必须玩不透明度。 Any help please? 有什么帮助吗?

You have the right idea using $(window).scroll(function(){…}); 您可以使用$(window).scroll(function(){…});正确的主意$(window).scroll(function(){…});

You'll want to figure out the Y-Coordinate at which you want the div to be invisible and calculate the opacity of the div from that. 您将要找出要使div不可见的Y坐标,并从中计算div的不透明度。 Most of the time, I'd imagine this Maximum Y-coordinate should be the same as the height of the effected div. 大多数时候,我会想像这个最大Y坐标应该与受影响的div的高度相同。 Lets say our div is 500px in height. 假设我们的div高度为500px。 If the div should be at 0-opacity at Y-coordinate 500, then at y-coordinate 100 the opacity should be 20% (or .2) 如果div在Y坐标500处应为0不透明度,则在y坐标100处应为20%(或.2)

Working Sample: http://jsfiddle.net/FzNrG/5/ 工作示例: http : //jsfiddle.net/FzNrG/5/

$(window).scroll(function(){
    var opacity = 1- ( $(window).scrollTop() / $('#theDiv').height());
    if (opacity>1) opacity=1;
    if (opacity<0) opacity=0;

    //$('#debug').html('ScrollTop:' + $(window).scrollTop() + '<br>Opacity: ' + opacity);
    $('#theDiv').stop().fadeTo(250, opacity);    
});
$(window).scroll(function(){…});

is a good start... it fires whatever you want when you start a scroll. 是个不错的开始……启动滚动时,它会触发任何您想要的内容。 for example just put some .height(); 例如只是放一些.height(); in it to read elements height and if its below or higher of some number you want start .animate(); 在其中读取元素的高度,如果它低于或高于某个数字,则要启动.animate();。 like the opacity... just play around a bit. 就像不透明...只是玩了一下。

would suggest to read the source code of the linked page: (it's well commented too) 建议阅读链接页面的源代码:(也有很好的注释)

function slidingTitle() {
    //Get scroll position of window
    windowScroll = $(this).scrollTop();

    //Slow scroll of .art-header-inner scroll and fade it out
    $artHeaderInner.css({
       'margin-top' : -(windowScroll/3)+"px",
       'opacity' : 1-(windowScroll/550)
    });

    //Slowly parallax the background of .art-header
    $artHeader.css({
       'background-position' : 'center ' + (-windowScroll/8)+"px"
    });

   //Fade the .nav out
   $nav.css({
      'opacity' : 1-(windowScroll/400)
   });
}

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

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