简体   繁体   English

平滑滚动到div聚合物元素

[英]Smooth scroll to div in polymer element

I am using the Polymer 1.0 tools to build a webapp and I am having trouble starting a smooth scroll to a specific div. 我正在使用Polymer 1.0工具来构建Web应用程序,因此无法顺利滚动到特定的div。 I have success using the polymer method this.$.div.scrollIntoView() but this just moves to the specific div without scrolling to it. 我使用聚合物方法this。$。div.scrollIntoView()取得了成功,但这只是移动到特定的div而不滚动到它。 I would like to use the jQuery method scrollTop() but cannot seem to figure where to fire this function and when/how I can attach this function to a paper-fab. 我想使用jQuery方法scrollTop(),但似乎无法弄清楚在何处触发此函数以及何时/如何将此函数附加到纸上。 Here is what I have so far: 这是我到目前为止的内容:

<div align="center">
    <paper-fab icon="arrow-downward" id="fab" on-click="scrollToView"></paper-fab>
</div>

And here are my scripts at the bottom of this specific Polymer element: 这是此特定Polymer元素底部的脚本:

<script>
    Polymer({
        is: 'my-view',
        scrollToView: function() {
            this.$.parallax.scrollIntoView(false); 
        }
    });
</script>
<script type="text/javascript">
    function goToByScroll() {
        $('html, body').animate({
            scrollTop: $("#section_one_cont").offset().top}, 'slow');
    }
    $("#fab").click(function(e) {
            goToByScroll();
    })
</script>

So the first 'Polymer' script does in fact work but not in an appealing way, and then if I try to only use the jQuery scripts, they don't do anything. 因此,第一个“ Polymer”脚本实际上可以工作,但不能以吸引人的方式起作用,然后,如果我尝试仅使用jQuery脚本,则它们什么也没做。

This is the jQuery CDN I am using: 这是我正在使用的jQuery CDN:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

Any and all help is appreciated, I would really like to figure out how to use non polymer scripts within Polymer elements as that would open up a few more things I would like to achieve with this. 感谢所有帮助,我真的很想弄清楚如何在Polymer元素中使用非Polymer脚本,因为这将为我带来更多的帮助。 Thanks in advance! 提前致谢!

I was trying for several hours to find a solution for my own project (preferable one that doesn't require JQuery). 我尝试了几个小时才能找到适合自己项目的解决方案(最好是不需要JQuery的解决方案)。 Unfortunately all options come with a twist. 不幸的是,所有选项都有一个转折。

scrollIntoView() scrollIntoView()

With scrollIntoView it is possible to define a behavior which can be set to smooth, instant or auto. 使用scrollIntoView,可以定义可以设置为平滑,即时或自动的行为。 The catch is that the scrollIntoViewOptions which are defined in the scrollIntoView are only supported by Firefox. 问题在于,仅Firefox支持在scrollIntoView中定义的scrollIntoViewOptions。

element.scrollIntoView({block: "end", behavior: "smooth"});


scroll-behavior 滚动行为

I in person would prefer this solution. 我本人更喜欢这种解决方案。 As it is just a simple CSS snippet. 因为这只是一个简单的CSS代码段。 However, similar to the above example not all Browser support it and Chrome and Opera only do it when enabling the experimental web platform features. 但是,与上述示例类似,并非所有浏览器都支持它,而Chrome和Opera仅在启用实验性Web平台功能时才支持它。 Which doesn't really help your users. 这并不能真正帮助您的用户。

scroll-behavior: smooth;

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

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