Enable smooth scrolling for my website in all browsers

前端 未结 6 884
醉酒成梦
醉酒成梦 2020-12-02 09:15

I\'m developing a parallax scrolling website using the Stellar and Skrollr libraries. The website behaves perfectly in Firefox because of Firefox\'s smooth scrolling feature

6条回答
  •  醉酒成梦
    2020-12-02 10:04

    I had not a lot of time, but I tried to write a (cross browser & dirty) smooth scrolling functionality on the fly. When you stop scrolling it smoothly decelerates. You can rewrite it a little bit so it fits your needs.

    Give it a try here:

    Smooth scrolling:

    function getScrollTop(){
        if(typeof pageYOffset!= 'undefined'){
            //most browsers except IE before #9
            return pageYOffset;
        } else {
            var B = document.body; //IE 'quirks'
            var D = document.documentElement; //IE with doctype
            D = (D.clientHeight) ? D : B;
            return D.scrollTop;
        }
    }
    
    var timeouts = [];
    var scrolling = false;
    var scroller;
    var scrollTop = getScrollTop();
    var timeMs;
    var alter = false;
    var speed = 5;
    window.onscroll = function() {
        if(alter) {
            var timeDif = new Date().getMilliseconds() - timeMs;
            speed = 5 - (timeDif / 50);
            console.log(speed);
        }
        timeMs = new Date().getMilliseconds();
        alter = !alter;
        var scrollDirection = getScrollTop() - scrollTop;
        scrollDirection = scrollDirection / Math.abs(scrollDirection);
        scrollTop = getScrollTop();
        clearTimeout(scroller);
        scroller = setTimeout(function(){
            console.log('smooth scrolling active');
            if(!scrolling) {
                timeouts.length = 0;
                scrolling = true;
                var steps = 50;
                var delay = 6;
                for(var i = 0; i < steps; i++) {
                    (function(i){
                        var timeout = setTimeout(function(){
                            var perc = i / steps; 
                            var val = (perc == 1) ? 1 : (-Math.pow(2, -10 * perc) + 1); 
                            var scrollY = val * speed * scrollDirection;
                            window.scrollTo(0, getScrollTop() + scrollY);
                            setTimeout(function(){
                                if(i == (steps - 1)) scrolling = false;
                            }, steps * delay);
                        }, (i * delay));
                        timeouts.push(timeout);
                    })(i);
                }
            }
        }, 50);
    };
    

    http://jsfiddle.net/ubawR/4/

提交回复
热议问题