{"id":6457,"date":"2024-01-11T16:46:00","date_gmt":"2024-01-11T16:46:00","guid":{"rendered":"https:\/\/codehim.com\/?p=6457"},"modified":"2024-01-22T14:47:30","modified_gmt":"2024-01-22T09:47:30","slug":"smooth-scroll-to-top-in-javascript","status":"publish","type":"post","link":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/","title":{"rendered":"Smooth Scroll to Top in JavaScript"},"content":{"rendered":"<p>This JavaScript code snippet helps you to create a smooth scroll to the top button. It dynamically adds back to the top page button on a webpage. You just need to add JS function to your web project, then this plugin dynamically adds back to the top button.<\/p>\n<p>It comes with following features:<b><\/b><\/p>\n<ul>\n<li>3 animate modes:\u00a0<b>[&#8220;normal&#8221;|&#8221;linear&#8221;|false]<\/b><\/li>\n<li>Customize your animation with available settings &#8211; make it snappy or fluent<\/li>\n<li>Double click to skip the animation<\/li>\n<li>Every next single click adds initial speed<\/li>\n<li>Stop scroll animation by dragging down the scroll bar<\/li>\n<li>Stop scroll animation by mouse wheel down<\/li>\n<li>Animated button fade-in-out on scroll<\/li>\n<\/ul>\n<h2>How to Create Smooth Scroll to Top in JavaScript<\/h2>\n<p>1. First of all, add the following CSS styles to your project:<\/p>\n<pre class=\"prettyprint linenums lang-css\">\/* CSS *\/\r\n@import url(https:\/\/fonts.googleapis.com\/css?family=Open+Sans);\r\nbody{\r\n  font-family: 'Open Sans', sans-serif;\r\n  font-size: 1.2rem;\r\n  line-height: 2rem;\r\n  height: 100%;\r\n  position: relative;\r\n}\r\n\r\n.scroll-button{\r\n  box-sizing: border-box;\r\n  font-size: 1.2rem;\r\n  line-height: 2rem;\r\n  padding: 5px;\r\n  width: 80px;\r\n  height: 80px;\r\n  right: 20px;\r\n  bottom: 20px;\r\n  visibility: visible;\r\n  filter: alpha(opacity=50);\r\n  filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=50);\r\n  opacity: 0.5;\r\n  cursor: pointer;\r\n  transition: all 1.2s;\r\n  -webkit-transition: all 1.2s;\r\n  -moz-transition: all 1.2s;\r\n  -ms-transition: all 1.2s;\r\n  -o-transition: all 1.2s;\r\n}\r\n\r\n\/* STYLE BUTTON 1 - filled in *\/\r\n\/* .scroll-button{\r\n  background: rgb(249, 104, 211);\r\n  border: none;\r\n  color: white;\r\n} *\/\r\n\r\n\/* STYLE BUTTON 2 - only outlines *\/\r\n.scroll-button{\r\n  background: none;\r\n  border: solid 2px rgb(210, 104, 211);\r\n  color: rgb(249, 104, 211);\r\n}\r\n\r\n\/* SHAPE BUTTON 1 - round *\/\r\n\/* .scroll-button{\r\n  border-radius: 50%;\r\n} *\/\r\n\r\n\/* SHAPE BUTTON 2 - square *\/\r\n.scroll-button{\r\n  border-radius: 0%;\r\n}\r\n\r\n\/* POSITION BUTTON 1 - on the bottom of the screen *\/\r\n.scroll-button{\r\n  position: fixed;\r\n}\r\n\r\n\/* POSITION BUTTON 2 - on the bottom of the page *\/\r\n\/* .scroll-button{\r\n  position: absolute;\r\n} *\/\r\n\r\n\r\n\r\n\r\n\r\n.scroll-button:hover{\r\n  filter: alpha(opacity=100);\r\n  filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);\r\n  opacity: 1;\r\n}\r\n\r\n.scroll-button--hidden{\r\n  filter: alpha(opacity=0);\r\n  filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);\r\n  opacity: 0;\r\n  visibility: hidden;\r\n}<\/pre>\n<p>3. Then, add the following JavaScript code and done.<\/p>\n<pre class=\"prettyprint linenums lang-js\">\/\/ PURE JAVASCRIPT \"Scroller\" CLASS (OOP)\r\n\/*\r\n* Author: Wiktor Liszkiewicz\r\n*\/\r\n\r\nfunction Scroller(options) {\r\n  this.options = options;\r\n  this.button = null;\r\n  this.stop = false;\r\n}\r\n\r\nScroller.prototype.constructor = Scroller;\r\n\r\nScroller.prototype.createButton = function() {\r\n\r\n  this.button = document.createElement('button');\r\n  this.button.classList.add('scroll-button');\r\n  this.button.classList.add('scroll-button--hidden');\r\n  this.button.textContent = \"^\";\r\n  document.body.appendChild(this.button);\r\n}\r\n  \r\nScroller.prototype.init = function() {\r\n  this.createButton();\r\n  this.checkPosition();\r\n  this.click();\r\n  this.stopListener();\r\n}\r\n\r\nScroller.prototype.scroll = function() {\r\n  if (this.options.animate == false || this.options.animate == \"false\") {\r\n    this.scrollNoAnimate();\r\n    return;\r\n  }\r\n  if (this.options.animate == \"normal\") {\r\n    this.scrollAnimate();\r\n    return;\r\n  }\r\n  if (this.options.animate == \"linear\") {\r\n    this.scrollAnimateLinear();\r\n    return;\r\n  }\r\n}\r\nScroller.prototype.scrollNoAnimate = function() {\r\n  document.body.scrollTop = 0;\r\n  document.documentElement.scrollTop = 0;\r\n}\r\nScroller.prototype.scrollAnimate = function() {\r\n  if (this.scrollTop() &gt; 0 &amp;&amp; this.stop == false) {\r\n    setTimeout(function() {\r\n      this.scrollAnimate();\r\n      window.scrollBy(0, (-Math.abs(this.scrollTop())\/this.options.normal['steps']));\r\n    }.bind(this), (this.options.normal['ms']));\r\n  }\r\n}\r\nScroller.prototype.scrollAnimateLinear = function() {\r\n  if (this.scrollTop() &gt; 0 &amp;&amp; this.stop == false) {\r\n    setTimeout(function() {\r\n      this.scrollAnimateLinear();\r\n      window.scrollBy(0, -Math.abs(this.options.linear['px']));\r\n    }.bind(this), this.options.linear['ms']);\r\n  }\r\n}\r\n\r\nScroller.prototype.click = function() {\r\n  \r\n  this.button.addEventListener(\"click\", function(e) {\r\n    e.stopPropagation();\r\n      this.scroll();\r\n  }.bind(this), false);\r\n  \r\n  this.button.addEventListener(\"dblclick\", function(e) {\r\n    e.stopPropagation();\r\n      this.scrollNoAnimate();\r\n  }.bind(this), false);\r\n  \r\n}\r\n\r\nScroller.prototype.hide = function() {\r\n  this.button.classList.add(\"scroll-button--hidden\");\r\n}\r\n\r\nScroller.prototype.show = function() {\r\n  this.button.classList.remove(\"scroll-button--hidden\");\r\n}\r\n\r\nScroller.prototype.checkPosition = function() {\r\n  window.addEventListener(\"scroll\", function(e) {\r\n    if (this.scrollTop() &gt; this.options.showButtonAfter) {\r\n      this.show();\r\n    } else {\r\n      this.hide();\r\n    }\r\n  }.bind(this), false);\r\n}\r\n\r\nScroller.prototype.stopListener = function() {\r\n  \r\n  \/\/ stop animation on slider drag\r\n  var position = this.scrollTop();\r\n  window.addEventListener(\"scroll\", function(e) {\r\n    if (this.scrollTop() &gt; position) {\r\n      this.stopTimeout(200);\r\n    } else {\r\n      \/\/...\r\n    }\r\n    position = this.scrollTop();\r\n  }.bind(this, position), false);\r\n\r\n  \/\/ stop animation on wheel scroll down\r\n  window.addEventListener(\"wheel\", function(e) {\r\n    if(e.deltaY &gt; 0) this.stopTimeout(200);\r\n  }.bind(this), false);\r\n}\r\n\r\nScroller.prototype.stopTimeout = function(ms){\r\n   this.stop = true;\r\n         \/\/ console.log(this.stop); \/\/\r\n   setTimeout(function() {\r\n     this.stop = false;\r\n           console.log(this.stop); \/\/\r\n   }.bind(this), ms);\r\n}\r\n\r\nScroller.prototype.scrollTop = function(){\r\n   var curentScrollTop = document.documentElement.scrollTop || document.body.scrollTop;\r\n  return curentScrollTop;\r\n}\r\n\r\n\r\n\r\n\/\/ ------------------- USE EXAMPLE ---------------------\r\n\/\/ *Set options\r\nvar options = {\r\n  'showButtonAfter': 200, \/\/ show button after scroling down this amount of px\r\n  'animate': \"normal\", \/\/ [false|normal|linear] - for false no aditional settings are needed\r\n  \/\/ easy out effect\r\n  'normal': { \/\/ applys only if [animate: normal] - set scroll loop \"distanceLeft\"\/\"steps\"|\"ms\"\r\n    'steps': 15, \/\/ more \"steps\" per loop =&gt; slower animation\r\n    'ms': 1000\/60 \/\/ less \"ms\" =&gt; quicker animation, more \"ms\" =&gt; snapy\r\n  },\r\n  \/\/ linear effect\r\n  'linear': { \/\/ applys only if [animate: linear] - set scroll \"px\"|\"ms\"\r\n    'px': 80, \/\/ more \"px\" =&gt; quicker your animation gets\r\n    'ms': 1000\/60 \/\/ Less \"ms\" =&gt; quicker your animation gets, More \"ms\" =&gt;\r\n  }, \r\n};\r\n\/\/ *Create new Scroller and run it.\r\nvar scroll = new Scroller(options);\r\nscroll.init();<\/pre>\n<p>That&#8217;s all! hopefully, you have successfully integrated this smooth scroll to top code snippet into your project. If you have any questions or facing any issues, feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This JavaScript code snippet helps you to create a smooth scroll to the top button. It dynamically adds back to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":6472,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[116],"tags":[78],"class_list":["post-6457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vanilla-javascript","tag-scrolling"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Smooth Scroll to Top in JavaScript &#8212; CodeHim<\/title>\n<meta name=\"description\" content=\"Here is a lightweight JavaScript code snippet to create smooth scroll to top button. You can view demo and download source code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Smooth Scroll to Top in JavaScript &#8212; CodeHim\" \/>\n<meta property=\"og:description\" content=\"Here is a lightweight JavaScript code snippet to create smooth scroll to top button. You can view demo and download source code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"CodeHim\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codehimofficial\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-11T16:46:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-22T09:47:30+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"960\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Asif Mughal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:site\" content=\"@CodeHimOfficial\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Asif Mughal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/\"},\"author\":{\"name\":\"Asif Mughal\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\"},\"headline\":\"Smooth Scroll to Top in JavaScript\",\"datePublished\":\"2024-01-11T16:46:00+00:00\",\"dateModified\":\"2024-01-22T09:47:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/\"},\"wordCount\":170,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png\",\"keywords\":[\"Scrolling Effects\"],\"articleSection\":[\"Vanilla JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/\",\"url\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/\",\"name\":\"Smooth Scroll to Top in JavaScript &#8212; CodeHim\",\"isPartOf\":{\"@id\":\"https:\/\/codehim.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png\",\"datePublished\":\"2024-01-11T16:46:00+00:00\",\"dateModified\":\"2024-01-22T09:47:30+00:00\",\"description\":\"Here is a lightweight JavaScript code snippet to create smooth scroll to top button. You can view demo and download source code.\",\"breadcrumb\":{\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage\",\"url\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png\",\"contentUrl\":\"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png\",\"width\":1280,\"height\":960,\"caption\":\"Smooth Scroll to Top in JavaScript\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codehim.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Vanilla JavaScript\",\"item\":\"https:\/\/codehim.com\/category\/vanilla-javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Smooth Scroll to Top in JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codehim.com\/#website\",\"url\":\"https:\/\/codehim.com\/\",\"name\":\"CodeHim\",\"description\":\"Web Design Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/codehim.com\/#organization\"},\"alternateName\":\"Web Design Codes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codehim.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/codehim.com\/#organization\",\"name\":\"CodeHim - Web Design Code & Scripts\",\"url\":\"https:\/\/codehim.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"contentUrl\":\"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg\",\"width\":280,\"height\":280,\"caption\":\"CodeHim - Web Design Code & Scripts\"},\"image\":{\"@id\":\"https:\/\/codehim.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/codehimofficial\",\"https:\/\/x.com\/CodeHimOfficial\",\"https:\/\/www.instagram.com\/codehim\/\",\"https:\/\/www.linkedin.com\/company\/codehim\",\"https:\/\/co.pinterest.com\/codehim\/\",\"https:\/\/www.youtube.com\/@codehim\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed\",\"name\":\"Asif Mughal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codehim.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g\",\"caption\":\"Asif Mughal\"},\"description\":\"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.\",\"sameAs\":[\"https:\/\/codehim.com\"],\"url\":\"https:\/\/codehim.com\/author\/asif-mughal\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Smooth Scroll to Top in JavaScript &#8212; CodeHim","description":"Here is a lightweight JavaScript code snippet to create smooth scroll to top button. You can view demo and download source code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Smooth Scroll to Top in JavaScript &#8212; CodeHim","og_description":"Here is a lightweight JavaScript code snippet to create smooth scroll to top button. You can view demo and download source code.","og_url":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/","og_site_name":"CodeHim","article_publisher":"https:\/\/www.facebook.com\/codehimofficial","article_published_time":"2024-01-11T16:46:00+00:00","article_modified_time":"2024-01-22T09:47:30+00:00","og_image":[{"width":1280,"height":960,"url":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png","type":"image\/png"}],"author":"Asif Mughal","twitter_card":"summary_large_image","twitter_creator":"@CodeHimOfficial","twitter_site":"@CodeHimOfficial","twitter_misc":{"Written by":"Asif Mughal","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#article","isPartOf":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/"},"author":{"name":"Asif Mughal","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed"},"headline":"Smooth Scroll to Top in JavaScript","datePublished":"2024-01-11T16:46:00+00:00","dateModified":"2024-01-22T09:47:30+00:00","mainEntityOfPage":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/"},"wordCount":170,"commentCount":0,"publisher":{"@id":"https:\/\/codehim.com\/#organization"},"image":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png","keywords":["Scrolling Effects"],"articleSection":["Vanilla JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/","url":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/","name":"Smooth Scroll to Top in JavaScript &#8212; CodeHim","isPartOf":{"@id":"https:\/\/codehim.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage"},"image":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png","datePublished":"2024-01-11T16:46:00+00:00","dateModified":"2024-01-22T09:47:30+00:00","description":"Here is a lightweight JavaScript code snippet to create smooth scroll to top button. You can view demo and download source code.","breadcrumb":{"@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#primaryimage","url":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png","contentUrl":"https:\/\/codehim.com\/wp-content\/uploads\/2022\/05\/Smooth-Scroll-to-Top-in-Jav.png","width":1280,"height":960,"caption":"Smooth Scroll to Top in JavaScript"},{"@type":"BreadcrumbList","@id":"https:\/\/codehim.com\/vanilla-javascript\/smooth-scroll-to-top-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codehim.com\/"},{"@type":"ListItem","position":2,"name":"Vanilla JavaScript","item":"https:\/\/codehim.com\/category\/vanilla-javascript\/"},{"@type":"ListItem","position":3,"name":"Smooth Scroll to Top in JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/codehim.com\/#website","url":"https:\/\/codehim.com\/","name":"CodeHim","description":"Web Design Code Snippets","publisher":{"@id":"https:\/\/codehim.com\/#organization"},"alternateName":"Web Design Codes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codehim.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codehim.com\/#organization","name":"CodeHim - Web Design Code & Scripts","url":"https:\/\/codehim.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/","url":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","contentUrl":"http:\/\/codehim.com\/wp-content\/uploads\/2023\/06\/Codehim-short-logo.jpg","width":280,"height":280,"caption":"CodeHim - Web Design Code & Scripts"},"image":{"@id":"https:\/\/codehim.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codehimofficial","https:\/\/x.com\/CodeHimOfficial","https:\/\/www.instagram.com\/codehim\/","https:\/\/www.linkedin.com\/company\/codehim","https:\/\/co.pinterest.com\/codehim\/","https:\/\/www.youtube.com\/@codehim"]},{"@type":"Person","@id":"https:\/\/codehim.com\/#\/schema\/person\/cc48f1dbe072a89a62a98171b7db43ed","name":"Asif Mughal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codehim.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b25bfcd7d4e341c2c6f785a88d8ad2a4?s=96&d=mm&r=g","caption":"Asif Mughal"},"description":"I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences. I truly enjoy what I'm doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.","sameAs":["https:\/\/codehim.com"],"url":"https:\/\/codehim.com\/author\/asif-mughal\/"}]}},"views":3153,"_links":{"self":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/6457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/comments?post=6457"}],"version-history":[{"count":0,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/posts\/6457\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media\/6472"}],"wp:attachment":[{"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/media?parent=6457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/categories?post=6457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codehim.com\/wp-json\/wp\/v2\/tags?post=6457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}