|
1 | 1 | const storage = []; |
2 | 2 |
|
3 | | -module.exports = ({ history, reset = true, limit = 20 }) => ({ |
4 | | - start: () => { |
5 | | - if(history.action === 'PUSH') { |
6 | | - storage.push([ |
7 | | - window.scrollX, |
8 | | - window.scrollY |
9 | | - ]); |
| 3 | +module.exports = ({ history, elementId, reset = true, limit = 20 }) => { |
| 4 | + const $el = elementId ? document.getElementById(elementId) : window; |
| 5 | + |
| 6 | + return { |
| 7 | + start: () => { |
| 8 | + if(history.action === 'PUSH') { |
| 9 | + if($el) { |
| 10 | + if(elementId) { |
| 11 | + storage.push([ |
| 12 | + $el.scrollLeft, |
| 13 | + $el.scrollTop |
| 14 | + ]); |
| 15 | + } else { |
| 16 | + storage.push([ |
| 17 | + $el.scrollX, |
| 18 | + $el.scrollY |
| 19 | + ]); |
| 20 | + } |
| 21 | + } |
10 | 22 |
|
11 | | - if(storage.length > limit) { |
12 | | - delete storage.shift(); |
| 23 | + if(storage.length > limit) { |
| 24 | + delete storage.shift(); |
| 25 | + } |
13 | 26 | } |
14 | | - } |
15 | | - }, |
16 | | - render: () => { |
17 | | - const { location } = history; |
18 | | - const state = location.state || {}; |
19 | | - const { resetScroll, resetOnAction = 'PUSH'} = state; |
20 | | - let position = [0, 0]; |
| 27 | + }, |
| 28 | + render: () => { |
| 29 | + const { location } = history; |
| 30 | + const state = location.state || {}; |
| 31 | + const { resetScroll, resetOnAction = 'PUSH'} = state; |
| 32 | + let position = [0, 0]; |
21 | 33 |
|
22 | | - switch (history.action) { |
23 | | - case 'POP': |
24 | | - if(storage.length && !(resetScroll && resetOnAction === 'POP')) { |
25 | | - position = storage.pop(); |
26 | | - } |
27 | | - window.scrollTo(...position); |
28 | | - break; |
29 | | - case 'PUSH': |
30 | | - if(reset || (resetScroll && resetOnAction === 'PUSH')) { |
31 | | - window.scrollTo(...position); |
32 | | - } |
33 | | - break; |
| 34 | + switch (history.action) { |
| 35 | + case 'POP': |
| 36 | + if(storage.length && !(resetScroll && resetOnAction === 'POP')) { |
| 37 | + position = storage.pop(); |
| 38 | + } |
| 39 | + |
| 40 | + $el && $el.scrollTo(...position); |
| 41 | + |
| 42 | + break; |
| 43 | + case 'PUSH': |
| 44 | + if(reset || (resetScroll && resetOnAction === 'PUSH')) { |
| 45 | + $el && $el.scrollTo(...position); |
| 46 | + } |
| 47 | + |
| 48 | + break; |
| 49 | + } |
34 | 50 | } |
35 | | - } |
36 | | -}); |
| 51 | + }; |
| 52 | +}; |
0 commit comments