Create Anchor Element in polymer

雨燕双飞 提交于 2019-12-03 22:02:14

The Web Component gods (aka Blink engineers) have decided that anchors inside of shadow-roots will not automatically scroll into view like they do in the main document. For this reason, I believe you will have to do something like you showed to handle this in JavaScript.

After brief searching, I couldn't find a reference to this question in the spec, it really needs to be spelled out somewhere.

If you come up with general solution, elementize it and share it back to the community. =P

Let's say you have a simple-element with some child elements with ids as anchors:

<simple-element>
    <div id="anchor1"></div>
    <div id="anchor2"></div>
</simple-element>

Then you can make a function to scrollIntoView when the hash changes:

window.addEventListener('hashchange', function() {
    let targetElement = document.querySelector('simple-element').$[location.hash.substr(1)];
    if(targetElement) {
        targetElement.scrollIntoView();
    }
}, false);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!