Fullpage.js / enhance SlimScroll to detect visible content for animations

When using Fullpage.js, overflowing section / slide content will be made scrollable with Slimscroll. If you want to use addons that rely on the natural scroll event, these will fail with Slimscroll. One of the candidates breaking is Scrollreveal.js for example.

I decided to work around that, to allow animations to be triggered when elements become visible or invisible  to the viewport.

ADDITIONAL PLUGINS

You could code the viewport visibility check yourself or use the “visible” jquery plugin.

“This is a jQuery plugin which allows us to quickly check if an element is within the browsers visual viewport, regardless of the scroll position. If a user can see this element, the function will return true.” jquery-visible on Github

EXTENDING SLIMSCROLL WITHIN FULLPAGE.JS

The idea was to extend or hook into slimscroll without touching the fullpage.js codebase. Fullpage.js wraps overflowing content within a fp-scrollable container. Slimscroll itself provides events to track your position within the scrollable area in pixels or when top / bottom have been reached.

TRACK TOP / BOTTOM

  1. $(selector).slimScroll().bind('slimscroll', function(e, pos){
  2.     console.log("Reached " + pos");
  3. });

TRACK POSITION

  1. $(selector).slimScroll().bind('slimscrolling', function(e, pos){
  2.     console.log("Reached " + pos");
  3. });

MAIN GOAL

  1. Hook into fullpage.js
  2. Track viewport & visible elements (jquery-visible)
  3. Track direction of scroll
  4. Get current position
  5. Assign classes to elements to trigger animations

MY SOLUTION

This is just a crude and simple starting point, but should give you the basic idea. This still needs some throttling, so that its not called on every scroll position.

  1. var currentScrollDirection = "down";
  2. var currentScrollPosition  = 0;
  3. $("body").delegate(".fp-scrollable", "slimscrolling", function(e, pos){
  4.  
  5.  // Top has been reached                                               
  6.  if(cPos == 0){
  7.   currentScrollDirection = "down";
  8.  }
  9.  
  10.  // Scrolling up or down?
  11.  if(currentScrollPosition > pos){
  12.    currentScrollDirection = "up"
  13.  }else{
  14.    currentScrollDirection = "down"
  15.  }
  16.  
  17.  // Store current position to compare on next check
  18.   currentScrollPosition = pos;
  19.  
  20.  // Our scroll element
  21.  var element = $(this);
  22.  
  23.  // Using the class "reveal" for all elements to track within
  24.  // Loop over elements on scroll
  25.  
  26.  element.find(".reveal").each(function(){
  27.  
  28.  // using the visible plugin                                           
  29.  if ($(this).visible(true) ) {
  30.   // add inView and direction classes
  31.   $(this).addClass("inView")
  32.   .removeClass("outView")
  33.   .removeClass("up")
  34.   .removeClass("down")
  35.   .addClass(direction);
  36.  }else{
  37.   // add outView class and handle other classes
  38.   $(this).addClass("outView")
  39.   .removeClass("up")
  40.   .removeClass("down")
  41.   .removeClass("inView");
  42.                                                                        
  43.  }
  44. })

HTML EXAMPLE

  1. <div class="scrollArea">
  2.  <div id="elOne" class="reveal">
  3.  </div>
  4.  <div id="elTwo" class="reveal">
  5.  </div>
  6.  <div id="elThree" class="reveal">
  7.  </div>
  8. </div>

CSS EXAMPLE

Very basic idea to get some transitions working.

  1. .inView.down{
  2. border-left:4px solid red;
  3. opacity: 1;
  4. animation: scaleUpDown 1.5s ease both;
  5. }
  6. .inView.up{
  7. border-left:4px solid blue;
  8. opacity: 1;
  9. }
  10.  
  11. .outView{
  12. opacity: .6;
  13. -webkit-transition:opacity 1500ms ease-out;
  14. -moz-transition:opacity 1500ms ease-out;
  15. -o-transition:opacity 1500ms ease-out;
  16. transition:opacity 1500ms ease-out;
  17. }
  18.  
  19. @keyframes scaleUpDown {
  20.  from{ opacity: 0; transform: scale(0.2) rotate(30deg) translate(50%)};
  21.  to { opacity: 1; transform: scale(1.0) rotate(0deg) translate(50%)};
  22. }

Hopefully Fullpage.js will be switching to iScroll natively in the future, as it provides far more options to handle scrollable areas. You can use iScroll now, but you have to disable the scrolling feature within Fullpage.js and call iScroll yourself. Not that difficult to do :)

There is also a WordPress Plugin that wraps Fullpage.js natively with a nice interface (WP_Fullpage) and my upcoming Visual Composer integration, which already uses iScroll :)

Enjoy coding ….

Alex

I am a full-stack developer. I love programming,  design and know my way around server architecture as well.  I would never feel complete, with one of these missing. I have a broad range of interests, that’s why I constantly dive into new technologies and expand my knowledge where ever required. Technologies are evolving fast and I enjoy using the latest. Apart from that, I am a peace loving guy who tries to have people around him that think the same.  I truly believe in the principle: “If you help someone, someone will help you, when you need it."

Recent Posts

Particle Network Animations in Javascript

What are particle animations? Particle network animations in JavaScript typically involve creating visual representations of… Read More

3 days ago

B&B / Hotel Booking Solutions for WordPress | 2024

BOOKING SOLUTIONS 202x This is my take on a subset of booking, appointment, PMS or… Read More

1 month ago

WordPress Cron + WP-CLI + Ntfy

THE GOAL Create a system cron for WordPress, that is accessible and can be easily… Read More

2 months ago

2024 is here and now :)

2024, what's cooking? Slowly getting into the 2024 spirit. 3 projects coming to a close… Read More

4 months ago

2023 ends and whats next !

Short look back at 2023 This has been a busy and interesting year. I am… Read More

4 months ago

cubicFUSION Grid Tweaker – Elementor Grid made easy.

Elementor Pro provides grid containers as an experimental feature. The options provided are limited, when… Read More

5 months ago