简体   繁体   English

jQuery:动画向下导航,无法向上滑动/正常工作

[英]jquery: animate down navigation, not sliding back up/working properly

Hey so i was trying to get my navigation to animate down after a certain div has passed but its not working properly. 嗨,所以我试图在某个div通过后使导航动画降下来,但无法正常工作。 not sure why. 不知道为什么。 it works when sliding down although a bit buggy(sometimes there seems to be a delay before it slides down and other times it slides down properly immediately). 它在滑落时仍然有效,尽管有一些小问题(有时滑下来之前会有延迟,而其他时候立即立即滑下来)。 It also does not slide up it justs removes it self. 它也不会向上滑动,只是将其自身移除。 what am i doing wrong? 我究竟做错了什么?

here is my code: 这是我的代码:

$(window).scroll(function () {
    targetScroll = $('#scroll_verder').position().top,
    currentScroll = $('html').scrollTop() || $('body').scrollTop();
if(currentScroll>=targetScroll){
    $('.navbar').addClass('show-menu').animate({ top: '0px' });
}

else {
    $('.navbar').stop();
    $('.navbar').removeClass('show-menu');
    $('.navbar').animate({ top: '-50px' });
}
});

Had a look at your code on the link you posted. 在您发布的链接上查看了您的代码。 This should do the trick: 这应该可以解决问题:

var reachedTarget = false; // Prevent animation collisions with this
var targetScroll = $('#scroll_verder').position().top;
$(window).scroll(function () {
  var currentScroll = $('html').scrollTop() || $('body').scrollTop();
  if ( currentScroll >= targetScroll ) {
    if ( !reachedTarget ) {
      $('.navbar').stop();
      $('.navbar').addClass('show-menu').animate({ top: '0px' });
    }
    reachedTarget = true;
  } else{
    if ( reachedTarget ) {
      $('.navbar').stop();
      $('.navbar').removeClass('show-menu').animate({ top: '-50px' });
    }
    reachedTarget = false;
  }
});

EDIT: In CSS (to make sure initial position is correct): 编辑:在CSS中(以确保初始位置正确):

.navbar.show-menu {
  z-index: 999;
  display: block;
  top : -50px;
}

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

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