简体   繁体   English

Bootstrap scrollspy偏移和平滑动画

[英]Bootstrap scrollspy offset and smooth animate

Html: HTML:

<div id="mark" class="container">
    <div class="col-xs-12">
        <img src="/images/newdesk3.jpg" width="60px">
        <span>Lorem ipsum dolor</span>  

        <div id="t3"><button class="btn btn-warning">Buy Now</button></div>
        <div id="t2"><a href="#oneeye">Technical</a></div>
        <div id="t1"><a href="#similarproducts">Product presentation</a></div>
    </div>
</div>
...
<div id="similarproducts">
...
</div>
<div id="oneeye">
...
</div>

jquery offset: jQuery偏移量:

 var offset = 80;
$('#t2 a, #t1 a').click(function(event) {
    event.preventDefault();

    $($(this).attr('href'))[0].scrollIntoView();
        scrollBy(0, -offset);
});

jquery smooth animate: jQuery平滑动画:

$("#t2 a, #t1 a").on('click', function(event) {

    if (this.hash !== "") {

      event.preventDefault();      
      var hash = this.hash;

      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){  

        window.location.hash = hash;
      });
    }  
  });

Jquery offset and smooth animate(scrolling) are working individual as a code but when I put them together are not working. jQuery偏移量和平滑动画(滚动)作为代码单独工作,但是当我将它们放在一起时不起作用。 I use jquery version 1.9.1 with Bootstrap v3.3.5 我在Bootstrap v3.3.5中使用jquery版本1.9.1

I need to specify an offset position with animate. 我需要指定带有动画的偏移位置。 What is the proper code jquery to combine these codes and working properly? 将这些代码组合起来并正常工作的正确代码是什么?

the new jquery code will be 新的jQuery代码将是

$("#t2 a, #t1 a").on('click', function(event) {

    if (this.hash !== "") {

      event.preventDefault();      
      var hash = this.hash;

      $('html, body').animate({
        scrollTop: $(hash).offset().top -80
      }, 800, function(){  

        window.location.hash = hash;
      });
    }  
  });

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

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