Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make scrolling to anchors more robust. #181

Merged
merged 3 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions demo/iron-doc-module.html

Large diffs are not rendered by default.

66 changes: 39 additions & 27 deletions iron-doc-viewer-behavior.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,45 @@
*/
scrollToAnchor: function(hash) {
hash = hash || window.location.hash;
if (hash && hash.length > 0) {
if (hash.indexOf('#') === 0) {
hash = hash.substring(1);
}
// Ensure dom-repeats have stamped.
Polymer.dom.flush();
var anchor = this.fragmentPrefix + hash;
var element = this.$$('[anchor-id="' + anchor + '"]');
if (element) {
// Don't scroll until we've drawn the next frame, otherwise our
// element might not know it's final screen position yet.
Polymer.RenderStatus.afterNextRender(this, function() {
element.scrollIntoView({behavior: 'smooth'});
// Highlight the section for a moment so we can tell where
// exactly we were deep-linked.
element.classList.add('scrolled');
this.async(function() {
element.classList.remove('scrolled');
}, 1000);
});

} else {
// Maybe our API section has this anchor.
var api = this.$$('iron-doc-api');
if (api) {
api.scrollToAnchor(hash);
}
if (!hash || hash.length == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

===

return;
}
if (hash.indexOf('#') === 0) {
hash = hash.substring(1);
}
// Ensure dom-repeats have stamped.
Polymer.dom.flush();
var anchor = this.fragmentPrefix + hash;
var element = this.$$('[anchor-id="' + anchor + '"]');
if (element) {
// Don't scroll until we've drawn the next frame, otherwise our
// element might not know it's final screen position yet.
Polymer.RenderStatus.afterNextRender(this, function() {
element.scrollIntoView({behavior: 'smooth'});
// Highlight the section for a moment so we can tell where
// exactly we were deep-linked.
element.classList.add('scrolled');
this.async(function() {
element.classList.remove('scrolled');
}, 1000);
});
return;
}
// Maybe our API section has this anchor.
var api = this.$$('iron-doc-api');
if (api) {
api.scrollToAnchor(hash);
return;
}

// Maybe some other subsection has the anchor.
var subElements = this.root.querySelectorAll('*');
for (var i = 0; i < subElements.length; i++) {
var element = subElements[i];
if (element.fragmentPrefix && element.scrollToAnchor &&
hash.indexOf(element.fragmentPrefix) === 0) {
element.scrollToAnchor(hash.slice(element.fragmentPrefix.length));
return;
}
}
},
Expand Down