javascript - 我如何从滑动检测中获得负数?
问题描述
我有一个变量称为x
起点,myCount
用于计算用户所做的每次滑动,最后一个变量dist
返回特定数字离起点多远。
我想将-1存储在myCount
用户dist
将在-201下拖动(在移动设备中滑动)到左侧的情况下。
存储正数工作正常(通过拖动右侧),但它从不存储负数。
有什么解决方案可以解决这个问题吗?
这是我自己到目前为止所做的:
$(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>
解决方案
$(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>
请看一下,让我知道这是否是您要找的。
推荐阅读
- javascript - scrollY 和 scrollTop 的区别
- c++ - 如何在 C++ 中声明和使用二维指针数组
- c++ - 调试器:如何在构造函数中获取类名的地址
- tensorflow - 使用 MirroredStrategy() 卡住模型
- java - 将数据从firebase添加到android中的listview
- javascript - 我如何在 Material-ui 中实现下拉标题?
- docker - 在本地运行 GitLab 和 GitLab-Runner docker 实例时,管道中的构建步骤因连接被拒绝错误而失败
- consensus - 为什么 RAFT 协议拒绝使用较低期限的 RequestVote?
- python - 无法在 Ubuntu 20.04 LTS 上使用 python 接收 UDP 广播,适用于同一网络上的 Raspbian 和 Centos
- docker - 服务笔记本既没有指定图像也没有指定构建上下文。必须提供至少一个