当前位置: 首页 > 面试题库 >

在800px之后在scrollDown上显示div

师增
2023-03-14
问题内容

当我从页面顶部向下滚动800px后,我想显示一个隐藏的div。到目前为止,我已经有了这个示例,但是我想它需要修改才能达到我想要的目的。

编辑:

[并且当scrollUp且高度小于800px时,此div应该隐藏]

HTML:

<div class="bottomMenu">
  <!-- content -->
</div>

CSS:

.bottomMenu {
    width: 100%;
    height: 60px;
    border-top: 1px solid #000;
    position: fixed;
    bottom: 0px;
    z-index: 100;
    opacity: 0;
}

jQuery的:

$(document).ready(function() {
    $(window).scroll( function(){
        $('.bottomMenu').each( function(i){
            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();
            if( bottom_of_window > bottom_of_object ){
                $(this).animate({'opacity':'1'},500);
            }
        }); 
    });
});

问题答案:

如果要在滚动多个像素后显示div,请执行以下操作:

$(document).scroll(function() {
  var y = $(this).scrollTop();
  if (y > 800) {
    $('.bottomMenu').fadeIn();
  } else {
    $('.bottomMenu').fadeOut();
  }
});



$(document).scroll(function() {

  var y = $(this).scrollTop();

  if (y > 800) {

    $('.bottomMenu').fadeIn();

  } else {

    $('.bottomMenu').fadeOut();

  }

});


body {

  height: 1600px;

}

.bottomMenu {

  display: none;

  position: fixed;

  bottom: 0;

  width: 100%;

  height: 60px;

  border-top: 1px solid #000;

  background: red;

  z-index: 1;

}


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Scroll down... </p>

<div class="bottomMenu"></div>

它简单但有效。

如果要在滚动多个像素后显示div,

没有jQuery:

myID = document.getElementById("myID");

var myScrollFunc = function() {
  var y = window.scrollY;
  if (y >= 800) {
    myID.className = "bottomMenu show"
  } else {
    myID.className = "bottomMenu hide"
  }
};

window.addEventListener("scroll", myScrollFunc);



myID = document.getElementById("myID");



var myScrollFunc = function() {

  var y = window.scrollY;

  if (y >= 800) {

    myID.className = "bottomMenu show"

  } else {

    myID.className = "bottomMenu hide"

  }

};



window.addEventListener("scroll", myScrollFunc);


body {

  height: 2000px;

}

.bottomMenu {

  position: fixed;

  bottom: 0;

  width: 100%;

  height: 60px;

  border-top: 1px solid #000;

  background: red;

  z-index: 1;

  transition: all 1s;

}

.hide {

  opacity: 0;

  left: -100%;

}

.show {

  opacity: 1;

  left: 0;

}


<div id="myID" class="bottomMenu hide"></div>

.scrollY 文档.className 文档.addEventListener文档

如果要在滚动到元素后显示它:

工作实例

$('h1').each(function () {
    var y = $(document).scrollTop();
    var t = $(this).parent().offset().top;
    if (y > t) {
        $(this).fadeIn();
    } else {
        $(this).fadeOut();
    }
});



$(document).scroll(function() {

  //Show element after user scrolls 800px

  var y = $(this).scrollTop();

  if (y > 800) {

    $('.bottomMenu').fadeIn();

  } else {

    $('.bottomMenu').fadeOut();

  }



  // Show element after user scrolls past

  // the top edge of its parent

  $('h1').each(function() {

    var t = $(this).parent().offset().top;

    if (y > t) {

      $(this).fadeIn();

    } else {

      $(this).fadeOut();

    }

  });

});


body {

  height: 1600px;

}

.bottomMenu {

  display: none;

  position: fixed;

  bottom: 0;

  width: 100%;

  height: 60px;

  border-top: 1px solid #000;

  background: red;

  z-index: 1;

}

.scrollPast {

  width: 100%;

  height: 150px;

  background: blue;

  position: relative;

  top: 50px;

  margin: 20px 0;

}

h1 {

  display: none;

  position: absolute;

  bottom: 0;

}


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Scroll Down...</p>

<div class="scrollPast">

  <h1>I fade in when you scroll to my parent</h1>



</div>

<div class="scrollPast">

  <h1>I fade in when you scroll to my parent</h1>



</div>

<div class="scrollPast">

  <h1>I fade in when you scroll to my parent</h1>



</div>

<div class="bottomMenu">I fade in when you scroll past 800px</div>

请注意,您无法获取设置为display: none;的元素的偏移量,而是获取元素父元素的偏移量。

如果要在每次滚动时导航或div导航或停靠在页面顶部,并在向上滚动时取消导航/取消导航,请停靠:

工作实例

$(document).scroll(function () {
    //stick nav to top of page
    var y = $(this).scrollTop();
    var navWrap = $('#navWrap').offset().top;
    if (y > navWrap) {
        $('nav').addClass('sticky');
    } else {
        $('nav').removeClass('sticky');
    }
});

#navWrap {
    height:70px
}
nav {
    height: 70px;
    background:gray;
}
.sticky {
    position: fixed;
    top:0;
}



$(document).scroll(function () {

    //stick nav to top of page

    var y = $(this).scrollTop();

    var navWrap = $('#navWrap').offset().top;

    if (y > navWrap) {

        $('nav').addClass('sticky');

    } else {

        $('nav').removeClass('sticky');

    }

});


body {

    height:1600px;

    margin:0;

}

#navWrap {

    height:70px

}

nav {

    height: 70px;

    background:gray;

}

.sticky {

    position: fixed;

    top:0;

}

h1 {

    margin: 0;

}


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit, morbo vel maleficia? De apocalypsi gorger omero undead survivor dictum mauris. Hi mindless mortuis soulless creaturas,

  imo evil stalking monstra adventus resi dentevil vultus comedat cerebella viventium. Qui animated corpse, cricket bat max brucks terribilem incessu zomby. The voodoo sacerdos flesh eater, suscitat mortuos comedere carnem virus. Zonbi tattered for solum

  oculi eorum defunctis go lum cerebro. Nescio brains an Undead zombies. Sicut malus putrid voodoo horror. Nigh tofth eliv ingdead.</p>

<div id="navWrap">

  <nav>

    <h1>I stick to the top when you scroll down and unstick when you scroll up to my original position</h1>



  </nav>

</div>

<p>Zombie ipsum reversus ab viral inferno, nam rick grimes malum cerebro. De carne lumbering animata corpora quaeritis. Summus brains sit, morbo vel maleficia? De apocalypsi gorger omero undead survivor dictum mauris. Hi mindless mortuis soulless creaturas,

  imo evil stalking monstra adventus resi dentevil vultus comedat cerebella viventium. Qui animated corpse, cricket bat max brucks terribilem incessu zomby. The voodoo sacerdos flesh eater, suscitat mortuos comedere carnem virus. Zonbi tattered for solum

  oculi eorum defunctis go lum cerebro. Nescio brains an Undead zombies. Sicut malus putrid voodoo horror. Nigh tofth eliv ingdead.</p>


 类似资料:
  • 问题内容: 我有一个位于JButton顶部的JLabel,但它不会显示在顶部。注释掉JButton的代码后,将显示JLabel,这意味着它在其中,但在底部。有没有办法在JButton的顶部显示JLabel? 任何帮助都是极好的。谢谢! 编辑: 为了澄清起见,我需要在游戏中使用该按钮,单击该按钮时,按钮上方会显示一个JLabel,以显示添加的“得分”。在我的游戏中,JLabel将比JButton小,

  • 我正在尝试在上显示一个以将上一页/活动移动到主页面(第一次打开)。我做不到。 我的代码。 代码在中。

  • 我正在从应用程序中的迁移到。但我不知道如何在

  • 问题内容: 我正在寻找一种将 javadocs 从我的开源项目(在 Eclipse中 生成)转换为 GitHub MarkDown的方法 ,或者想出一些其他简单的解决方案在 GitHub上 显示我的文档(避开简单地添加目录)。有一个简单的解决方案吗?我可以简单地将 GitHub 指向我的目录吗?还有更优雅的东西吗?我一直在 Google 上 大放异彩 。 问题答案: 我认为用MarkDown制作一

  • 我有一个列表活动,其中用户可以执行打开操作模式和。用户还可以打开操作模式,然后在其顶部打开。 动作模式实际上是搜索动作模式,用户可以从中选择打开动作模式的结果(如果这有助于理解的话)。

  • 我在我的列表视图中添加了一个可绘制的分隔线,但它出现在除最后一项之外的所有项目中。 我在列表中只有5个项目,所以在最后一个项目之后都是空白,我想在最后一项之后也有一个分隔符 我试过这个: 在我的列表视图中。但它不起作用 非常感谢。