移动端怎么用JS判断是手势是向上滑动还是向下滑动

JavaScript015

移动端怎么用JS判断是手势是向上滑动还是向下滑动,第1张

var divTwo = $('.divTwo')

//1.禁用div的滚轮事件

$('.divTwo').mousewheel(function (e) {

return false

})

//2.如果滚动条到底底部的时候 禁用window的滚轮滚动

//3.判断滚动的方向

divTwo.mousewheel(function (e, delta) {

var decoration = delta > 0 ? 'Up' : 'down'

$('.divFixed').text(decoration)

var scrollTop = divTwo.scrollTop()

var scrollHegiht = divTwo[0].scrollHeight

var height = divTwo.height()

//滚动条 到底部且 滚轮向下滚动

if (scrollTop + height >= scrollHegiht && delta < 0) {

e.preventDefault()

}

//滚动条 到顶部 且滚轮向上

else if (scrollTop == 0 && delta > 0) {

e.preventDefault()

}

})

$(document).mousewheel(function (e, delta) {

$('.divFixed').text(delta)

})

有一个mousewheel.js插件可以实现你说的功能。这是你需要的判断向上或者向下滚动的代码。希望对你有帮助,或者你直接百度一下mousewheel看一下具体的代码。

js实现随页面滑动效果的方法。具体如下:

页面向上向下滚动,分享到的模块随着滑动。

要点:

代码如下:

var scrtop =document.documentElement.scrollTop||document.body.scrollTop

var height = document.documentElement.clientHeight||document.body.clientHeight

var top = scrtop + (height - jb51.offsetHeight)/2

top = parseInt(top)

获得页面垂直居中的位置

上代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="gb2312" />

<title>无标题文档</title>

<style>

body{margin:0padding:0font:12px/1.5 arialheight:2000px}

#jb51{width:100pxheight:200pxline-height:200px

text-align:centerborder:1p solid #ccc

background:#f5f5f5position:absoluteleft:-100pxtop:0}

#jb51_tit{position:absoluteright:-20pxtop:60px

width:20pxheight:60pxpadding:10px 0

background:#06ctext-align:center

line-height:18pxcolor:#fff}

</style>

<script>

window.onload = function(){

var jb51 = document.getElementById("jb51")

jb51.onmouseover = function(){

startrun(jb51,0,"left")

}

jb51.onmouseout = function(){

startrun(jb51,-100,"left")

}

window.onscroll = window.onresize = function(){

var scrtop=document.documentElement.scrollTop||document.body.scrollTop

var height=document.documentElement.clientHeight||document.body.clientHeight

var top = scrtop + (height - jb51.offsetHeight)/2

top = parseInt(top)

startrun(jb51,top,"top")

}

}

var timer = null

function startrun(obj,target,direction){

clearInterval(timer)

timer = setInterval(function(){

var speed = 0

if(direction == "left"){

speed = (target-obj.offsetLeft)/8

speed = speed>0?Math.ceil(speed):Math.floor(speed)

if(obj.offsetLeft == target){

clearInterval(timer)

}else{

obj.style.left = obj.offsetLeft + speed + "px"

}

}

if(direction == "top"){

speed = (target-obj.offsetTop)/8

speed = speed>0?Math.ceil(speed):Math.floor(speed)

if(obj.offsetTop == target){

clearInterval(timer)

}else{

obj.style.top = obj.offsetTop + speed + "px"

}

document.title = obj.offsetTop + ',' + target + ',' +speed

}

},30)

}

</script>

</head>

<body>

<div id="jb51">

分享到内容

<span id="jb51_tit">分享到</span>

</div>

</body>

</html>