Scroll-Snap (jQuery): Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „ Einfaches aber effektives jQuery Skript, welches Scroll-Snap bis IE 9 abbildet. Funktioniert in komplexen HTML Setups besser. '''jQuery Plugin & Beispiel'''…“)
 
 
(Eine dazwischenliegende Version desselben Benutzers wird nicht angezeigt)
Zeile 1: Zeile 1:
  
Einfaches aber effektives jQuery Skript, welches Scroll-Snap bis IE 9 abbildet. Funktioniert in komplexen HTML Setups besser.
+
Einfaches aber effektives jQuery Skript, welches Scroll-Snap bis IE 9 abbildet. Funktioniert in komplexen HTML Setups besser als CSS scroll-snap und ist kompatibler.
  
 
'''jQuery Plugin & Beispiel'''
 
'''jQuery Plugin & Beispiel'''
Zeile 97: Zeile 97:
 
   });
 
   });
 
})
 
})
 +
</syntaxhighlight>
 +
 +
<syntaxhighlight lang="css">
 +
body.lock-scroll{
 +
  overflow: hidden;
 +
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Aktuelle Version vom 22. Januar 2021, 10:08 Uhr

Einfaches aber effektives jQuery Skript, welches Scroll-Snap bis IE 9 abbildet. Funktioniert in komplexen HTML Setups besser als CSS scroll-snap und ist kompatibler.

jQuery Plugin & Beispiel

/*! jQuery Scroll Snap 1.0.0 | MIT *
 * https://github.com/jpcurrier/jquery-scroll-snap !*/
 ( function( $ ){
  $.fn.scrollSnap = function( options ){

    // default options
    var settings = $.extend({
      speed: 400
    }, options );

    var scrollStop,
      lastScrollTop =
        window.pageYOffset ||
          document.documentElement.scrollTop ||
            document.body.scrollTop ||
              0;

    function snapScroll( $snap, scroll, direction ){
      if( $( 'body' ).hasClass( 'lock-scroll' ) )
        return;

      var windowHeight = $( window ).height();
      direction = typeof direction !== 'undefined' ? direction : 'up';

      var scrollTo = false;
      $snap.each( function(){
        if(
          scroll <= $( this ).offset().top && (
            // advance
            ( direction == 'down' && scroll >= $( this ).offset().top - ( windowHeight * 2 / 3 ) ) ||
            // stabilize
            ( direction == 'up' && scroll >= $( this ).offset().top - ( windowHeight / 3 ) )
          ) ||
          scroll >= $( this ).offset().top && (
            // advance
            ( direction == 'up' && scroll <= $( this ).offset().top + ( windowHeight * 2 / 3 ) ) ||
            // stabilize
            ( direction == 'down' && scroll <= $( this ).offset().top + ( windowHeight / 3 ) )
          )
        ){
          $( 'body:not( .lock-scroll )' ).addClass( 'lock-scroll' );
          scrollTo = $( this ).offset().top;
        }
      } );

      if( scrollTo !== false ){
        $( 'html, body' ).animate(
          { scrollTop: scrollTo },
          settings.speed,
          function(){
            $( 'body.lock-scroll' ).removeClass( 'lock-scroll' );
            /*setTimeout( function(){
              $( 'body.lock-scroll' ).removeClass( 'lock-scroll' );
            }, 600 );*/
          }
        );
      }
    }

    var $snap = this;
    $( window ).on( 'scroll', function(){
      var scroll =
        window.pageYOffset ||
          document.documentElement.scrollTop ||
            document.body.scrollTop ||
              0;

			var direction = 'up';
      if( scroll > lastScrollTop )
        direction = 'down';

      lastScrollTop = scroll;

      clearTimeout( scrollStop );
      scrollStop = setTimeout( function(){
        snapScroll( $snap, scroll, direction );
      }, 200 );
    } );
  };
} )( jQuery );


$(document).ready(function() {

  // default options
  //$( '.snap' ).scrollSnap();

  // custom options
  $( '.snap' ).scrollSnap({
    speed: 400
  });
})
body.lock-scroll{
  overflow: hidden;
}
<section class="snap" style="height: 50vh;">
LoremIpsum
</section>
<section class="snap" style="height: 50vh;">
LoremIpsum
</section>
<section class="snap" style="height: 50vh;">
LoremIpsum
</section>
<section class="snap" style="height: 50vh;">
LoremIpsum
</section>