JAVASCRIPT   36

back to top js

Guest on 24th June 2022 03:18:41 PM

  1. $(document).on('ready', function() {
  2.         // browser window scroll (in pixels) after which the "back to top" link is shown
  3.         var offset = 300,
  4.                 //browser window scroll (in pixels) after which the "back to top" link opacity is reduced
  5.                 offset_opacity = 1200,
  6.                 //duration of the top scrolling animation (in ms)
  7.                 scroll_top_duration = 700,
  8.                 //grab the "back to top" link
  9.                 $back_to_top = $('.cd-top');
  10.  
  11.         //hide or show the "back to top" link
  12.         $(window).scroll(function(){
  13.                 ( $(this).scrollTop() > offset ) ? $back_to_top.addClass('cd-is-visible') : $back_to_top.removeClass('cd-is-visible cd-fade-out');
  14.                 if( $(this).scrollTop() > offset_opacity ) {
  15.                         $back_to_top.addClass('cd-fade-out');
  16.                 }
  17.         });
  18.  
  19.         //smooth scroll to top
  20.         $back_to_top.on('click', function(event){
  21.                 event.preventDefault();
  22.                 $('body,html').animate({
  23.                         scrollTop: 0 ,
  24.                         }, scroll_top_duration
  25.                 );
  26.         });
  27.  
  28. });

Raw Paste


Login or Register to edit or fork this paste. It's free.