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

pat-scroll: Implement selector:top #722

Merged
merged 2 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Features
~~~~~~~~

- pat-scroll: Implement new special `selector:top` attribute value to scroll the scroll container just to the top of the page. Ref: #721.
- pat-scroll: To define the scrollable target search also for `overflow-x` and `overflow-y` declarations.
- Rework push message support for the STOMP message protocoll instead of backends instead of WAMP.
We are using the RabbitMQ message broker for push support instead of crossbar.io.
Expand Down
2 changes: 1 addition & 1 deletion src/pat/scroll/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ The available options are:
| ----- | ------- | ----------- | ----------- |
| `trigger` | `click` | `click`, `auto` | `auto` means that the scrolling will happen as soon as the page loads. `click` means that the configured element needs to be clicked first. |
| `direction` | `top` | `top`, `left` | The direction in which the scrolling happens. |
| `selector` | | A CSS or jQuery selector string. | A selector for the element which will be scrolled by a number of pixels equal to `offset`. By default it will be the element on which the pattern is declared. Ignored unless `offset` is specified.|
| `selector` | `top`, CSS selector| A CSS or jQuery selector string or 'top'. | A selector for the element which will be scrolled by a number of pixels equal to `offset`. By default it will be the element on which the pattern is declared. Ignored unless `offset` is specified.|
| `offset` | | A number | `offset` can only be used with scrollable elements. (An element is "scrollable" if it has scrollbars, i.e. when the CSS property `overflow` is either `auto` or `scroll`.) The element scrolled by `offset` can be specified with the `selector` option. If `selector` is not present, the element on which `pat-scroll` is declared will be scrolled. |
4 changes: 3 additions & 1 deletion src/pat/scroll/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ <h3>3</h3>
</p>
</section>
<p>
<a href="#demo-main-nav" class="pat-scroll" data-pat-scroll="trigger:auto">Back to top</a>
<a href="#demo-main-nav"
class="pat-scroll"
data-pat-scroll="selector: top; trigger: auto">Back to top</a>
</p>
</article>
</body>
Expand Down
36 changes: 20 additions & 16 deletions src/pat/scroll/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ define([
}
},

findScrollContainer: function(el) {
var scrollable = $(el).parents().filter(function() {
return (
['auto', 'scroll'].indexOf($(this).css('overflow')) > -1 ||
(scroll === 'scrollTop' && ['auto', 'scroll'].indexOf($(this).css('overflow-y')) > -1) ||
(scroll === 'scrollLeft' && ['auto', 'scroll'].indexOf($(this).css('overflow-x')) > -1)
);
}).first();
if ( typeof scrollable[0] === 'undefined' ) {
scrollable = $('html, body');
}
return scrollable;
},

smoothScroll: function() {
var href, fragment;
var scroll = this.options.direction == "top" ? 'scrollTop' : 'scrollLeft',
Expand All @@ -128,6 +142,10 @@ define([
// apply scroll options directly
scrollable = this.options.selector ? $(this.options.selector) : this.$el;
options[scroll] = this.options.offset;
} else if (this.options.selector === "top") {
// Just scroll up, period.
scrollable = this.findScrollContainer(target);
options['scrollTop'] = 0;
} else {
// Get the first element with overflow (the scroll container)
// starting from the *target*
Expand All @@ -144,23 +162,9 @@ define([
return;
}

scrollable = $(target.parents().filter(function() {
return (
['auto', 'scroll'].indexOf($(this).css('overflow')) > -1 ||
(scroll === 'scrollTop' && ['auto', 'scroll'].indexOf($(this).css('overflow-y')) > -1) ||
(scroll === 'scrollLeft' && ['auto', 'scroll'].indexOf($(this).css('overflow-x')) > -1)
);
}).first())
scrollable = this.findScrollContainer(target);

if ( typeof scrollable[0] === 'undefined' ) {
scrollable = $('html, body');
// positioning context is document
if ( scroll === "scrollTop" ) {
options[scroll] = Math.floor(target.safeOffset().top);
} else {
options[scroll] = Math.floor(target.safeOffset().left);
}
} else if ( scroll === "scrollTop" ) {
if ( scroll === "scrollTop" ) {
// difference between target top and scrollable top becomes 0
options[scroll] = Math.floor(scrollable.scrollTop()
+ target.safeOffset().top
Expand Down