Skip to content

Commit

Permalink
feat(core): improve watchOverflow detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nolimits4web committed Aug 11, 2021
1 parent b97286f commit 627ae4c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
22 changes: 11 additions & 11 deletions src/core/check-overflow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ function checkOverflow() {
const swiper = this;
const params = swiper.params;
const wasLocked = swiper.isLocked;
const lastSlidePosition =
swiper.slides.length > 0 &&
params.slidesOffsetBefore +
params.spaceBetween * (swiper.slides.length - 1) +
swiper.slides[0].offsetWidth * swiper.slides.length;
const { slidesOffsetBefore } = params;

if (params.slidesOffsetBefore && params.slidesOffsetAfter && lastSlidePosition) {
swiper.isLocked = lastSlidePosition <= swiper.size;
if (slidesOffsetBefore) {
const lastSlideIndex = swiper.slides.length - 1;
const lastSlideRightEdge =
swiper.slidesGrid[lastSlideIndex] +
swiper.slidesSizesGrid[lastSlideIndex] +
slidesOffsetBefore * 2;
swiper.isLocked = swiper.size > lastSlideRightEdge;
} else {
swiper.isLocked = swiper.snapGrid.length === 1;
}

swiper.allowSlideNext = !swiper.isLocked;
swiper.allowSlidePrev = !swiper.isLocked;

// events
if (wasLocked !== swiper.isLocked) swiper.emit(swiper.isLocked ? 'lock' : 'unlock');

if (wasLocked && wasLocked !== swiper.isLocked) {
swiper.isEnd = false;
if (swiper.navigation) swiper.navigation.update();
}
if (wasLocked !== swiper.isLocked) {
swiper.emit(swiper.isLocked ? 'lock' : 'unlock');
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/modules/navigation/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ export default function Navigation({ swiper, extendParams, on, emit }) {
init();
update();
});
on('toEdge', () => {
update();
});
on('fromEdge', () => {
on('toEdge fromEdge lock unlock', () => {
update();
});
on('destroy', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/modules/pagination/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ export default function Pagination({ swiper, extendParams, on, emit }) {
$el[swiper.enabled ? 'removeClass' : 'addClass'](swiper.params.pagination.lockClass);
}
});
on('lock unlock', () => {
update();
});
on('click', (_s, e) => {
const targetEl = e.target;
const { $el } = swiper.pagination;
Expand Down
13 changes: 5 additions & 8 deletions src/modules/scrollbar/scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ export default function Scrollbar({ swiper, extendParams, on, emit }) {
trackSize = swiper.isHorizontal() ? $el[0].offsetWidth : $el[0].offsetHeight;

divider =
swiper.size / (swiper.virtualSize - (swiper.params.centeredSlides ? swiper.snapGrid[0] : 0));
swiper.size /
(swiper.virtualSize +
swiper.params.slidesOffsetBefore -
(swiper.params.centeredSlides ? swiper.snapGrid[0] : 0));
if (swiper.params.scrollbar.dragSize === 'auto') {
dragSize = trackSize * divider;
} else {
Expand Down Expand Up @@ -296,13 +299,7 @@ export default function Scrollbar({ swiper, extendParams, on, emit }) {
updateSize();
setTranslate();
});
on('update resize observerUpdate', () => {
updateSize();
});
on('resize', () => {
updateSize();
});
on('observerUpdate', () => {
on('update resize observerUpdate lock unlock', () => {
updateSize();
});
on('setTranslate', () => {
Expand Down

0 comments on commit 627ae4c

Please sign in to comment.