hammer.js(http://hammerjs.github.io/recognizer-swipe/)检查滑动的速度,我想知道如何获得这个值,以便阻止我的元素#visitContent的边距有多大,并获得更平滑的滑动。
HTML
<section id="visits">
<div id='visitsContent'>
<div></div>
<div></div>
<div></div>
</div>
</section>
的Javascript
var visits = document.getElementById('visits');
var mc = new Hammer(visits, {velocity:0.80});
mc.on("swipeleft", function(ev) {
$('#visitsContent').css({
marginLeft : '-=200px'
})
});
mc.on("swiperight", function(ev) {
$('#visitsContent').css({
marginLeft : '+=200px'
})
});
SASS
#visits
width: 100%
padding: 0px
border-bottom: none
background-color: $backgroundColorSecond
margin-bottom: 10px
margin-left: -20px
#visitsContent
div
width: 180px
height: 180px
float: left
margin-left: 5px
background-color: green
color: black
text-align: center
还有一个小提琴 http://jsfiddle.net/mL911mqn/
答案 0 :(得分:2)
从您的jsfiddle判断,您现在看起来正在使用平移手势而不是轻扫手势。所以在你的pan事件处理程序中,你应该能够像这样访问拖动速度:
function panGestureHandler(ev){
var velocity = ev.gesture.velocity;
var velocityX = ev.gesture.velocityX;
var velocityY = ev.gesture.velocityY;
console.log("horizontal drag speed = " + velocityX);
}