简体   繁体   English

滑块滑动很多次

[英]Slider slides many times

I am trying to create a slider with event listeners touchstart and touchmove . 我正在尝试使用事件监听器touchstarttouchmove创建一个滑块。

The slider works very good, if you click on the buttons. 如果单击按钮,则滑块的效果很好。 But if you move your finger from left to right, it slides many times until the last image but it should only slide once. 但是,如果您从左向右移动手指,它会滑动很多次直到最后一张图像,但它只应该滑动一次。 Also you cannot slide back. 您也无法向后滑动。

var i = 0;

// Go next
$('.next').bind('click', function() {           
    niceSlider('left', '<');
});

// Go Back
$('.back').bind('click', function() {
    niceSlider('right', '>', 0); 
});

// Greather or less
function greatherOrLess(num1, operator, num2) {
    if (operator == '<') {
    return (num1 < num2) ? true : false;
  }
  else if (operator == '>') {
    return (num1 > num2) ? true : false;
  }
}

// Slider
function niceSlider(direction, operator, NumberOfAllImages = 4, position = 600) {
  var direction = (direction == 'left') ? '-' : '+';  
  if (greatherOrLess(i, operator, NumberOfAllImages)) {
    if (direction == '+' || direction == '-') {
      $('li').animate({'left': direction + '=600px'}, 300).delay(600);  
      x = (direction == '-') ? i++ : i--;
    }
  }
  console.log($('li:first').position().left);
  console.log(x);   
}

// Event Listener
var slider = document.querySelector('.slider');
slider.addEventListener('touchstart', handleTouchStart, false);
slider.addEventListener('touchmove', handleTouchMove, false);

// Start from touch
function handleTouchStart(evt) {
  startClientX = evt.touches[0].clientX;
  startClientY = evt.touches[0].clientY;
}

// Move from touch
function handleTouchMove(evt) {
  moveClientX = evt.touches[0].clientX;
  moveClientY = evt.touches[0].clientY;

  var diffClientX = startClientX - moveClientX;
  var diffClientY = startClientY - moveClientY;

  if (Math.abs(diffClientX) > Math.abs(diffClientY)) {
    if (diffClientX > 0) {
      niceSlider('left', '<');
    }
    else {
      niceSlider('right', '>');
    }
  }
}

There must be something wrong with the function handleTouchMove . 函数handleTouchMove一定有问题。 How can I fix it? 我该如何解决?

https://jsfiddle.net/6t1wx95f/16/ https://jsfiddle.net/6t1wx95f/16/

function handleTouchStart(evt) {
  startClientX = evt.touches[0].clientX;
  startClientY = evt.touches[0].clientY;
  checkTouch = true;
}

// Move from touch
function handleTouchMove(evt) {
  moveClientX = evt.touches[0].clientX;
  moveClientY = evt.touches[0].clientY;

  if (checkTouch) {
    var diffClientX = startClientX - moveClientX;
    var diffClientY = startClientY - moveClientY;
    if (Math.abs(diffClientX) > Math.abs(diffClientY)) {
      if (diffClientX > 0) {
        niceSlider('left', '<');
      } else {
        niceSlider('right', '>', 0);
      }
    }
    checkTouch = false;
  }
}

function handleTouchEnd(evt) {
  checkTouch = true;
}

Here is jsFiddle , check it for only once use a boolean value. 这是jsFiddle ,仅使用布尔值检查一次。 On touch move next function was calling on every move. 触摸移动时,下一个功能正在移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM