如何从滑动检测中获得负数?

问题描述 投票:3回答:1

我有一个名为x的变量,它是起始点,myCount用于计算用户完成的每次滑动,最后一个变量dist返回特定数字,距离起点有多远。

我想在myCount中存储-1,如果dist将在-201下拖动(在移动中滑动)由用户左侧。

存储正数工作正常(通过拖动右侧),但它从不存储负数。

有没有解决方案来解决这个问题?

这就是我自己到目前为止所做的事情:

CodePen

$(function stripeAnimeModule() {
  // Variables
    var screen = $('.page1'),
    buttons = $('.buttons').find('.button'),
    myCount = 1,
    x,
    dist;
  // Functions
    function clicked(e) {
      if ($(this).hasClass('prev')) {
        myCount -= 1;
        console.log(myCount, 'this is clicked');
      } else {
        myCount += 1;
        console.log(myCount);
      }
    }
    function touchStart(e) {
      return x = e.touches[0].pageX;
      e.preventDefault();
    }
    function touchMove(e) {
      var drag = e.touches[0].pageX;
      var dist = Math.sqrt(x + drag);
      e.preventDefault();
    }
    function touchEnd(e) {
      var dist = e.changedTouches[0].pageX - x;
      if (dist < Math.abs(-200)) {
        myCount = myCount;
        console.log('nothing', dist, myCount);
      } else if (dist > 201) {
        myCount += 1;
        console.log('distance is: ' + dist, x, 'myCount is: '+ myCount);
      } else if (dist > -201) {
        myCount -= 1;
        console.log('distance is: ' + dist, x, 'myCount is: '+ myCount);
      }
      e.stopPropagation();
      e.preventDefault();
    }
    buttons.on({click: clicked});
    screen.bind({touchstart:touchStart, touchmove:touchMove, touchend: touchEnd});
})
* {
  margin: 0;
  padding: 0;
}
ul li {
  list-style: none;
}
a {
  display: block;
}
.page1 {
  position: absolute;
  width: 100vw;
  height: 100vh;
  background-color: grey;
}
.buttons {
  z-index: 1;
}
.prev {
  position: absolute;
  left: 0;
  margin: 0 40px;
}
.shrink {
  position: absolute;
}
.next {
  position: absolute;
  right: 0;
  margin: 0 40px;
}
<div class="page1" href="#"></div>
<ul class="buttons">
  <li class="button prev">Prev</li>
  <li class="shrink"></li>
  <li class="button next">Next</li>
</ul>
<br><br><br><br><br><br><br><br><br><br><br><br><br>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
javascript jquery html css swipe
1个回答
2
投票

$(function stripeAnimeModule() {
  // Variables
    var screen = $('.page1'),
    buttons = $('.buttons').find('.button'),
    myCount = 1,
    x,
    dist;
  // Functions
    function clicked(e) {
      if ($(this).hasClass('prev')) {
        myCount -= 1;
        console.log(myCount, 'this is clicked');
      } else {
        myCount += 1;
        console.log(myCount);
      }
    }
    function touchStart(e) {
      return x = e.touches[0].pageX;
      e.preventDefault();
    }
    function touchMove(e) {
      var drag = e.touches[0].pageX;
      var dist = Math.sqrt(x + drag);
      e.preventDefault();
    }
    function touchEnd(e) {
      var dist = e.changedTouches[0].pageX - x;
      if (dist > 201) {
        myCount += 1;
        console.log('distance is: ' + dist, x, 'myCount is: '+ myCount);
      } else if (dist < -201) {
        myCount -= 1;
        console.log('distance is: ' + dist, x, 'myCount is: '+ myCount);
      } else {
        myCount = myCount;
        console.log('nothing', dist, myCount);
      }
      e.stopPropagation();
      e.preventDefault();
    }
    buttons.on({click: clicked});
    screen.bind({touchstart:touchStart, touchmove:touchMove, touchend: touchEnd});
})
* {
  margin: 0;
  padding: 0;
}
ul li {
  list-style: none;
}
a {
  display: block;
}
.page1 {
  position: absolute;
  width: 100vw;
  height: 100vh;
  background-color: grey;
}
.buttons {
  z-index: 1;
}
.prev {
  position: absolute;
  left: 0;
  margin: 0 40px;
}
.shrink {
  position: absolute;
}
.next {
  position: absolute;
  right: 0;
  margin: 0 40px;
}
<div class="page1" href="#"></div>
<ul class="buttons">
  <li class="button prev">Prev</li>
  <li class="shrink"></li>
  <li class="button next">Next</li>
</ul>
<br><br><br><br><br><br><br><br><br><br><br><br><br>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果您正在寻找,请查看并告诉我们。

© www.soinside.com 2019 - 2024. All rights reserved.