简体   繁体   English

jquery scrollintoview with bootstrap scrollspy

[英]jquery scrollintoview with bootstrap scrollspy

Hello fellow stackoverflow members who use bootstrap! 你好使用bootstrap的stackoverflow成员! I appreciate your time and input. 我感谢你的时间和投入。 I am having trouble implementing a jQuery.scrollintoview with bootstrap scrollspy. 我在使用bootstrap scrollspy实现jQuery.scrollintoview时遇到了麻烦。

http://jsfiddle.net/aKK2k/1/ http://jsfiddle.net/aKK2k/1/

Above is fiddle, scrollspy is broken but the scrollintoview should still work, or am i mistaken? 上面是小提琴,rolllspy被打破但是scrollintoview仍然可以工作,或者我错了?

Nav buttons that move the page: 导航页面的导航按钮:

 <li>
            <a href="#section-2" id="a-section-2">
              Products
            </a>
 </li>

Below is the scroll-to-section 下面是滚动到部分

 <h3 class="center" id="section-2">
       So, how can Day & Night help your business today?
 </h3>

Below is the JS that handles the page scrolling / hiding url / etc. 下面是处理页面滚动/隐藏URL /等的JS。

The implementation happens at 实施发生在

$($(this).attr('href'))[0].scrollIntoView(250, "easeOutExpo");

The whole JS 整个JS

  <script>
 $("document").ready(function() {
      $(document).on('click','.navbar li a',function(event) {
        event.preventDefault();
       if($.trim($(this).html())!='FAQ'){
//if($.trim($(this).html())!='FAQ' || $.trim($(this).html())!='FAQ2'){
           var offset = $('#myNavbar').height()+30;
           if ($(window).width() <= 768) {
                  var offset = 100;
             }
$($(this).attr('href'))[0].scrollIntoView(250, "easeOutExpo");
           scrollBy(0, -offset);
          }
          else
          {
              window.location.href = $(this).attr('href');
          }

      });

   //document.referrer returns the url from which this page has been entered,
   //we will use this to check if we are redirected from FAQs page
   var previous_url = document.referrer;
   if(previous_url=='http://dnwebdev.com/dev/faq/'){
   //if we were redirected from FAQ page, we would have a #section-value in our url
   //hash here fetched that value
   var hash = document.URL.substr(document.URL.indexOf('#')+1);

   //this is the important part, we are gonna trigger that the
   //#section-value passed in url is _clicked_. And so the browser will
   //scroll down to that section
   $('.navbar li a#a-'+hash).trigger('click');
   //once it scrolls down, this deletes the #section-value from url
   history.pushState('', document.title, window.location.pathname);
   }

});




function close_toggle() {
   $('.nav a').on('click', function () {
       if ($(".navbar-toggle").css("display") != "none") {
           $(".navbar-toggle").click();
       } else {
           $('.nav a').off('click');
       }
   });
}
close_toggle(); 




$(window).resize(close_toggle);
      </script> 

Currently the page jumps when an "easeOutExpo" is what we are going for. 目前,当“easeOutExpo”成为我们的目标时,页面会跳转。 I am very new to JS, any input is appreciated. 我对JS很新,任何意见都表示赞赏。

The website is http://dnwebdev.com/ I added jqueryUI, declared jquery before ui, but the scroll still won't work. 该网站是http://dnwebdev.com/我添加了jqueryUI,在ui之前声明了jquery,但滚动仍然无效。

Robert 罗伯特

You are invoking scrollIntoView on the raw DOM element, when you want to invoke it on the jquery matched set. 当你想在jquery匹配集上调用它时,你在原始DOM元素上调用scrollIntoView。 Remove the [0] and try this: 删除[0]并尝试:

$($(this).attr('href')).scrollIntoView(250, "easeOutExpo");

fiddle: http://jsfiddle.net/aKK2k/7/ 小提琴: http//jsfiddle.net/aKK2k/7/

(i've also added the jquery stuff to your fiddle). (我还在你的小提琴中加入了jquery的东西)。

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

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