-
Notifications
You must be signed in to change notification settings - Fork 27.4k
$anchorScroll Offset #2070
Comments
Funny enough, i noticed that even the official docs suffer from this same problem. http://docs.angularjs.org/api/AUTO.$provide#factory Notice how when you visit that url, you cannot see the title for that documentation? It's because it is hidden behind the page header, haha. |
Volunteer |
Actually, the underlying scroll functionality comes from https://developer.mozilla.org/en-US/docs/DOM/element.scrollIntoView which doesn't allow for an offset. Doing the $window.scroll after calling elm.scrollIntoView() may be obviously hacky. |
+1 for a solution to this problem |
+1 here as well. Probably not uncommon to want a 10-20 pixel offset from the element being scrolled to. |
Yea, that's why i haven't asked for my code to be pulled in. It's hacky, but i'm not aware of any other way. Anchors on my site would be broken without it heh. |
+1 here too. |
I wrote the following in my controller. Really awful hack put could not figure any other way. Am new to Angular; but thought i would share. $scope.goToInternalHash = function(id, offset) { |
Adding invisible anchor tags with negative relative offset seems like a nice workaround for this issue, e.g. http://stackoverflow.com/a/13184714/295416 |
Might as well be using angular-ui and bootstrap Scrollspy or wrapping some other plugin in a service. |
I have the same problem. |
+1 for a solution to this There are any number of little hacks that can be implemented, but it would be nice if there was something built in. |
+1 |
3 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
7 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 ( $anchorScroll is cool ) |
+1 |
+1 |
1 similar comment
👍 |
I like @a8m's solution that uses css only: <span class="anchor" id="head1">head1</span> .anchor {
padding-top: 40px;
} demo: http://jsbin.com/foyelo/9/edit this solution is clean (keeps the layout information in CSS, doesn't require duplicate scrolling, timeout, etc) and flexible. Adding the extra offset info to $anchorScroll will result only in hacks and duplication of layout info between css and js. unless there is a good reason why the css solution doesn't work, we should close this issue |
The drawback of this approach is that it is kind of cubersome:
It is not that it's unworkable, but it sounds like a tedious (and error-prone) task, and a repetitive one in nature as well (supposing your header content and/or CSS won't stay unchanged forever). (Of course, this is not specific to the docs app - it's quite often in several projects.) The fixed offset in
(In case it's not clear yet, in the example at hand, we would pass a function that returns the current heght of the fixed header as offset.) What do you think (@IgorMinar, @petebacondarwin) ? |
How about this? Create a directive that you attach to any headings that you don't want to be obscured. This directive will insert a new anchor element in front of the heading (as proposed by @IgorMinar), which has a dynamic padding style based either on an expression passed into the directive or based on a config value defined in a service: Explicit Example <h1 ng-offset="{{ headerHeight }}">Heading 1</h1> In this case the offset is computed based on the value of $scope.headerHeight, which can be changed at run time. Implicit Example
In this case the directive could look for a service, say, .value('headingOffset', { value: 40 }); |
I will prototype this but my only concern is whether it would work on initial page render before the first digest has run... |
@petebacondarwin: I have implemented a similar solution and use in my projects (where I usually have collapsible header menus and the issue is harder to address). For what you propose to work |
Add support for a configurable scroll offset to $anchorScrollProvider. The offset is expressed in pixels and can be specified either as a fixed value or as a function that return the offset it pixels dynamically. (This is a POC and a WIP: no docs, no tests) Related to angular#9368 Closes angular#2070, angular#9360
Add support for a configurable scroll offset to $anchorScrollProvider. The offset is expressed in pixels and can be specified either as a fixed value or as a function that return the offset it pixels dynamically. (This is a POC and a WIP: no docs, no tests) Related to angular#9368 Closes angular#2070, angular#9360
Add support for a configurable scroll offset to $anchorScrollProvider. Related to angular#9368 Closes angular#2070 Closes angular#9371
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable scroll offset to $anchorScrollProvider. The offset is expressed in pixels and can be specified either as a fixed value or as a function that return the offset it pixels dynamically. (This is a POC and a WIP: no docs, no tests) Related to angular#9368 Closes angular#2070, angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable scroll offset to $anchorScrollProvider. The offset is expressed in pixels and can be specified either as a fixed value or as a function that return the offset it pixels dynamically. (This is a POC and a WIP: no docs, no tests) Related to angular#9368 Closes angular#2070, angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Add support for a configurable vertical scroll offset to `$anchorScroll`. The offset can be defined by a specific number of pixels, a callback function that returns the number of pixels on demand or a jqLite/JQuery wrapped DOM element whose height and position are used if it has fixed position. The offset algorithm takes into account items that are near the bottom of the page preventing over-zealous offset correction. Closes angular#9368 Closes angular#2070 Closes angular#9360
Just a quick feature request.
If your app has a fixed position header,
$anchorScroll
will "incorrectly" position the scroll for the users view. Nothing is broke here, but allowing for an$anchorScrollProvider
offset number would be nice.Basically, right after
elm.scrollIntoView()
it would execute roughly..if (offset > 0) $window.scroll($window.scrollX, $window.scrollY - offset);
. This would allow$anchorScrollProvider
&$anchorScroll
to play nicely with fixed position headers.The text was updated successfully, but these errors were encountered: