简体   繁体   English

固定菜单,滚动时标题div消失

[英]Fixed menu with title div disappearing on scrolling

I wish to combine the scripts of this template (I'm revising) https://codepen.io/lydiawawa/pen/gEPpwX to carry the effect from this example: https://codepen.io/jamesbarnett/pen/JwFuy 我希望结合此模板的脚本(我正在修订) https://codepen.io/lydiawawa/pen/gEPpwX来体现此示例的效果: https : //codepen.io/jamesbarnett/pen/JwFuy

The javascript I need to revise is as of the following: 我需要修改的JavaScript如下:

/* JavaScript from: http://jqueryfordesigners.com/fixed- 
   floating-elements/ */

$(function () {
var sidebar = $('.sidebar');
var top = sidebar.offset().top - parseFloat(sidebar.css('margin-top'));

$(window).scroll(function (event) {
  var y = $(this).scrollTop();
  if (y >= top) {
    sidebar.addClass('fixed');
  } else {
    sidebar.removeClass('fixed');
  }
});
});

Could anyone help me to revise the script to achieve the effects? 谁能帮我修改脚本以达到效果?

Thank you! 谢谢!

You've already set the position of nav as fixed by default which won't give you the desired result. 默认情况下,您已经将nav的位置设置为fixed ,这不会给您想要的结果。 You should set its position as static by default, and change the value after the document has reached a certain point by scrolling. 默认情况下,您应该将其位置设置为static ,并在文档到达特定点后通过滚动更改该值。

As I mentioned earlier, that script in the actual OP is doing the right job. 正如我之前提到的,实际OP中的脚本执行正确的工作。 The only thing I've done is to copy/paste that script to your example ( and some tiny CSS changes ). 我唯一要做的就是将脚本复制/粘贴到您的示例中(以及一些小的CSS更改)。

check out this revised version at codePen 在codePen上查看此修订版

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

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