Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): swiping in rewind mode #5422

Merged
merged 6 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 61 additions & 64 deletions playground/core/index.html
Original file line number Diff line number Diff line change
@@ -1,72 +1,69 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Swiper Playground</title>
<meta name="viewport" content="width=device-width" />
</head>

<head>
<meta charset="UTF-8" />
<title>Swiper Playground</title>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<section class="swiper">
<div class="swiper-pagination"></div>
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</section>
<style>
body,
html {
padding: 0;
margin: 0;
position: relative;
height: 100%;
}

<body>
<section class="swiper">
<div class="swiper-pagination"></div>
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
<div class="swiper-slide">Slide 4</div>
<div class="swiper-slide">Slide 5</div>
<div class="swiper-slide">Slide 6</div>
<div class="swiper-slide">Slide 7</div>
<div class="swiper-slide">Slide 8</div>
<div class="swiper-slide">Slide 9</div>
<div class="swiper-slide">Slide 10</div>
</section>
<style>
body,
html {
padding: 0;
margin: 0;
position: relative;
height: 100%;
}
.swiper {
width: 100%;
height: 300px;
margin: 50px auto;
}

.swiper {
width: 100%;
height: 300px;
margin: 50px auto;
}
.swiper-slide {
background: #f1f1f1;
color: #000;
text-align: center;
line-height: 300px;
}
</style>
<script type="module">
// eslint-disable-next-line
import 'swiper/swiper-bundle.css';

.swiper-slide {
background: #f1f1f1;
color: #000;
text-align: center;
line-height: 300px;
}
</style>
<script type="module">
// eslint-disable-next-line
import 'swiper/swiper-bundle.css';

// eslint-disable-next-line
import Swiper from 'swiper/swiper-bundle.esm.js';

// eslint-disable-next-line
window.swiper = new Swiper({
el: '.swiper',
createElements: true,
speed: 300,
cssMode: true,
rewind: true,
slidesPerView: 1,
navigation: true,
pagination: {
el: '.swiper-pagination',
dynamicBullets: true,
// dynamicMainBullets: 3,
},
});

</script>
</body>
// eslint-disable-next-line
import Swiper from 'swiper/swiper-bundle.esm.js';

// eslint-disable-next-line
window.swiper = new Swiper({
el: '.swiper',
createElements: true,
speed: 300,
// cssMode: true,
rewind: true,
slidesPerView: 1,
navigation: true,
pagination: {
el: '.swiper-pagination',
dynamicBullets: true,
// dynamicMainBullets: 3,
},
});
</script>
</body>
</html>
34 changes: 28 additions & 6 deletions src/core/events/onTouchEnd.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,23 +101,45 @@ export default function onTouchEnd(event) {
}
}

let rewindFirstIndex;
let rewindLastIndex;
if (params.rewind) {
if (swiper.isBeginning) {
const lastIndex =
swiper.params.virtual && swiper.params.virtual.enabled && swiper.virtual
? swiper.virtual.slides.length - 1
: swiper.slides.length - 1;
rewindLastIndex = lastIndex;
} else if (swiper.isEnd) {
rewindFirstIndex = 0;
}
}
// Find current slide size
const ratio = (currentPos - slidesGrid[stopIndex]) / groupSize;
const increment = stopIndex < params.slidesPerGroupSkip - 1 ? 1 : params.slidesPerGroup;

if (timeDiff > params.longSwipesMs) {
// Long touches
if (!params.longSwipes) {
swiper.slideTo(swiper.activeIndex);
return;
}
if (swiper.swipeDirection === 'next') {
if (ratio >= params.longSwipesRatio) swiper.slideTo(stopIndex + increment);
if (ratio >= params.longSwipesRatio)
swiper.slideTo(params.rewind && swiper.isEnd ? rewindFirstIndex : stopIndex + increment);
else swiper.slideTo(stopIndex);
}
if (swiper.swipeDirection === 'prev') {
if (ratio > 1 - params.longSwipesRatio) swiper.slideTo(stopIndex + increment);
else swiper.slideTo(stopIndex);
if (ratio > 1 - params.longSwipesRatio) {
swiper.slideTo(stopIndex + increment);
} else if (
rewindLastIndex !== null &&
ratio < 0 &&
Math.abs(ratio) > params.longSwipesRatio
) {
swiper.slideTo(rewindLastIndex);
} else {
swiper.slideTo(stopIndex);
}
}
} else {
// Short swipes
Expand All @@ -130,10 +152,10 @@ export default function onTouchEnd(event) {
(e.target === swiper.navigation.nextEl || e.target === swiper.navigation.prevEl);
if (!isNavButtonTarget) {
if (swiper.swipeDirection === 'next') {
swiper.slideTo(stopIndex + increment);
swiper.slideTo(rewindFirstIndex !== null ? rewindFirstIndex : stopIndex + increment);
}
if (swiper.swipeDirection === 'prev') {
swiper.slideTo(stopIndex);
swiper.slideTo(rewindLastIndex !== null ? rewindLastIndex : stopIndex);
}
} else if (e.target === swiper.navigation.nextEl) {
swiper.slideTo(stopIndex + increment);
Expand Down