javascript - 当用户位于带有动画导航的页面顶部时应用 css

标签 javascript jquery html css

我有当前代码可以在用户向上滚动时隐藏/显示我的导航。

$.fn.moveIt = function(){
  var $window = $(window);
  var instances = [];

 $(this).each(function(){
   instances.push(new moveItItem($(this)));
 });

window.onscroll = function(){
var scrollTop = $window.scrollTop();
instances.forEach(function(inst){
  inst.update(scrollTop);
});
}
}

var moveItItem = function(el){
  this.el = $(el);
  this.speed = parseInt(this.el.attr('data-scroll-speed'));
};

moveItItem.prototype.update = function(scrollTop){
 this.el.css('transform', 'translateY(' + -(scrollTop / this.speed) + 'px)');
 };

// Initialization
$(function(){
  $('[data-scroll-speed]').moveIt();
});

});

CSS

#s-nav {
position: fixed;
display: block;
z-index: 999;
width: 100%;
top: 0; left: 0;
}

#s-nav.nav-up { top: -80px; }

我想要包含的功能是在用户到达页面顶部(或非常接近页面顶部)时应用特定的 CSS。

基本上,当用户位于页面顶部时,我希望导航位于页面的下方,然后当用户滚动浏览页面时,让显示的代码将其“隐藏/显示”在页面顶部。

最佳答案

你可以这样使用:

$(document).scroll(function() { 
  //check if it is at the top of the page
   if($(window).scrollTop() === 0) {
     //do your code here or add css using jquery
   }
});

关于javascript - 当用户位于带有动画导航的页面顶部时应用 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44081372/

相关文章:

带有 jQ​​uery 的 Javascript 正则表达式包含正则表达式扩展

javascript - addEventListener 如何忽略图像并继续获取数字

html - 页脚从下往上滑动,点击不保持事件状态

javascript - 如何在页面刷新时保留之前的数据?

javascript - 使用 jQuery 时扩展 object.prototype 的替代方法

javascript - 如何提取文本区域内容的第一行

javascript - 在多步骤向导中提交之前获取所有输入值以供审核 - 如何?

javascript - RxJS:模拟 Angulars @Input() 绑定(bind)?

javascript - extjs 4 - 样式化 Ext.menu.Menu( float )

html - 如何防止打印::在 css 之后?