Skip to content

Commit

Permalink
[Affix] Add affix and affixed events.
Browse files Browse the repository at this point in the history
  • Loading branch information
wxsms committed Oct 30, 2017
1 parent 556cd55 commit 0f176da
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
7 changes: 7 additions & 0 deletions docs/pages/components/Affix.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ Name | Type | Default | Required | Description
Name | Description
--------- | -----------------------
`default` | The affix body.

### Events

Name | Description
--------- | -----------------------
`affix` | This event fires immediately before the element has been affixed.
`affixed` | This event is fired after the element has been affixed.
22 changes: 10 additions & 12 deletions src/components/affix/Affix.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,19 @@
const body = document.body
for (let type of ['Top', 'Left']) {
let t = type.toLowerCase()
let ret = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset']
const method = 'scroll' + type
if (typeof ret !== 'number') {
// ie6,7,8 standard mode
ret = document.documentElement[method]
if (typeof ret !== 'number') {
// quirks mode
ret = document.body[method]
}
}
scroll[t] = ret
scroll[t] = window['page' + (type === 'Top' ? 'Y' : 'X') + 'Offset']
element[t] = scroll[t] + rect[t] - (this.$el['client' + type] || body['client' + type] || 0)
}
let fix = scroll.top > element.top - this.offset
if (this.affixed !== fix) { this.affixed = fix }
if (this.affixed !== fix) {
this.affixed = fix
if (this.affixed) {
this.$emit('affix')
this.$nextTick(() => {
this.$emit('affixed')
})
}
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/specs/Affix.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ describe('Affix', () => {

it('should be able to toggle affix class', async () => {
expect($el.find('.affix').length).to.equal(0)
window.scrollTo(0, 200)
await utils.sleep(200)
expect($el.find('.affix').length).to.equal(1)
window.scrollTo(0, 500)
await utils.sleep(200)
expect($el.find('.affix').length).to.equal(1)
Expand All @@ -48,11 +51,18 @@ describe('Affix', () => {
})

it('should not toggle affix class if element is hidden', async () => {
let $body = $('html, body')
$body.css('height', '9999px')
$el.css('display', 'none')
await vm.$nextTick()
expect($el.find('.affix').length).to.equal(0)
window.scrollTo(0, 500)
await utils.sleep(200)
expect($el.find('.affix').length).to.equal(0)
window.scrollTo(0, 0)
await utils.sleep(200)
expect($el.find('.affix').length).to.equal(0)
$body.css('height', '')
})

it('should be able to use offset', async () => {
Expand Down

0 comments on commit 0f176da

Please # to comment.