JAVASCRIPT - Infinite scrolling using AJAX


SUBMITTED BY: efbee

DATE: Oct. 5, 2016, 7:06 p.m.

FORMAT: JavaScript

SIZE: 978 Bytes

HITS: 1195

  1. <script>
  2. //init controller
  3. var controller = new ScrollMagic.Controller({triggerElement: ".dynamiccontent #loader", triggerHook: "onEnter"})
  4. .addTo(controller)
  5. .on("enter", function (e)
  6. {
  7. if(!$("#loader").hasClass("active")){
  8. $("#loader").addClass("active");
  9. if(console){
  10. console.log("loading new items");
  11. }
  12. //simulate ajax call to add content usingĀ  the function below
  13. setTimeout(addBoxes, 1000, 9);
  14. }
  15. });
  16. //pseudo function to add new content . In real life it would be done through an ajax request.
  17. function addBoxes(amount){
  18. for (i=1;i<=amount;i++){
  19. var randomColor = '#'+ ('00000'+(Math.random())*0xFFFFFF<<0).toString(16)).slice(-6);
  20. $("<div></div>")
  21. .addClass("box1")
  22. .css(background-color, randomColor)
  23. .appendTo(".dynamicContent #content");
  24. }
  25. //loading dont -> revert to normal state
  26. scene.update();//make sure the scene gets the new start position
  27. $("#loader").removeClass("active");
  28. }
  29. //add some boxes to start with
  30. addBoxes(18);
  31. </script>

comments powered by Disqus