Polymer 聚合物滚动到特定的div
我正在使用Polymer Starter Kit,但我需要的是,当我单击某个元素(链接、按钮或类似项)时,页面会滚动到同一页面中的特定Polymer 聚合物滚动到特定的div,polymer,polymer-1.0,Polymer,Polymer 1.0,我正在使用Polymer Starter Kit,但我需要的是,当我单击某个元素(链接、按钮或类似项)时,页面会滚动到同一页面中的特定,就像您使用: 并转到: 如何在polymer中做到这一点?在polymer元素的模板中,您应该有希望滚动到的div。为div指定某种id。例如:。Polymer做了一件非常方便的事情,它将引用放置到元素的$property上具有id的所有模板节点。在元素的方法内部,可以按如下方式滚动到div: this.$.icecream.scrollIntoView()。您
,就像您使用:
并转到:
如何在polymer中做到这一点?在polymer元素的模板中,您应该有希望滚动到的div。为div指定某种id。例如:
。Polymer做了一件非常方便的事情,它将引用放置到元素的$property上具有id的所有模板节点。在元素的方法内部,可以按如下方式滚动到div:
this.$.icecream.scrollIntoView()
。您可以通过将其注册为元素中另一个元素上的单击处理程序,或强制调用元素上的方法(例如,myElementInstance.SomeMethodThakesScrolling()
)来触发此操作。在聚合元素的模板中,您应该有希望滚动到的div。为div指定某种id。例如:
。Polymer做了一件非常方便的事情,它将引用放置到元素的$property上具有id的所有模板节点。在元素的方法内部,可以按如下方式滚动到div:
this.$.icecream.scrollIntoView()
。您可以将其注册为元素中另一个元素的单击处理程序,或者强制调用元素上的方法,例如,myElementInstance.somemethodthatscausescrolling()
我将其剪切到聚合应用程序的index.html主体中。虽然这是一个简单的登录页,我不介意将所有链接都变成锚,但它应该很容易适应:
<script>
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
</script>
$(函数(){
$('a[href*=“#”]:不是([href=“#”]”)。单击(函数(){
if(location.pathname.replace(/^\/,'')==this.pathname.replace(/^\/,'')和&location.hostname==this.hostname){
var target=$(this.hash);
target=target.length?target:$('[name='+this.hash.slice(1)+']');
if(目标长度){
$('html,body')。设置动画({
scrollTop:target.offset().top
}, 1000);
返回false;
}
}
});
});
我把这个剪下来的片段扔进了一个聚合物应用程序index.html的主体中。虽然这是一个简单的登录页,我不介意将所有链接都变成锚,但它应该很容易适应:
<script>
$(function() {
$('a[href*="#"]:not([href="#"])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html, body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
</script>
$(函数(){
$('a[href*=“#”]:不是([href=“#”]”)。单击(函数(){
if(location.pathname.replace(/^\/,'')==this.pathname.replace(/^\/,'')和&location.hostname==this.hostname){
var target=$(this.hash);
target=target.length?target:$('[name='+this.hash.slice(1)+']');
if(目标长度){
$('html,body')。设置动画({
scrollTop:target.offset().top
}, 1000);
返回false;
}
}
});
});
非常感谢,我意识到当我想去的div在元素中时,它就起作用了,但是,如果div在index.html中呢?可能吗?当然可以。使用document.querySelector获取div,然后调用.scrolling查看该div太棒了!谢谢,是否可以产生一种效果,比如页面是用聚合物“缓慢”滚动到该div?非常感谢,我意识到当我想要的div位于元素内部时,这种效果是有效的,但是,如果div位于index.html中呢?可能吗?当然可以。使用document.querySelector获取div,然后调用.scrolling查看该div太棒了!谢谢,能不能让页面像是用聚合物“缓慢”滚动到那个div?