setTranslate.js 1.3 KB

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