Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. function clickOnIndices(carrosselPai) {
  2. var carouselIndice = carrosselPai.find('.indice');
  3. var carousel = carrosselPai.find('.carousel');
  4. carouselIndice.click(function (e) {
  5. e.preventDefault();
  6. var $this = $(this);
  7. var indice = $this.data('indice');
  8.  
  9. if( ! $this.hasClass('active') ) {
  10. carouselIndice.removeClass('active');
  11. $this.addClass('active')
  12. }
  13.  
  14. if( indice === 0 ) {
  15. carousel.css('left', 0);
  16. } else {
  17. carousel.css('left', '-' + indice + '00%');
  18. }
  19. });
  20. carouselIndice.eq(0).click();
  21.  
  22. // Touch Events
  23. carrosselPai.addEventListener("touchstart", handleStart, false);
  24. carrosselPai.addEventListener("touchmove", handleMove, false);
  25. carrosselPai.addEventListener("touchcancel", handleCancel, false);
  26. carrosselPai.addEventListener("touchend", handleEnd, false);
  27.  
  28. var posIniX = 0;
  29. var posFimX = 0;
  30. var posIniY = 0;
  31. var posFimY = 0;
  32. var changePage = false;
  33. function handleStart(evt) {
  34. posIniX = evt.touches[0].pageX;
  35. posIniY = evt.touches[0].pageY;
  36. changePage = false;
  37. console.log(evt);
  38. }
  39. function handleMove(evt) {
  40. posFimX = evt.touches[0].pageX;
  41. posFimY = evt.touches[0].pageY;
  42. changePage = true;
  43. }
  44. function handleEnd() {
  45. var scrollActive = carrosselPai.find('.indice.active');
  46. if( changePage) {
  47. var difX = (posIniX - posFimX);
  48. if( difX > 50 ) {
  49. // Move para direita
  50. scrollActive.next('.indice').click();
  51. changePage = false;
  52. } else if( difX < -50 ) {
  53. // Move para esquerda
  54. scrollActive.prev('.indice').click();
  55. changePage = false;
  56. } else {
  57. scrollActive.find('.scroll').click();
  58. return false;
  59. }
  60. }
  61. changePage = false;
  62. }
  63. function handleCancel() {
  64. changePage = false;
  65. }
  66.  
  67. function commentsCarousel (carrosselPai, qtdeToShow) {
  68. if(carrosselPai.length) {
  69. var carousel = carrosselPai.find('.carousel');
  70. var tamUtil = carrosselPai.width();
  71. qtdeToShow = qtdeToShow || 3;
  72. var qtdeItems = carousel.find('.item').length;
  73.  
  74. // Verifica se a divisão ficará correta
  75. for(; (qtdeItems % qtdeToShow) !== 0;){
  76. qtdeItems++;
  77. }
  78.  
  79. var tamTotalPai = (qtdeItems * tamUtil) / qtdeToShow;
  80. var percentualTotal = (tamTotalPai * 100) / tamUtil;
  81. var tamItem = tamTotalPai / qtdeItems;
  82. var percentualUni = (tamItem * 100) / tamTotalPai;
  83.  
  84. carousel.css('width', percentualTotal + '%');
  85. carousel.find('.item').css('width', percentualUni + '%');
  86.  
  87. // Coloca os indices
  88. for (var i = 0; i < (percentualTotal / 100); i++) {
  89. carrosselPai.append('<a class="indice" href="#" data-indice="'+ i +'"></a>');
  90. }
  91.  
  92. clickOnIndices(carrosselPai);
  93. }
  94.  
  95. commentsCarousel($('#carousel'), 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement