Skip to content

Commit 72c59cf

Browse files
committed
Also supports inline scrolling
1 parent b9dfe4b commit 72c59cf

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/js-scroll-effect-module.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ export default class SCROLL_EFFECT_MODULE {
77

88
// Set State.
99
this.state = {
10-
NumScrolltopPre: window.pageYOffset,
11-
NumScrolltop : window.pageYOffset,
10+
NumScrolltopPre: 0,
11+
NumScrolltop : 0,
12+
NumWindowHeight: 0,
1213
PosList : [],
14+
$targets : null,
15+
$parent : null,
16+
$body : null,
1317
};
1418

1519
// config, options.
1620
let configDefault = {
17-
target : null,
21+
target : '[data-scroll]',
22+
targetDataName : '[data-scroll-name]',
23+
parent : 'window',
24+
body : 'body',
25+
1826
classNameInview : 'is-active',
1927

2028
displayRatio : 0.8,
@@ -90,7 +98,7 @@ export default class SCROLL_EFFECT_MODULE {
9098
if(this.timer) clearTimeout(this.timer);
9199

92100
// for Resize-Event
93-
window.addEventListener('resize', () => {
101+
this.state.$parent.addEventListener('resize', () => {
94102
this.Start();
95103
});
96104

@@ -101,7 +109,7 @@ export default class SCROLL_EFFECT_MODULE {
101109
}, this.config.firstDelay);
102110
} else if(this.config.autoStartType === 'load'){
103111
// for Load-Event
104-
window.addEventListener('load', () => {
112+
this.state.$parent.addEventListener('load', () => {
105113
this.timer = setTimeout(()=>{
106114
this.Start();
107115
this._BindEventScroll();
@@ -129,11 +137,11 @@ export default class SCROLL_EFFECT_MODULE {
129137
}
130138

131139
// スクロールの間引き処理
132-
window.addEventListener('scroll', throttle(function(){
140+
this.state.$parent.addEventListener('scroll', throttle(function(){
133141
_that._StoreElementStateAtPosList();
134142
}, _that.config.throttleInterval), {passive: true});
135143

136-
window.addEventListener('scroll', ()=>{
144+
this.state.$parent.addEventListener('scroll', ()=>{
137145
if(this.timerScroll) clearTimeout(this.timerScroll);
138146

139147
// スクロール終了時に実行
@@ -146,19 +154,23 @@ export default class SCROLL_EFFECT_MODULE {
146154
_SetDom(){
147155
this.state.PosList = [];
148156

149-
this.state.NumScrolltop = window.pageYOffset;
150-
this.NumWindowHeight = window.innerHeight;
157+
this.state.$targets = DOM.selectDom(this.config.target);
158+
this.state.$parent = this.config.parent == 'window' ? window : document.querySelector(this.config.parent);
159+
this.state.$body = this.config.body == 'body' ? document.body : document.querySelector(this.config.body);
151160

152-
let _elem = DOM.selectDom(this.config.target);
161+
this.state.NumScrolltop = (this.config.parent === 'window' ? this.state.$parent.pageYOffset : this.state.$parent.scrollTop);
162+
this.state.NumWindowHeight = (this.config.parent === 'window' ? this.state.$parent.innerHeight : this.state.$parent.clientHeight);
153163

154-
if(_elem){
155-
_elem.map((el,i)=>{
164+
if(this.state.$targets){
165+
this.state.$targets.map((el,i)=>{
156166
let offset = 0;
157167
if(el.dataset && el.dataset.semOffset !== undefined) offset = Number(el.dataset.semOffset);
168+
let _y = el.getBoundingClientRect().top - (this.config.parent === 'window' ? 0 : this.state.$parent.getBoundingClientRect().top);
169+
158170
let obj = {
159171
el: el,
160172
index: i + 1,
161-
pos: el.getBoundingClientRect().top + this.state.NumScrolltop - offset,
173+
pos: _y + this.state.NumScrolltop - offset,
162174
height: el.clientHeight,
163175
height2: el.offsetHeight,
164176
count: 0,
@@ -194,7 +206,7 @@ export default class SCROLL_EFFECT_MODULE {
194206
if(!this.state.PosList.length) return false;
195207

196208
// Scroll top cache
197-
this.state.NumScrolltop = window.pageYOffset;
209+
this.state.NumScrolltop = (this.config.parent === 'window' ? this.state.$parent.pageYOffset : this.state.$parent.scrollTop);
198210

199211
// 要素のactive状態を設定するユーティリティ関数
200212
let setActiveState = (el, active) => {
@@ -206,7 +218,7 @@ export default class SCROLL_EFFECT_MODULE {
206218

207219
let activeCountBefore = this.state.PosList.filter(item => item.active === true).length;
208220

209-
let flgPageBottom = this.state.NumScrolltop >= document.body.clientHeight - window.innerHeight;
221+
let flgPageBottom = this.state.NumScrolltop >= this.state.$body.clientHeight - (this.config.parent === 'window' ? this.state.$parent.innerHeight : this.state.$parent.clientHeight);
210222

211223
// Store element state at PosList.
212224
for (let _i = 0; _i < this.state.PosList.length; _i++) {

0 commit comments

Comments
 (0)