Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

LazyLoad Javascript

Hi There. I am using http://justgage.com/ to load some circle charts into my site.

I would like to create a visually appealing effect so that when the user scrolls to that part of my site where the graphs are, the graphs load for them so that they can see it. How can this be achieved? Loading JavaScript on scroll?

1 Answer

Thank you for your reply! Do you have any idea how to implement this with a script? I only see how to load those text popups with waypoints? How can I achieve this with loading a script say 200px from the top?

Jeremy Germenis
Jeremy Germenis
29,854 Points

In the given example on the home page:

$('.thing').waypoint(function(direction) {
  alert('Top of thing hit top of viewport.');
});

You would just replace the alert call with a function call. Place your script code in a function and load it when the page loads.

$('.thing').waypoint(function(direction) {
  myFunction();
});

Thanks for your response!

Unfortunately, I still cannot get my script to load.

          $('#example-basic').waypoint(function(direction) {
          window.onload = function(){

                                 var g1 = new JustGage({
            id: "g1", 
            value: 85, 
            min: 0,
            max: 100,
            gaugeWidthScale: 0.1,
            title: "Customer Sales Increase",
            label: "",  
            showMinMax: true,
            gaugeColor: "#fff",
            levelColors: ["#66B9DC"],
            showInnerShadow: true,        
            startAnimationTime: 1300,
            startAnimationType: "bounce",         
          });
                      });

I get errors in my browser when inspecting element in chrome saying :

Uncaught TypeError: undefined is not a function waypoints.min.js:8

Uncaught ReferenceError: $ is not defined index.php:6

Jeremy Germenis
Jeremy Germenis
29,854 Points

remove the line window.onload = function(){ since you are not loading the function when the window loads, you may also need to remove the code var g1 = new