微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-如何从滑动检测中获取负数?

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

我想将-1存储在myCount中,如果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,'myCount is: '+ myCount);
      } else if (dist > -201) {
        myCount -= 1;
        console.log('distance is: ' + dist,'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>
最佳答案
$(function stripeAnimeModule() {
  // Variables
    var screen = $('.page1'),'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,'myCount is: '+ myCount);
      } else if (dist < -201) {
        myCount -= 1;
        console.log('distance is: ' + dist,'myCount is: '+ myCount);
      } else {
        myCount = myCount;
        console.log('nothing',myCount);
      }
      e.stopPropagation();
      e.preventDefault();
    }
    buttons.on({click: clicked});
    screen.bind({touchstart:touchStart,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>

请看看,让我知道这是否是您想要的.

原文地址:https://www.jb51.cc/html/530431.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐