jquery - 侧面导航栏没有响应

标签 jquery html css twitter-bootstrap

我想制作一个完全响应式的可滚动 Bootstrap 侧导航。 我在不同的链接和网站中搜索了各种可能的方法,但没有实现我想要的。

所以这是我的问题: 我想要一个在每个媒体屏幕上都能完全响应的侧面导航。它在较大的屏幕中表现正常,但在小屏幕中,我设置到侧面导航的偏移量不匹配,并且我认为不可能通过 jquery 在每个媒体屏幕中添加偏移量。那么,请问有人可以帮我解决这个问题吗?下面是我创建的 JSFiddle 的链接,这是我正在使用的 jquery:

$(document).ready(function() {
  $("#myNav").affix({
    offset: {
      top: 1100,
      bottom: ($('.other').outerHeight(true)) + 445
    }
  });

  $("#myNav").on('affixed.bs.affix');
});

$('#myNav a[href*=#]:not([href=#])').click(function() {
  if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
    var target = $(this.hash);
    target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
    if (target.length) {
      $('html,body').animate({
        scrollTop: target.offset().top - 50
      }, 1000);
      return false;
    }
  }
});

https://jsfiddle.net/sheela_paudel/yncauzo7/1/

预先感谢您的帮助。那意义重大。我还想问我们是否可以以百分比而不是像素的形式给出偏移值。

最佳答案

我在 JS 中做了一些更改,通过使用类或 ID 设置顶部和底部的偏移量

$(function(){
    var stickyElement   = '#myScrollspy',  
     bottomElement   = '.other'; 
     if($( stickyElement ).length){
     $( stickyElement ).each(function(){
     var fromTop = $( this ).offset().top, 
     fromBottom = $( document ).height()-($( this ).offset().top + $( this ).outerHeight()),
     stopOn = $( document ).height()-( $( bottomElement ).offset().top)+($( this ).outerHeight() - $( this ).height()); 
      if( (fromBottom-stopOn) > 200 ){
      $( this ).css('width', $( this ).width()).css('top', 0).css('position', '');
      $( this ).affix({
      offset: { 
       bottom: stopOn
       }
        }).on('affix.bs.affix', function(){ $( this ).css('top', 0).css('position', ''); });
           }
         $( window ).trigger('scroll'); 
       }); 
       }
      });

您可以使用 Jquery 克服它

you can see it in this link

var StickyElement = '#myScrollspy', 底部元素 = '.other';

只需在这两行中给出您想要的类名或 ID,即可获得顶部和底部的偏移量。

希望这对您有帮助。

关于jquery - 侧面导航栏没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789459/

相关文章:

javascript - 在 javascript/jquery 中转义 & 号

javascript - 如何在已初始化 select2 的输入框上设置默认值?

javascript - 单击事件功能未从 Fancybox 灯箱内部触发

javascript - 必须列在 web_accessible_resources list 键中,以便由扩展之外的页面加载。

jquery - 禁用 materialize.css 模态页脚按钮

html - Flexbox 垂直对齐包装器中的特定内容?

javascript - 从函数返回更新变量

html - 如何覆盖父级的 css 属性并将其应用于特定的子元素属性?

html - 如何更改所选选项中的字体颜色?

css - 类似于 Firefox 的 Style Editor for Chrome 的开发者工具