我对Javascript / jQuery知之甚少,而且代码很麻烦。
我为旋转木马移动设备做了,但是控制台在“changedTouches [0] .pageX”的部分显示“未捕获的TypeError:无法读取未定义的属性'0”
有人可以帮助我吗?
CarouselCMS.prototype.mobileNav = function() {
var that = this,
diff = 0;
this.$element.bind('touchstart', function(e){
var initialX = e.changedTouches[0].pageX;
initialY = e.changedTouches[0].pageY;
dragging = true;
that.$element.bind('touchmove', function(e) {
if(Math.abs(initialY - e.changedTouches[0].pageY) < 10 || dragging == true) {
if(Math.abs(initialX - e.changedTouches[0].pageX) > 10 ){
that.$container.css("transition", "none");
$('body').css('overflow-y','hidden');
dragging = true;
var translate = that.translateCalc();
diff = initialX - e.changedTouches[0].pageX;
translate -= diff;
that.$container.css("transform","translateX("+translate+"px)");
console.log('x');
}
e.preventDefault();
}else {
diff = 0;
that.$element.unbind('touchmove');
}
});
});
this.$element.bind('touchend', function(){
that.$element.unbind('touchmove');
$('body').css('overflow-y','auto');
that.$container.css("transition", "transform "+(that.options.speed/1000)+"s linear");
if(diff > 100){
that.next();
}else if(diff < -100){
that.prev();
}
diff = 0;
that.moveSlider();
});
}
答案 0 :(得分:3)
jQuery Event对象没有changedTouches属性。
改为使用:
e.originalEvent.changedTouches[0]