简体   繁体   English

Material Design Lite和jQuery,平滑滚动无法正常工作

[英]Material Design Lite and jQuery, smooth scroll not working

I am unable to use .animate method of jQuery with Google's Material Design Lite(MDL). 我无法使用Google的Material Design Lite(MDL)使用.animate方法。 I have used MDL to make navigation bar, but smooth scrolling was not working. 我使用MDL制作导航栏,但平滑滚动无法正常工作。

Simple jQuery code is this: 简单的jQuery代码是这样的:

$(function(){
            $('a.smooth').click(function(){
                console.log("SMOOTH BEGIN");
                var speed = 1000;
                var href= $(this).attr("href");
                var target = $(href == "#" || href == "" ? 'html' : href);
                var position = target.offset().top;
                $("html, body").animate({scrollTop:position}, speed, "swing");
                console.log("SMOOTH END");
            });
        });

And simple html code is this: 简单的HTML代码是这样的:

<!-- Navigation (this is included header) -->
<nav class="mdl-navigation">
    <a class="mdl-navigation__link" href="">Home</a>
    <a class="mdl-navigation__link smooth" href="#product">Product</a>
</nav>

<!--Main contents -->
<main class="mdl-layout__content">
    <div id="product"><!—-Contents-—></div>
</main>

This code showed me the log, "SMOOTH BEGIN" and "SMOOTH END". 这段代码向我展示了日志,“SMOOTH BEGIN”和“SMOOTH END”。 However, that link worked as ordinary link, not like smooth. 但是,该链接作为普通链接,不像平滑。 How can I get jQuery working with MDL? 如何让jQuery使用MDL? Maybe some conflicts are occurring, but console is not showing anything. 也许正在发生一些冲突,但是控制台没有显示任何内容。

The reason you are not seeing anything happen is because you are scrolling on the body node. 您没有看到任何事情的原因是因为您正在滚动身体节点。 MDL handles the overflow within the mdl-layout__content , this is the element you should scroll on. MDL处理mdl-layout__content中的溢出,这是您应该滚动的元素。

So this - 所以这 -

$("html, body").animate({scrollTop:position}, speed, "swing");

Now becomes- 现在变成 -

$(".mdl-layout__content").animate({scrollTop:position}, speed, "swing");

Here's a codepen example - http://codepen.io/mdlhut/pen/BNeoVa 这是一个codepen示例 - http://codepen.io/mdlhut/pen/BNeoVa

Mitsuhiko Shimomura helped me in a different stack-overflow question. Mitsuhiko Shimomura帮助我解决了一个不同的堆栈溢出问题。 Instead of var position = target.offset().top; 而不是var position = target.offset().top; I used var position = target.get( 0 ).offsetTop - 130; 我用var position = target.get( 0 ).offsetTop - 130; if not the scroll would go to top and throw off the position, it did not look good. 如果不是滚动将顶部并甩掉位置,它看起来不太好。 I had to add - 130 to the .offsetTop because the smooth scroll was going past my target id's in the html. 我不得不在.offsetTop添加- 130 ,因为平滑滚动在html中超过了我的目标id。 Thank you for the help! 感谢您的帮助! See my app where I used this smoothscroll feature. 查看我使用此smoothscroll功能的应用程序

And remember to add smooth class to anchors in html like this 并记得像这样在html中为锚点添加平滑类

<a class="smooth" href="#scrollToId">Target</a> 
<div id="scrollToId">Target</div>

$(function(){
  $('a.smooth').click(function(){
    console.log("SMOOTH BEGIN");
    var speed = 1000;
    var href= $(this).attr("href");
    var target = $(href == "#" || href == "" ? 'html' : href);
    var position = target.get( 0 ).offsetTop - 130;
    $(".mdl-layout__content").animate({scrollTop:position}, speed, "swing");
    console.log("SMOOTH END");
  });
});

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

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