简体   繁体   English

根据滚动位置淡入和淡出某些DIV

[英]Fade in and out certain DIVs based on scroll position

The basic problem I'm having is that I'd like to fade in and out particular divs based on the user's current vertical location on the page. 我遇到的基本问题是,我想根据用户当前在页面上的垂直位置淡入和淡出特定的div。 Getting the 1st div to fade in when scrolling down is easy, as is getting it to fade out when I go back to the top of the page. 向下滚动时使第一个div淡入很容易,当我回到页面顶部时,使第一个div淡出也很容易。 I'm guessing I need to use come kind of greater than, less than scrollTop javascript to establish the limits of each. 我猜我需要使用来大于,小于scrollTop的javascript来确定每个限制。

Here's the javascript I've been using so far: 这是到目前为止我一直在使用的javascript:

if(scrollTop > 500) {
$("#Project-Desc, #Aqueous-Desc").fadeIn('slow');}
if(scrollTop < 500) {
$("#Project-Desc").fadeOut('slow');}

Here's my site to establish what exactly I'm trying to do: 这是我的网站,用于确定我要尝试执行的操作:

http://luke-keller.com/test2/ http://luke-keller.com/test2/

You'll see that the small gray box that fades in will contain project descriptions, and that's where I need to fade out old content and fade in new content based on the scroll position or the like. 您会看到淡入的小灰色框将包含项目描述,这是我需要根据滚动位置等淡出旧内容并淡入新内容的地方。

Thanks! 谢谢!

PS - Additional HTML PS-其他HTML

<div id="Portfolio">

    <div id="Port-Position">

        <div id="Aqueous" class="Projects">
            <div class="Project-Spacer">
            </div>
            <img src="Images/Projects/AqueousModel.jpg" width="600" height="400" border="0" alt=""/>
            <img src="Images/Projects/AqueousPlan.jpg" width="600" height="400" border="0" alt=""/>
            <img src="Images/Projects/AqueousModel.jpg" width="600" height="400" border="0" alt=""/>
            </div>

            <div class="Project-Divider">
            </div>

            <div id="Townhouse" class="Projects">
            <div class="Project-Spacer">
            </div>
            <img src="Images/Projects/townhousePlan.jpg" width="600" height="400" border="0" alt=""/>
            <img src="Images/Projects/townhouseModel.jpg" width="600" height="400" border="0" alt=""/>
            <img src="Images/Projects/townhousePath.jpg" width="600" height="400" border="0" alt=""/>
            </div>

      </div>
   </div>

        <div id="Project-Desc">

            <div id="Aqueous-Desc" class="Desc">
                <h2>AQUEOUS</h2>
                <p><h4>2006 - Temple University</h4></p>

                <div class="Desc-Body">
                Aqueous, meaning of water, is a blah blah blah more text here blah blah blah sweet awesome. Minecraft. Love that stuff.
                </div>

                <div class="Desc-Footer">
                Clay Prototype, Handrawn Plan on Vellum, Foamcore Model.
                </div>

             </div>

             <div id="Townhouse-Desc" class="Desc">
                <h2>TOWNHOUSE</h2>
                <p><h4>2006 - Temple University</h4></p>

                <div class="Desc-Body">
                Aqueous, meaning of water, is a blah blah blah more text here blah blah blah sweet awesome. Minecraft. Love that stuff.
                </div>

                <div class="Desc-Footer">
                Clay Prototype, Handrawn Plan on Vellum, Foamcore Model.
                </div>

             </div>

</div>

NOTE: The Project Description, the box I'm getting to fade in and out, is a fixed position element if that makes any difference. 注意:项目描述(我要逐渐淡入和淡出的框)是固定位置元素,如果有任何区别。

This was very helpful. 这非常有帮助。 I made a few modifications that I find help with easing the transitions between elements when the user scrolls quickly. 我进行了一些修改,这些内容有助于减轻用户快速滚动时元素之间的过渡。

Something like 就像是

     if( y > (600) && y < (1900) ){
        $("#Aqueous-Desc").fadeIn('slow');
     }else{
        $("Aqueous-Desc").fadeOut("slow");
     }

This binds the opacity content to the range set in the if statement so if you're ever out of bounds it will fade out. 这会将不透明度内容绑定到if语句中设置的范围,因此,如果您超出范围,它将消失。

I think I have the answer. 我想我有答案。 I welcome any and all who have any suggestions regarding this code, but essentially you need to find the scroll position of the window, and insert fade in and fade outs based on certain parameters (where the position is greater than x but less than y, fade in #mydiv). 我欢迎所有对此代码有任何建议的人,但是本质上,您需要找到窗口的滚动位置,并根据某些参数(其中位置大于x但小于y,淡入#mydiv)。 I coded a few below and have plenty more to go. 我在下面编写了一些代码,还有很多工作要做。

$(window).scroll(function(){
  // gets the position of the window
          var y = $(window).scrollTop();

            if( y < (600) ){
            $(".Desc").fadeOut('slow');}

          if( y > (600) && y < (1900) ){
            $("#Aqueous-Desc").fadeIn('slow');}
          if( y > (1900) ){
            $("#Aqueous-Desc").fadeOut('slow');}
          if( y < (600) ){
            $("#Aqueous-Desc").fadeOut('slow');}


            if( y > (2100) && y < (3300) ){
                $("#Townhouse-Desc").fadeIn('slow');}
            if( y > (3300) ){
                $("#Townhouse-Desc").fadeOut('slow');}
            if( y < (2100) ){
                $("#Townhouse-Desc").fadeOut('slow');}


            if( y > (3500) && y < (4700) ){
                $("#Poley-Desc").fadeIn('slow');}
            if( y > (4700) ){
                $("#Poley-Desc").fadeOut('slow');}
            if( y < (3500) ){
                $("#Poley-Desc").fadeOut('slow');}

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

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