setTranslate.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. export default function setTranslate(translate, byController) {
  2. const swiper = this;
  3. const {
  4. rtlTranslate: rtl,
  5. params,
  6. $wrapperEl,
  7. wrapperEl,
  8. progress
  9. } = swiper;
  10. let x = 0;
  11. let y = 0;
  12. const z = 0;
  13. if (swiper.isHorizontal()) {
  14. x = rtl ? -translate : translate;
  15. } else {
  16. y = translate;
  17. }
  18. if (params.roundLengths) {
  19. x = Math.floor(x);
  20. y = Math.floor(y);
  21. }
  22. if (params.cssMode) {
  23. wrapperEl[swiper.isHorizontal() ? 'scrollLeft' : 'scrollTop'] = swiper.isHorizontal() ? -x : -y;
  24. } else if (!params.virtualTranslate) {
  25. $wrapperEl.transform(`translate3d(${x}px, ${y}px, ${z}px)`);
  26. }
  27. swiper.previousTranslate = swiper.translate;
  28. swiper.translate = swiper.isHorizontal() ? x : y; // Check if we need to update progress
  29. let newProgress;
  30. const translatesDiff = swiper.maxTranslate() - swiper.minTranslate();
  31. if (translatesDiff === 0) {
  32. newProgress = 0;
  33. } else {
  34. newProgress = (translate - swiper.minTranslate()) / translatesDiff;
  35. }
  36. if (newProgress !== progress) {
  37. swiper.updateProgress(translate);
  38. }
  39. swiper.emit('setTranslate', swiper.translate, byController);
  40. }