最近做一個移動端H5活動,頁面中含有拖拽功能,因此包含了touchstart、touchmove、touchend相關事件,其中preventDefault();方法使頁面出現無法上滑滾動。
總結:頁面不能滑動無非就是css和js兩個方面的問題
有的人說如果我寫很多個p標籤不寫任何樣式看能不能滑動,如果能滑動說明是樣式的原因,要是也不能滑動那就應該是js的原因,是有一定的道理的,但是先別忘了看一下你的html或body是不是加了height:100%;overflow:hidden;
1.css問題:主要排查overflow:hidden;
檢查也有一定的順序,檢查超出高度的標籤是否用了overflow:hidden;最好先檢查html或body是不是加了height:100%;overflow:hidden;然後再看包裹在最外邊的元素是否加了overflow:hidden;
2.js問題:主要是有在touchstart、touchmove或touchend等事件中的阻止默認事件的原因
例如:$("#myCarousel").on("touchstart", function (e) {
e.preventDefault();
startX = e.originalEvent.changedTouches[0].pageX,
startY = e.originalEvent.changedTouches[0].pageY;
});
這種代碼其中e.preventDefault();會阻止掉默認的滾動行爲。