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

标签 animation highcharts

我有一个页面,分为通过 anchor 访问的部分。
有没有办法让 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]
        }]

    });
});

最佳答案

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

如果用户单击 skills,此方法也有效。链接在顶部。

你要小心滚动事件,因为它可能是一个真正的性能瓶颈。我拿了approach John Resig 建议定期检查滚动位置。

Working Demo

$(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
        });
    };
});

关于animation - highcharts:在可见而不是在页面加载时触发动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18364088/

相关文章:

javascript - Node.js/Express 不输出 console.log,highcharts.js 给出未定义的错误

javascript - 柱形图上的 Highcharts 工具提示

python - 如何将条形图添加到基于 Django 的数据库?

python - 在 python 中使用来自网络的图像

ios - 在动画完成后需要返回的函数中嵌入 UIView 动画

python - 使用 matplotlib.animation.FuncAnimation 制作具有周期性边界的多车道高速公路的动画

css - Safari 错误?使用 GSAP 翻译表行组使标题跳到底部

android - 动画 layout_weight 变化

javascript - Highcharts 错误 csv.replace 不是函数

javascript - HighChart 图表在 Xaxis 上对齐错误..无法在 JSFiddle 上重现