Create Anchor Element in polymer

送分小仙女□ 提交于 2019-12-05 05:12:16

问题


Is it possible to create an element that will be used as an anchor in polymer. So for example

<template repeat="{{ content in contentitems }}">
    <div id="{{ content.id }}">{{content.stuff}}</div>
</template>

Would it be possible to create a hyperlink to the content#id anchor like http://example.com/#someid


Alternatively, we can query that element with querySelector like the below and then scroll it into view if necessary with JavaScript. I'd rather not have to use a JS router however for anchor hyperlinking?

scrollIntoViewFunc(document.querySelector("html /deep/ #someid"))

Here's an actual URL I want to get working: http://megawac.github.io/status-whiskey/#status-408


回答1:


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




回答2:


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);


来源:https://stackoverflow.com/questions/25689640/create-anchor-element-in-polymer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!