{"id":5099,"date":"2017-12-27T01:00:00","date_gmt":"2017-12-27T00:00:00","guid":{"rendered":"https:\/\/alvarotrigo.com\/epa\/how-create-jquery-scrolltop-animation-without-jquery\/"},"modified":"2024-07-25T14:23:42","modified_gmt":"2024-07-25T12:23:42","slug":"how-create-jquery-scrolltop-animation-without-jquery","status":"publish","type":"post","link":"https:\/\/alvarotrigo.com\/blog\/how-create-jquery-scrolltop-animation-without-jquery\/","title":{"rendered":"Create jQuery scrollTop animation without jQuery"},"content":{"rendered":"\n            <p>Have you ever tried to replicate the smooth scroll animation of <strong>jQuery scrollTop<\/strong>  but <strong>without jQuery<\/strong>? With plain \/ vanilla Javascript?<\/p>\n<p>I did, and let me tell you it is not something you find on the &quot;You don&#8217;t need jQuery&quot; webpages.<br>\nAnd it even gets more complicated when you want to apply a specific jQuery easing effect, fire a callback on finish, being able to stop the animation at any moment etc.<\/p>\n<p>You could go and make use of a smooth scrolling library like <a href=\"https:\/\/github.com\/cferdinandi\/smooth-scroll\" target=\"_blank\" rel=\"noopener nofollow\">this<\/a> but it is 5Kb with polyfills and minified and it does a lot more than scrolling. It deals with link anchors and the text within them and it has quite a lot of logic dedicated at that. Something perhaps unnecessary if you just want to scroll smootly.<\/p>\n<h2 class=\"wp-block-heading\" id=\"lightweight-javascript-effect\">Lightweight Javascript effect<\/h2>\n<p>If you want something much smaller to use within your code, here&#8217;s hat I&#8217;ve used for my <a href=\"https:\/\/alvarotrigo.com\/fullPage\/\">fullPage.js library<\/a>.<\/p>\n<p>It is only 0.4Kb minified and pretty straight forward.<\/p>\n<p>I&#8217;ve made it available at Github just before writing this article so you can have access to it all as well as the minified files.<\/p>\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a href=\"https:\/\/alvarotrigo.com\/skrollTop\/\" target=\"_blank\" title=\"See Demo\" class=\"wp-block-button__link wp-element-button\" rel=\"noopener\">See Demo<\/a><\/div>\n<div class=\"wp-block-button\"><a href=\"https:\/\/github.com\/alvarotrigo\/skrollTop.js\" target=\"_blank\" title=\"View on GitHub\" rel=\"noopener nofollow\" class=\"wp-block-button__link wp-element-button\">View on GitHub<\/a><\/div>\n<\/div>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-comment\">\/*!\n * skrollTop 0.0.1\n * https:\/\/github.com\/alvarotrigo\/skrollTop.js\n * @license MIT\n *\n * Copyright (C) 2018 alvarotrigo.com - A project by Alvaro Trigo\n *\/<\/span>\n(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">root, factory<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">if<\/span> ( <span class=\"hljs-keyword\">typeof<\/span> define === <span class=\"hljs-string\">'function'<\/span> &amp;&amp; define.amd ) {\n    define(&#91;], (<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\"><\/span>) <\/span>{\n      <span class=\"hljs-keyword\">return<\/span> factory(root, root.document);\n    }));\n  } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> ( <span class=\"hljs-keyword\">typeof<\/span> exports === <span class=\"hljs-string\">'object'<\/span> ) {\n    <span class=\"hljs-built_in\">module<\/span>.exports = factory(root, root.document);\n  } <span class=\"hljs-keyword\">else<\/span> {\n    root.skrollTop = factory(root, root.document);\n  }\n})(<span class=\"hljs-keyword\">typeof<\/span> global !== <span class=\"hljs-string\">'undefined'<\/span> ? global : <span class=\"hljs-keyword\">typeof<\/span> <span class=\"hljs-built_in\">window<\/span> !== <span class=\"hljs-string\">'undefined'<\/span> ? <span class=\"hljs-built_in\">window<\/span> : <span class=\"hljs-keyword\">this<\/span>, (<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">window, document<\/span>) <\/span>{\n    <span class=\"hljs-string\">'use strict'<\/span>;\n\n    <span class=\"hljs-keyword\">var<\/span> g_activeAnimation;\n    <span class=\"hljs-keyword\">var<\/span> self = {};\n\n    <span class=\"hljs-comment\">\/\/easeInOutCubic animation included in the library<\/span>\n    <span class=\"hljs-built_in\">Math<\/span>.easeInOutCubic = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">t, b, c, d<\/span>) <\/span>{\n        <span class=\"hljs-keyword\">if<\/span> ((t\/=d\/<span class=\"hljs-number\">2<\/span>) &lt; <span class=\"hljs-number\">1<\/span>) <span class=\"hljs-keyword\">return<\/span> c\/<span class=\"hljs-number\">2<\/span>*t*t*t + b;<span class=\"hljs-keyword\">return<\/span> c\/<span class=\"hljs-number\">2<\/span>*((t-=<span class=\"hljs-number\">2<\/span>)*t*t + <span class=\"hljs-number\">2<\/span>) + b;\n    };\n\n    self.stop = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\n        g_activeAnimation = <span class=\"hljs-literal\">false<\/span>;\n    };\n\n    <span class=\"hljs-comment\">\/**\n    * Simulates the animated scrollTop of jQuery. Used when css3:false or scrollBar:true or autoScrolling:false\n    * http:\/\/stackoverflow.com\/a\/16136789\/1081396\n    *\/<\/span>\n   self.scrollTo = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\">params<\/span>) <\/span>{\n       <span class=\"hljs-keyword\">var<\/span> element = <span class=\"hljs-keyword\">typeof<\/span> params.element !== <span class=\"hljs-string\">'undefined'<\/span> ? params.element : <span class=\"hljs-built_in\">window<\/span>;\n       <span class=\"hljs-keyword\">var<\/span> to = params.to;\n       <span class=\"hljs-keyword\">var<\/span> duration = <span class=\"hljs-keyword\">typeof<\/span> params.duration !== <span class=\"hljs-string\">'undefined'<\/span> ? params.duration : <span class=\"hljs-number\">700<\/span>;\n       <span class=\"hljs-keyword\">var<\/span> callback = <span class=\"hljs-keyword\">typeof<\/span> params.callback !== <span class=\"hljs-string\">'undefined'<\/span> ? params.callback : <span class=\"hljs-literal\">null<\/span>;\n       <span class=\"hljs-keyword\">var<\/span> easing = <span class=\"hljs-keyword\">typeof<\/span> params.easing !== <span class=\"hljs-string\">'undefined'<\/span> ? params.easing : <span class=\"hljs-built_in\">Math<\/span>.easeInOutCubic;\n\n       <span class=\"hljs-keyword\">var<\/span> start = (<span class=\"hljs-built_in\">window<\/span>.pageYOffset || <span class=\"hljs-built_in\">document<\/span>.documentElement.scrollTop)  - (<span class=\"hljs-built_in\">document<\/span>.documentElement.clientTop || <span class=\"hljs-number\">0<\/span>);\n       <span class=\"hljs-keyword\">var<\/span> change = to - start;\n       <span class=\"hljs-keyword\">var<\/span> currentTime = <span class=\"hljs-number\">0<\/span>;\n       <span class=\"hljs-keyword\">var<\/span> increment = <span class=\"hljs-number\">16<\/span>; <span class=\"hljs-comment\">\/\/same amount of milliseconds as requestAnimationFrame<\/span>\n       g_activeAnimation = <span class=\"hljs-literal\">true<\/span>;\n\n       <span class=\"hljs-keyword\">var<\/span> animateScroll = <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n            <span class=\"hljs-comment\">\/\/in case we want to stop it from other function whenever we want<\/span>\n           <span class=\"hljs-keyword\">if<\/span> (g_activeAnimation) {\n               currentTime += increment;\n               element.scrollTo(<span class=\"hljs-number\">0<\/span>, easing(currentTime, start, change, duration));\n\n               <span class=\"hljs-keyword\">if<\/span> (currentTime &lt; duration) {\n                   setTimeout(animateScroll, increment);\n               } <span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (callback){\n                   callback();\n               }\n           }<span class=\"hljs-keyword\">else<\/span> <span class=\"hljs-keyword\">if<\/span> (currentTime &lt; duration &amp;&amp; callback){\n               callback();\n           }\n       };\n\n       animateScroll();\n   };\n\n   <span class=\"hljs-keyword\">return<\/span> self;\n}));\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<p>In order to use it just call <code>skrollTop.scrollTo<\/code> like this:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">document<\/span>.getElementById(<span class=\"hljs-string\">'scrollMe'<\/span>).addEventListener(<span class=\"hljs-string\">'click'<\/span>, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>)<\/span>{\n    skrollTop.scrollTo({\n        <span class=\"hljs-attr\">to<\/span>: <span class=\"hljs-number\">800<\/span>,\n        <span class=\"hljs-attr\">duration<\/span>: <span class=\"hljs-number\">4000<\/span>,\n        <span class=\"hljs-attr\">callback<\/span>: <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n           <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">\"va\"<\/span>);\n        }\n    });\n});\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<h2 class=\"wp-block-heading\" id=\"how-to-add-jquery-easing-effects\">How to add jQuery easing effects<\/h2>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" data-src=\"..\/assets\/imgs\/2018-01-28\/jquery-scrollTop-easing.gif\" alt=\"jQuery UI scrollTop easing effects\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" \/><\/figure>\n<p>jQuery provides a small set of animation effects they call <a href=\"https:\/\/api.jqueryui.com\/easings\/\" target=\"_blank\" rel=\"noopener nofollow\"><strong>Easings Functions<\/strong><\/a>.<br>\nIf you want to have access to more effects, then you would have to go for jQuery UI<\/p>\n<p>The library includes the <strong>easeInOutCubic<\/strong> effect, but you can add any others provided by jQuery by using the <a href=\"https:\/\/github.com\/alvarotrigo\/fullPage.js\/blob\/2.9.7\/vendors\/jquery.easings.min.js\" target=\"_blank\" rel=\"noopener nofollow\">vendor file <code>easings.min.js<\/code><\/a> (only 12Kb) that I also provide in <a href=\"https:\/\/github.com\/alvarotrigo\/fullPage.js\" target=\"_blank\" rel=\"noopener nofollow\">fullPage.js<\/a>. Then just passs the name of the effect as an option to the library to specify which easing you want to use:<\/p>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- Optional ! --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/javascript\"<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"..\/easings.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n<span class=\"hljs-comment\">&lt;!-- end of optional --&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\/javascript\"<\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"..\/skrollTop.js\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">    skrollTop.scrollTo({\n        <span class=\"hljs-attr\">to<\/span>: <span class=\"hljs-number\">800<\/span>,\n        <span class=\"hljs-attr\">easing<\/span>: <span class=\"hljs-built_in\">window<\/span>.easings.easeOutBounce, <span class=\"hljs-comment\">\/\/ &lt;-- here!<\/span>\n        <span class=\"hljs-attr\">duration<\/span>: <span class=\"hljs-number\">600<\/span>,\n        <span class=\"hljs-attr\">callback<\/span>: <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span>(<span class=\"hljs-params\"><\/span>) <\/span>{\n           <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">\"finished!\"<\/span>);\n        }\n    });\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>","protected":false},"excerpt":{"rendered":"<p>Have you ever tried to replicate the smooth scroll animation of jQuery scrollTop but without jQuery? With plain \/ vanilla Javascript?<\/p>\n","protected":false},"author":1,"featured_media":5098,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[107],"tags":[40,18],"class_list":["post-5099","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-front-end","tag-jquery"],"acf":[],"_links":{"self":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5099","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/comments?post=5099"}],"version-history":[{"count":4,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5099\/revisions"}],"predecessor-version":[{"id":15496,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/posts\/5099\/revisions\/15496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media\/5098"}],"wp:attachment":[{"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/media?parent=5099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/categories?post=5099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alvarotrigo.com\/blog\/wp-json\/wp\/v2\/tags?post=5099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}