沿着路径滚动,曲线和线

时间:2013-01-31 02:37:23

标签: javascript jquery path scroll jquery-animate

有人知道一个更简单(不太臃肿)的函数/插件来计算自定义滚动路径,就像来自Joel Besada的jquery.scrollpath一样吗?

我需要的是让DOM节点沿预定路径移动的方法。我知道Math.cos& Math.sin(沿着曲线移动)但我不是真正的数学天才。

1 个答案:

答案 0 :(得分:0)

好的,明白了!

// Shamelessly stolen and ported from https://github.com/weepy/jquery.path
$.fn.arc = function (options) {
    options = $.extend({}, {
        center: [0, 0],
        radius: 0,
        start: 0,
        end: 0,
        dir: 1,
        rotate: true
    }, options);

    while (options.start > options.end && options.dir > 0) {
        options.start -= 360;
    }

    while (options.start < options.end && options.dir < 0) {
        options.start += 360;
    }

    var css = {};
    return this.each(function(){
        var self = $(this),
            win = $(window),
            doc = $(document);

        win.scroll(function(){
            var now = 1 - (1 * win.scrollTop() / (doc.height() - win.height())),
                a = (options.start * (now) + options.end * (1 - (now))) * Math.PI / 180;

            css.left = (Math.sin(a) * options.radius + options.center[0] + 0.5) | 0;
            css.top = (Math.cos(a) * options.radius + options.center[1] + 0.5) | 0;

            if(options.rotate){
                css.transform = "rotate(" + Math.atan2(options.center[1] - css.top, options.center[0] - css.left) + "rad)";
            }
            self.css(css);
        });
    });
};