i have a page separated out into sections that are accessed via anchors. is there a way to have the highcharts animation fire when its particular section becomes visible instead of on page load?
http://jsfiddle.net/YFMSb/2/ (the chart is under 'skills', so would like its initial animation to occur when that section of the page is brought up. does not need to re-animate during subsequent visits to/through the section)
$(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]
}]
});
});
Attach a scroll event listener to the window that detects when you get close to the
skills
section. When you create the chart, remove the scroll listener to ensure that the chart is only created once. This will also help with performance: no reason to listen to an event that we aren't going to respond to anymore.This approach also works if the user clicks the
skills
link at the top.You want to be careful with the scroll event as it can be a real performance bottleneck. I took the approach suggested by John Resig to check the scroll position at regular intervals.
Working Demo
You have to create the chart on click of 'skills'. jsfiddle below. http://jsfiddle.net/YFMSb/6/
createChart() basically defines your chart.
I'm supposing with "section becomes visible" you meant a scroll event.
This is the most basic code to make it work:
http://jsfiddle.net/YFMSb/12/