How to make slow scroll speed

ScrollTop is a jquery plugin (go to the top of the page), trying to make the scroll speed slow but not working. I changed scrollSpeed : 'fast',to scrollSpeed : 'slow',, but still quickly, nothing has changed.

JS:

$.fn.extend({

    addScrollTop: function(options) {

        var defaults = {
                useObjWindow : false,
                scrollSpeed : 'fast',
                zIndex: '99'
            }

            var options = $.extend(defaults, options);  

        if($('body').find('.scrollTop-btn').length == 0) {
            $('body').append('<div class="scrollTop-btn" style="display:none;"><i class="fa fa-chevron-up"></i></div>');
        }

        if(options.useObjWindow) {
            var parentWindow = this;
            var scrollWindow = this;
        }
        else {
            var parentWindow = window;
            var scrollWindow = 'html, body';
        }

        $(document).ready(function() {

            $('.scrollTop-btn').on('click', function() {
                $(scrollWindow).animate({scrollTop:0}, options.scrollSpeed);
            });

            $(parentWindow).scroll(function() { 
                $('.scrollTop-btn').hide();
                var aTop = $('.scrollTop-btn').height() + 20;

                if($(this).scrollTop() >= (aTop + 20)) {
                    $('.scrollTop-btn').css('z-index', options.zIndex);
                    $('.scrollTop-btn').show();
                }
                else {
                    if($('.scrollTop-btn').is(":visible")) {
                        $('.scrollTop-btn').hide();
                    }
                }

            });


        });
    }

});

Call:

jQuery(document).ready(function() {
jQuery("body").addScrollTop();
});

How to make it slower or smoother when it returns?

+4
source share
3 answers

use jquery animate ()

$('html,body').animate({ scrollTop: 0 }, 'slow');

refer to this stack overflow question

+10
source

If you want, you can configure how much time you want the "scroll" to last. Or do something else when the scroll effect ends.

I have: <a href="#" class="scrollToTop">

"begin"

$('.scrollToTop').on('click', function(event){
      event.preventDefault();
      $('html, body').stop().animate({scrollTop: $('.beginning').offset().top}, 500);
 });

, 500. , , .

http://api.jquery.com/animate/

+5

Only with CSS:

html {
  scroll-behavior: smooth;
}
0
source

Source: https://habr.com/ru/post/1615691/


All Articles