如何使这个javascript函数等到我在页面上滚动x像素?

时间:2014-02-23 02:12:17

标签: javascript jquery

Noob在这里......感谢您的帮助!

我希望此功能(带有动画百分比边框的圆圈)等到我在页面的那一部分开始之前。我只是不确定在哪里放置代码。

(function($){
    $.fn.percentPie = function(options){

        var settings = $.extend({
            width: 100,
            trackColor: "EEEEEE",
            barColor: "777777",
            barWeight: 30,
            startPercent: 0,
            endPercent: 1,
            fps: 60
        }, options);

        this.css({
            width: settings.width,
            height: settings.width
        });

        var that = this,
            hoverPolice = false,
            canvasWidth = settings.width,
            canvasHeight = canvasWidth,
            id = $('canvas').length,
            canvasElement = $('<canvas id="'+ id +'" width="' + canvasWidth + '" height="' + canvasHeight + '"></canvas>'),
            canvas = canvasElement.get(0).getContext("2d"),
            centerX = canvasWidth/2,
            centerY = canvasHeight/2,
            radius = settings.width/2 - settings.barWeight/2;
            counterClockwise = false,
            fps = 2000 / settings.fps,
            update = .01;
            this.angle = settings.startPercent;

        this.drawArc = function(startAngle, percentFilled, color){
            var drawingArc = true;
            canvas.beginPath();
            canvas.arc(centerX, centerY, radius, (Math.PI/180)*(startAngle * 360 - 90), (Math.PI/180)*(percentFilled * 360 - 90), counterClockwise);
            canvas.strokeStyle = color;
            canvas.lineWidth = settings.barWeight;
            canvas.stroke();
            drawingArc = false;
        }

        this.fillChart = function(stop){
            var loop = setInterval(function(){
                hoverPolice = true;
                canvas.clearRect(0, 0, canvasWidth, canvasHeight);

                that.drawArc(0, 360, settings.trackColor);
                that.angle += update;
                that.drawArc(settings.startPercent, that.angle, settings.barColor);

                if(that.angle > stop){
                    clearInterval(loop);
                    hoverPolice = false;
                }
            }, fps);
        }

        this.mouseover(function(){
            if(hoverPolice == false){
                that.angle = settings.startPercent;
                that.fillChart(settings.endPercent);
            }
        });

        this.fillChart(settings.endPercent);
        this.append(canvasElement);
        return this;
    }
}(jQuery));

1 个答案:

答案 0 :(得分:0)

如果您不介意使用jquery,可以收听滚动事件并查看页面顶部。

$(window).on('scroll', function() {
    if ($(this).scrollTop() > partOfThePageYourCirclesFire ) {
        // fire cirlces
    }
});