Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

animated scroll to anchor without jquery

function animate(elem, style, unit, from, to, time, prop) {
    if (!elem) {
        return;
    }
    var start = new Date().getTime(),
        timer = setInterval(function () {
            var step = Math.min(1, (new Date().getTime() - start) / time);
            if (prop) {
                elem[style] = (from + step * (to - from))+unit;
            } else {
                elem.style[style] = (from + step * (to - from))+unit;
            }
            if (step === 1) {
                clearInterval(timer);
            }
        }, 25);
    if (prop) {
          elem[style] = from+unit;
    } else {
          elem.style[style] = from+unit;
    }
}

window.onload = function () {
    var target = document.getElementById("div5");
    animate(document.scrollingElement || document.documentElement, "scrollTop", "", 0, target.offsetTop, 2000, true);
};
Comment

animated scroll to anchor without jquery

const requestAnimationFrame = window.requestAnimationFrame ||
    window.webkitRequestAnimationFrame ||
    window.mozRequestAnimationFrame ||
    window.oRequestAnimationFrame ||
    window.msRequestAnimationFrame;

function scrollTo(to) {
    const start = window.scrollY || window.pageYOffset
    const time = Date.now()
    const duration = Math.abs(start - to) / 3;

    (function step() {
        var dx = Math.min(1, (Date.now() - time) / duration)
        var pos = start + (to - start) * easeOutQuart(dx)

        window.scrollTo(0, pos)

        if (dx < 1) {
            requestAnimationFrame(step)
        }
    })()
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to convert variable to string in jquery 
Javascript :: exit forEach when null javascript 
Javascript :: midpointrounding.awayfromzero javascript 
Javascript :: removing element at index without changing the original array 
Javascript :: signin with google widget floating automatically 
Javascript :: log errors react 
Javascript :: setInterval issue, if i turn on new tap, that can be slower 
Javascript :: useSate object 
Javascript :: how to detect keyboard layout js 
Javascript :: kjk 
Javascript :: material ui hide asterisk 
Javascript :: announcement for all server bot is in 
Javascript :: route edit button in laravel ajax 
Javascript :: package json replace to dev dependencies 
Javascript :: app scrip sheet cell boarder 
Javascript :: Destructing variable assignment 
Javascript :: how to generate an onclose jest 
Javascript :: how to chnge line in browser js 
Javascript :: Contentful Migration - Transform Entires 
Javascript :: convert space to dash/hyphen javascript regex 
Javascript :: es6-map-an-array-of-objects-to-return-an-array-of-objects-with-new-keys 
Javascript :: find js like 
Javascript :: createPortal usage 
Javascript :: send keypress from iframe to parent 
Javascript :: flatpicker js init 
Javascript :: get player on seat 
Javascript :: mongoose populate not working 
Javascript :: sequelize read from moel 
Javascript :: too many rerenders 
Javascript :: zgadfgad 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =