updateProgress.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. export default function updateProgress(translate) {
  2. const swiper = this;
  3. if (typeof translate === 'undefined') {
  4. const multiplier = swiper.rtlTranslate ? -1 : 1; // eslint-disable-next-line
  5. translate = swiper && swiper.translate && swiper.translate * multiplier || 0;
  6. }
  7. const params = swiper.params;
  8. const translatesDiff = swiper.maxTranslate() - swiper.minTranslate();
  9. let {
  10. progress,
  11. isBeginning,
  12. isEnd
  13. } = swiper;
  14. const wasBeginning = isBeginning;
  15. const wasEnd = isEnd;
  16. if (translatesDiff === 0) {
  17. progress = 0;
  18. isBeginning = true;
  19. isEnd = true;
  20. } else {
  21. progress = (translate - swiper.minTranslate()) / translatesDiff;
  22. isBeginning = progress <= 0;
  23. isEnd = progress >= 1;
  24. }
  25. Object.assign(swiper, {
  26. progress,
  27. isBeginning,
  28. isEnd
  29. });
  30. if (params.watchSlidesProgress || params.centeredSlides && params.autoHeight) swiper.updateSlidesProgress(translate);
  31. if (isBeginning && !wasBeginning) {
  32. swiper.emit('reachBeginning toEdge');
  33. }
  34. if (isEnd && !wasEnd) {
  35. swiper.emit('reachEnd toEdge');
  36. }
  37. if (wasBeginning && !isBeginning || wasEnd && !isEnd) {
  38. swiper.emit('fromEdge');
  39. }
  40. swiper.emit('progress', progress);
  41. }