javascript - 将 fadeIn 添加到我的导航栏时出现问题

标签 javascript jquery html css

我正在尝试向我的导航栏添加淡入,因此当我到达某个滚动点时,导航栏会淡入。但是,我的尝试失败了。我尝试将 Jquery 添加到常规 javascript,所以我不确定这是问题所在还是问题所在。我希望导航栏仅在到达导航栏再次出现的页面下方的滚动点时淡入。

这可以在以下位置查看:

http://realtorcatch.com/test_index

我的 Javascript 是:

window.onscroll = function() {
var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
if (scrollTop >= document.getElementById("d").offsetTop) {
  document.getElementById("header").style.position = "fixed";
  document.getElementById("d").style.marginTop = "50px";
  document.getElementById("header").style.top = "0";
} else {
  $(function() { // $(document).ready shorthand
    $('#header').fadeIn('slow');
  });
document.getElementById("header").style.position = "static";
    document.getElementById("d").style.marginTop = "0px";
    document.getElementById("header").style.marginTop = "0px";
  }
}

然后我有以下 div,其中包含我的所有代码:

<div id="header">
</div>

最佳答案

首先,使用.hide() 隐藏 header ,然后调用.fadeIn() 时自动移除display: none将不透明度淡化为 100%。

$(function() { // $(document).ready shorthand
    window.onscroll = function() {
      var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;

      if (scrollTop >= document.getElementById("d").offsetTop) {
        if (!$('#header').hasClass('header-fixed')) {
          $('#header').addClass('header-fixed');
          $('#header').hide().fadeIn();
          document.getElementById("header").style.position = "fixed";
          document.getElementById("d").style.marginTop = "50px";
          document.getElementById("header").style.top = "0";
        }
      } else {
        document.getElementById("header").style.position = "static";
        document.getElementById("d").style.marginTop = "0px";
        document.getElementById("header").style.marginTop = "0px";
        $('#header').removeClass('header-fixed');
      }
    }
});

https://jsfiddle.net/jonathanzuniga/ogs9cem7/

关于javascript - 将 fadeIn 添加到我的导航栏时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34496440/

相关文章:

javascript - 创建一个动画序列循环 jquery

javascript - 可编辑的 Div 插入符位置

html - 在 Excel 中打开文件时,使用 XSL 创建的空 html 列太宽

javascript - EMBER直接路由URL访问不加载数据

javascript - 要在空格键上播放的音频,请单击

javascript - 从其他 .html 文档加载内容

javascript - Facebook Like "total likes"-回调?

javascript - 更改网页上的光标颜色?

javascript - 在今天改变 UL 背景

javascript - 如何停止javascript中的函数?