聚合物滚动到特定的div [英] Polymer scroll to a specific div
问题描述
我正在使用Polymer Starter Kit,但是我需要的是,当我单击某个元素(链接,按钮或类似内容)时,页面会滚动到同一页面中的特定<div>
,就像您使用时一样:
I am using Polymer Starter Kit, but what I need is that when I click in an element (link, buttom or similar) the page makes a scroll to a specific <div>
in the same page, like when you use:
<a href="specificID">
并转到:<div id="specificID">
我该如何在聚合物中做到这一点?
How can I do that in polymer?
推荐答案
在Polymer元素模板的内部,您应该具有要滚动到的div.给您的div一种ID.例如:<div id="icecream"></div>
. Polymer做了一件非常方便的事情,将对所有具有ID的模板节点的引用放在元素的$属性上.在元素上的方法内部,您可以滚动到div,如下所示:
this.$.icecream.scrollIntoView()
.您可以通过将其注册为元素中另一个元素上的点击处理程序,或强制性地在元素上调用方法来触发此操作,例如myElementInstance.someMethodThatCausesScrolling()
Inside of your Polymer element's template, you should have your div that you wish to scroll to. Give your div some kind of id. For example: <div id="icecream"></div>
. Polymer does a very convenient thing, where it places references to all of your template nodes that have an id on the $ property of your element. Inside of a method on your element, you can scroll to your div as follows:
this.$.icecream.scrollIntoView()
. You can trigger this to happen by registering it as a click handler on another element in your element, or by imperatively calling your method on your element, e.g. myElementInstance.someMethodThatCausesScrolling()
这篇关于聚合物滚动到特定的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!