highcharts:在可见而不是页面加载时触发动画

pix*_*oco 4 animation highcharts

我有一个页面分隔成通过锚点访问的部分.有没有办法让highcharts动画在其特定部分变为可见而不是页面加载时触发?

http://jsfiddle.net/YFMSb/2/(该图表位于"技能"下,因此希望在页面的该部分显示时进行初始动画.在后续访问期间不需要重新制作动画. /通过部分)

$(function () {
$('#container').highcharts({
    chart: {
            type: 'bar',
            spacingTop: 0
        },

        title: {
            text: ''
        },

        xAxis: {
            labels: {
                enabled: false
            }
        },

        yAxis: {
            min: 0,
            max: 100,
            title: {
                text: ''
            },
            labels: {
                enabled: false
            }
        },

        plotOptions: {
            series: {
                stacking: 'normal'
            }
        },


        tooltip: {
            formatter: function() {
               return '<b>'+ this.series.name +'</b>';
            }
        },

        series: [{
            name: 'clean',
            data: [10],
        }, {
            name: 'eat',
            data: [10]
        }, {
            name: 'sleep',
            data: [40]
        }, {
            name: 'work',
            data: [30]
        }, {
            name: 'play',
            data: [10]
        }]

    });
});
Run Code Online (Sandbox Code Playgroud)

cfs*_*cfs 8

将滚动事件侦听器附加到窗口,该窗口检测您何时接近该skills部分.创建图表时,请删除滚动侦听器以确保仅创建一次图表.这也有助于提高性能:没有理由倾听我们不再响应的事件.

如果用户单击skills顶部的链接,此方法也适用.

您需要小心滚动事件,因为它可能是一个真正的性能瓶颈.我采用John Resig建议的方法定期检查滚动位置.

工作演示

$(function () {
    var $window = $(window),
        didScroll = false,
        skillsTop = $('#skills').offset().top - 40; //the point at which we will create the chart

    $window.on('scroll', function () {
        //detected a scroll event, you want to minimize the code here because this event can be thrown A LOT!
        didScroll = true;
    });

    //check every 250ms if user has scrolled to the skills section
    setInterval(function () {
        if (didScroll) {
            didScroll = false;
            if ($window.scrollTop() >= skillsTop) {
                createChart();
            }
        }
    }, 250);

    function createChart() {
        $window.off('scroll'); //remove listener that will create chart, this ensures the chart will be created only once

        $('#container').highcharts({
            //chart configuration here
        });
    };
});
Run Code Online (Sandbox Code Playgroud)