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

Add @stick Event #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@ Use `v-sticky` directive to enable element postion sticky, and use `sticky-*` at
* `on-stick` _(function)_ - callback when sticky and release, receiveing 1 argument with object indicating the state, like:

```javascript
// The element is sticked on top
// The element is stuck on top
{
bottom: false,
top: true,
sticked: true
}
```

An expression that evaluates to false set on `v-sticky` can be used to disable stickiness condtionally.
# Events
* `@stick` - Same as `on-stick`-callback, but as an event

An expression that evaluates to false set on `v-sticky` can be used to disable stickiness conditionally.

```HTML
<div sticky-container>
Expand Down
9 changes: 5 additions & 4 deletions src/sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const batchStyle = (el, style = {}, className = {}) => {
};

class Sticky {
constructor(el, vm) {
constructor(el, vm, vnode) {
this.el = el;
this.vm = vm;
this.vnode = vnode;
this.unsubscribers = [];
this.isPending = false;
this.state = {
Expand Down Expand Up @@ -132,7 +133,6 @@ class Sticky {

fireEvents() {
if (
typeof this.options.onStick === 'function' &&
(this.lastState.top !== this.state.isTopSticky ||
this.lastState.bottom !== this.state.isBottomSticky ||
this.lastState.sticked !==
Expand All @@ -143,7 +143,8 @@ class Sticky {
bottom: this.state.isBottomSticky,
sticked: this.state.isBottomSticky || this.state.isTopSticky,
};
this.options.onStick(this.lastState);
if (typeof this.options.onStick === 'function') this.options.onStick(this.lastState);
if (this.vnode.data && this.vnode.data.on && this.vnode.data.on.stick) this.vnode.data.on.stick(this.lastState)
}
}

Expand Down Expand Up @@ -273,7 +274,7 @@ class Sticky {
export default {
inserted(el, bind, vnode) {
if (typeof bind.value === 'undefined' || bind.value) {
el[namespace] = new Sticky(el, vnode.context);
el[namespace] = new Sticky(el, vnode.context, vnode);
el[namespace].doBind();
}
},
Expand Down