当设备上下倾斜时,我正在尝试滚动要在上下构建的页面的正文。

此插件沿倾斜设备滚动,向左和向右滚动。

如何使其在上下倾斜的情况下上下滚动?

(function($) {
$.fn.tilt = function(params) {
    items = this;

    params = $.extend( {sensitivity: 1}, params);

    ax = ay = 0;

    window.addEventListener('devicemotion', function (e) {
        ax = e.accelerationIncludingGravity.x * params.sensitivity;
        ay = -e.accelerationIncludingGravity.y * params.sensitivity;

        if(ax > 0) {
            ax -= params.sensitivity;
            if(ax < 0) ax = 0;
        } else if(ax < 0) {
            ax += params.sensitivity;
            if(ax > 0) ax = 0;
        }

    }, false);

    mainLoop = setInterval("moveMe()");

    moveMe = function() {
        $(items).each(function() {
            scrollPos = $(this).scrollLeft() + ax;
            $(this).scrollLeft(scrollPos);
        });
    }
}
})(jQuery);
有帮助吗?

解决方案

做同样的事情,但是使用y参数?

(function($) {
$.fn.tilt = function(params) {
    items = this;

    params = $.extend( {sensitivity: 1}, params);

    ax = ay = 0;

    window.addEventListener('devicemotion', function (e) {
        ax = e.accelerationIncludingGravity.x * params.sensitivity;
        ay = -e.accelerationIncludingGravity.y * params.sensitivity;

        if(ay > 0) {
            ay -= params.sensitivity;
            if(ay < 0) ay = 0;
        } else if(ay < 0) {
            ay += params.sensitivity;
            if(ay > 0) ay = 0;
        }

    }, false);

    mainLoop = setInterval("moveMe()");

    moveMe = function() {
        $(items).each(function() {
            scrollPos = $(this).scrollTop() + ay;
            $(this).scrollTop(scrollPos);
        });
    }
}
})(jQuery);

其他提示

如果您正在使用jQuery,为什么不使用 $ .scrolltop() 修改元素的垂直滚动值?

var tilt_interval_id = setInterval(function(items) { onDeviceTilt(items); }, 50);    

function onDeviceTilt(items) {
   $(items).each(function() {
      var newScrollX = $(this).scrollLeft() + ax;
      var newScrollY = $(this).scrollTop() + ay;
      //Update the horizontal and vertical scroll positions
      $(this).scrollLeft(newScrollX);
      $(this).scrollTop(newScrollY);
   });
}

//Later, if you wish to clear the interval ...
clearInterval(tilt_interval_id);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top