Skip to content
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ npm i --save-dev vue-scroll-show
```js
import VueScrollShow from 'vue-scroll-show'

Vue.use(VueScrollShow)
Vue.use(VueScrollShow, defaultOptions)
```

#### SSR (Nuxt.js)

```js
import VueScrollShow from 'vue-scroll-show/dist/ssr.index'

Vue.use(VueScrollShow)
Vue.use(VueScrollShow, defaultOptions)
```

## Usage
Expand All @@ -43,7 +43,9 @@ or with options

| Option | Description |
| ------ | ------ |
| noActive | Add classes if element not in display area |
| active | Add classes if element in display area |
| delay | Timeout to add classes to element |
| offset | Screen offset to add class to element |
| parentId | Id parent element for starting add classes to directive elements |
| parentId | Id parent element for starting add classes to directive elements |
| alternate | If remove active class and add noActive class if element not in display area |
2 changes: 1 addition & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ssr.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ <h3>Scroll down</h3>
<div class='block'>
<div id='event'>
<div class='footer'>
<span v-scroll-show='{active: "show fadeIn", delay: 500, parentId: "event"}'>Show one from selector</span>
<span v-scroll-show='{active: "show fadeIn", delay: 1000, parentId: "event"}'>Show two from selector</span>
<span v-scroll-show='{active: "show fadeIn", delay: 500, parentId: "event", alternate: true}'>Show one from selector</span>
<span v-scroll-show='{active: "show fadeIn", delay: 1000, parentId: "event", alternate: true}'>Show two from selector</span>
</div>
</div>
<span v-scroll-show='{active: "show fadeIn"}'>Show three without selector</span>
<span v-scroll-show='{active: "show fadeIn", offset: 500}'>Show four with offset 500</span>
<span v-scroll-show='{active: "show fadeIn", alternate: true}'>Show three without selector</span>
<span v-scroll-show='{active: "show fadeIn", offset: 500, alternate: true}'>Show four with offset 500</span>
</div>

</div>

<script>
Expand All @@ -71,7 +71,7 @@ <h3>Scroll down</h3>
test: false
}),
methods: {

},
mounted () {
setTimeout(() => {
Expand Down
36 changes: 25 additions & 11 deletions src/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ const getParentById = (el, id) => {
return parent
}

const defaultOptions = {
noActive: 'no-active',
active: 'active',
delay: 0,
offset: 0,
parentId: null,
alternate: false
}

export default {
defaultOptions,

// Check item in the display area
inViewScroll (el, options) {
const rect = el.getBoundingClientRect()
Expand All @@ -22,34 +33,37 @@ export default {

bind (el, binding) {
// Default schema options
let options = {
active: 'active',
delay: 0,
offset: 0,
parentId: null
}
let options = Object.assign({}, defaultOptions)

// Assign default options and element options
options = Object.assign(options, binding.value)

// Add no-active class to element
el.classList.add('no-active')
el.classList.add(options.noActive)

el.$onScroll = () => {
const targetElement = options.parentId ? getParentById(el, options.parentId) : el

if (binding.def.inViewScroll(targetElement, options)) {
// Delay add class
setTimeout(() => {
const classList = options.active.split(' ')
const classList = options.active.trim().split(' ').filter(item => item)
classList.forEach(val => {
el.classList.add(val)
})
el.classList.remove('no-active')
el.classList.remove(options.noActive)
}, options.delay)

// Unbinding event
binding.def.unbind(el, binding)
if (!options.alternate) {
binding.def.unbind(el, binding)
}
} else if (options.alternate && !el.classList.contains(options.noActive)) {
const classList = options.active.trim().split(' ').filter(item => item)
classList.forEach(val => {
el.classList.remove(val)
})
el.classList.add(options.noActive)
}
}
document.addEventListener('scroll', el.$onScroll)
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Directive from './directive.js'

const VueScrollShow = {
install (Vue, options) {
install (Vue, options = {}) {
Object.assign(Directive.defaultOptions, options)
Vue.directive('scroll-show', Directive)
}
}
Expand Down
Loading