Skip to content

Commit 4b67720

Browse files
committed
fix carousel nav positioning issue
1 parent 4e36348 commit 4b67720

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

docs/fibonacci.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/fibonacci.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/carousel.js

+26-9
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,36 @@ export class Carousel extends HTMLElement {
166166
}
167167

168168
_setNavPositions() {
169-
let topPos;
169+
const setPosition = (pos) => {
170+
this._nextButton.style.top = pos;
171+
this._prevButton.style.top = pos;
172+
};
170173

171174
if (this._mediaQuery.matches) {
172-
topPos = `calc(50% - 12px)`;
173-
} else {
174-
const carouselRect = this.getBoundingClientRect();
175-
const figureRect = this._figure.getBoundingClientRect();
176-
const iconTopPos = (figureRect.top - carouselRect.top) + (figureRect.height / 2) - 12;
177-
topPos = `${iconTopPos}px`;
175+
setPosition(`calc(50% - 12px)`);
176+
return;
177+
}
178+
179+
const carouselRect = this.getBoundingClientRect(),
180+
postImage = this._figure.querySelector('img'),
181+
deduceAndSetPosition = () => {
182+
const figureRect = this._figure.getBoundingClientRect();
183+
const iconTopPos = (figureRect.top - carouselRect.top) + (figureRect.height / 2) - 12;
184+
setPosition(`${iconTopPos}px`);
185+
};
186+
187+
if (this._isImageLoaded(postImage)) {
188+
deduceAndSetPosition();
189+
return;
178190
}
179191

180-
this._nextButton.style.top = topPos;
181-
this._prevButton.style.top = topPos;
192+
setPosition(`calc(50% - 12px)`);
193+
postImage.removeEventListener('load', deduceAndSetPosition);
194+
postImage.addEventListener('load', deduceAndSetPosition);
195+
}
196+
197+
_isImageLoaded(img) {
198+
return img.complete && img.naturalHeight !== 0;
182199
}
183200

184201
_navigateToPage() {

src/scss/base/_carousel.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
a.cmp__carousel-tab {
4444
@include fibMixins.flex_init($center: true);
4545
position: relative;
46-
width: fibFuncs.size(21);
4746
height: fibVars.$carousel-tab-height;
47+
padding: 0 fibVars.$sm-size;
4848

4949
@include fibMixins.set_uppercase();
5050
line-height: fibVars.$carousel-tab-height;

0 commit comments

Comments
 (0)