setTranslate.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 (isNaN(translate)) {
  14. return
  15. }
  16. if (!$wrapperEl) return
  17. if (swiper.isHorizontal()) {
  18. x = rtl ? -translate : translate;
  19. } else {
  20. y = translate;
  21. }
  22. if (params.roundLengths) {
  23. x = Math.floor(x);
  24. y = Math.floor(y);
  25. }
  26. if (params.cssMode) {
  27. wrapperEl[swiper.isHorizontal() ? 'scrollLeft' : 'scrollTop'] = swiper.isHorizontal() ? -x : -y;
  28. } else if (!params.virtualTranslate) {
  29. $wrapperEl.transform(`translate3d(${x}px, ${y}px, ${z}px)`);
  30. }
  31. swiper.previousTranslate = swiper.translate;
  32. swiper.translate = swiper.isHorizontal() ? x : y; // Check if we need to update progress
  33. let newProgress;
  34. const translatesDiff = swiper.maxTranslate() - swiper.minTranslate();
  35. if (translatesDiff === 0) {
  36. newProgress = 0;
  37. } else {
  38. newProgress = (translate - swiper.minTranslate()) / translatesDiff;
  39. }
  40. if (newProgress !== progress) {
  41. swiper.updateProgress(translate);
  42. }
  43. swiper.emit('setTranslate', swiper.translate, byController);
  44. }