Javascript平滑滚动iPad

Javascript smooth scroll iPad

本文关键字:iPad 滚动 平滑 Javascript      更新时间:2023-09-26

我使用这个代码,要在ipad中滚动溢出的div(隐藏),并且工作人员希望它不平滑,有什么方法可以让它平滑滚动吗?

function initMobileScroll(ele) {
var mobileScrollArea = document.getElementById(ele);
mobileScrollArea.addEventListener('touchstart', function(event){
    touchstart (event);
});
mobileScrollArea.addEventListener('touchmove', function(event){
    touchmove (event);
});
// let’s set the starting point when someone first touches
function touchstart (e) {
    startY = e.touches[0].pageY;
    startX = e.touches[0].pageX;
}
// calls repeatedly while the user’s finger is moving
function touchmove(e) {
    var touches = e.touches[0];
    // override the touch event’s normal functionality
            e.preventDefault();
    // y-axis
    var touchMovedY = startY - touches.pageY;
    startY = touches.pageY; // reset startY for the next call
    mobileScrollArea.scrollTop = mobileScrollArea.scrollTop + touchMovedY;
    // x-axis
    var touchMovedX = startX - touches.pageX;
    startX = touches.pageX; // reset startX for the next call
    mobileScrollArea.scrollLeft = mobileScrollArea.scrollLeft + touchMovedX;
}   
}

代码源:http://www.flexmls.com/developers/2011/04/13/ipad-and-single-finger-scrolling-in-flexmls/

将其添加到您的css中:

-webkit-overflow-scrolling: touch;

兼容性

  • Safari:iOS 5

  • 安卓浏览器:3.0

  • 黑莓浏览器:6

  • Chrome手机版

  • Firefox Mobile

  • IE Mobile:9

  • Opera Mobile

示例:JSfiddle

参考:barrow.io-溢出滚动

看看这个:http://cubiq.org/iscroll-4

看起来和我想做的很相似。祝好运

if (evt.type == "touchstart")
{
    tsStartY = evt.originalEvent.changedTouches[0].clientY;
}
if (evt.type == "touchend")
{
    var te = evt.originalEvent.changedTouches[0].clientY;
    var delta = tsStartY - te;
    if (tsStartY > te) {
        // going down
        this.transY += delta;
        if (this.transY > this.minY)
        {
            this.transY = this.minY;
        }
    }
    if (tsStartY < te)
    {
        // going up
        this.transY -= -delta;
        if (this.transY < this.maxY) {
            this.transY = this.maxY;
        }
    }

}