Question

J'essaie de faire défiler le corps d'une page que je construis de haut en bas alors que l'appareil s'incline de haut en bas.

Ce plugin défile à gauche et à droite comme un appareil s'incline.

Comment puis-je le faire faire défiler de haut en bas avec l'inclinaison de haut en bas?

(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);
Était-ce utile?

La solution

Faire la même chose mais avec le paramètre 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);

Autres conseils

Si vous utilisez jQuery, pourquoi ne pas utiliser $ .scrolltop () Pour modifier la valeur de défilement vertical de l'élément?

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top