首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >沿路径、曲线和线条滚动

沿路径、曲线和线条滚动
EN

Stack Overflow用户
提问于 2013-01-31 10:37:23
回答 1查看 997关注 0票数 0

有没有人知道一个更简单(不那么臃肿)的函数/插件来计算自定义滚动路径,比如Joel Besada的jquery.scrollpath?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-01 02:59:38

好了,明白了!

代码语言:javascript
复制
// 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);
        });
    });
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14617550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档