{"id":6889,"date":"2019-09-08T12:25:03","date_gmt":"2019-09-08T10:25:03","guid":{"rendered":"https:\/\/www.creativejuiz.fr\/blog?p=6889"},"modified":"2019-09-08T14:49:14","modified_gmt":"2019-09-08T12:49:14","slug":"did-you-know-scrollintoview-js-scroll-element","status":"publish","type":"post","link":"https:\/\/www.creativejuiz.fr\/blog\/en\/tutorials\/did-you-know-scrollintoview-js-scroll-element","title":{"rendered":"Did you know \u2013 scrollIntoView() in JS in order to scroll to an element"},"content":{"rendered":"<p>Not so long ago I saw one of the developers I work with writing a very complex function to allow a smooth scrolling to a lower element on the page. I was quite surprised that he doesn&#8217;t use <code>scrollIntoView()<\/code> and his <code>behavior<\/code> parameter and its <code>smooth<\/code> value. Did you heard of it?<\/p>\n<p><!--more--><\/p>\n<p>When you are used to doing things in a certain way, it is difficult to question yourself, especially when projects are a little tense in terms of timing. Often, even as developers, you tend to create a small file of utilities or helpers to avoid having to rewrite the small useful functions that you are used to reusing.<\/p>\n<p>I wrote a little function myself a while ago using jQuery (yes, it&#8217;s way outdated: p), but it&#8217;s in french ^^&#8221;<br \/>\n<a href=\"https:\/\/www.creativejuiz.fr\/blog\/tutoriels\/jquery-effet-smooth-scroll-defilement-fluide\">Smoothscroll in jQuery<\/a>.<\/p>\n<h2>Smooth-scroll on click with <code>scrollIntoView()<\/code><\/h2>\n<p>Imagine an internal anchor in a page, when the user clicks on the link you want to propose a smooth scrolling to the beginning of this element (here the target is <code>#part-3<\/code>). This is possible with this little piece of JavaScript.<\/p>\n<div class=\"code-embed-wrapper\"> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-line=\"6\" data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">const target = document.getElementById(&#039;part-3&#039;),<br\/>      button = document.getElementById(&#039;goto&#039;);<br\/><br\/>button.addEventListener(&#039;click&#039;, function(){<br\/>\t<br\/>\ttarget.scrollIntoView({<br\/>\t\tblock: &#039;start&#039;,<br\/>\t\tbehavior: &#039;smooth&#039;,<br\/>\t\tinline: &#039;nearest&#039;<br\/>\t});<br\/>\t<br\/>});<\/code><\/pre> <div class=\"code-embed-infos\"> <\/div> <\/div>\n<p>As you can see, there&#8217;s nothing very complicated about it. The parameters are as follows, they are all optional:<\/p>\n<ul>\n<li><code>block<\/code> : which part of the block you want to reach, among <code>start<\/code>, <code>center<\/code>, <code>end<\/code> or <code>nearest<\/code> (by default <code>start<\/code>).<\/li>\n<li><code>behavior<\/code> : how to reach this block, among the options <code>auto<\/code> or <code>smooth<\/code>, (by defaut <code>auto<\/code>).<\/li>\n<li><code>inline<\/code> : where to reach the element if it&#8217;s a horizontal scrolling, among <code>start<\/code>, <code>center<\/code>, <code>end<\/code> or <code>nearest<\/code> (by default <code>nearest<\/code>)<\/li>\n<\/ul>\n<p>A little demonstration on CodePen to help you picture this.<\/p>\n<p class='codepen'  data-height='402' data-theme-id='0' data-slug-hash='rNBJVqJ' data-default-tab='result' data-animations='run' data-editable='' data-embed-version='2'>\nVoir la d\u00e9mo sur CodePen <\/p>\n\n<h2>SmoothScroll when adding an element<\/h2>\n<p>One of the use cases that occurs quite regularly is the case of an addition of section in a page after a user action, or a new element in a list for example. This is a good way for the user to better understand what just happened on the screen by guiding them to the element they have asked to add.<\/p>\n<div style=\"width: 402px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-6889-1\" width=\"402\" height=\"608\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2019\/09\/scroll-into-view-demo.mp4?_=1\" \/><source type=\"video\/webm\" src=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2019\/09\/scroll-into-view-demo.webm?_=1\" \/><a href=\"https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2019\/09\/scroll-into-view-demo.mp4\">https:\/\/www.creativejuiz.fr\/blog\/wp-content\/uploads\/2019\/09\/scroll-into-view-demo.mp4<\/a><\/video><\/div>\n<p>Here is a little demo of a todo list using <code>localStorage<\/code> to save your list and <code>scrollIntoView()<\/code>\u00a0 to bring you to the new elements.<\/p>\n<p><a class=\"demo\" href=\"https:\/\/www.creativejuiz.fr\/blog\/doc\/todo-list-javascript-scrollintoview.html\" target=\"_blank\" rel=\"noopener noreferrer\">See the demo<\/a><\/p>\n<p>I started a <a href=\"https:\/\/codepen.io\/GeoffreyCrofte\/pen\/RwbQrjQ?editors=0010\">demo on CodePen<\/a>\u00a0 but it&#8217;s kind of buggy because CodePen don&#8217;t know how to load data from <code>localStorage<\/code>. But you can play with it and fork it if you want to.<\/p>\n<p class='codepen'  data-height='250' data-theme-id='0' data-slug-hash='RwbQrjQ' data-default-tab='result' data-animations='run' data-editable='' data-embed-version='2'>\nVoir la d\u00e9mo sur CodePen <\/p>\n\n<p>A look to the support.<\/p>\n<div class=\"ciu_embed\" data-feature=\"scrollintoview\" data-periods=\"future_1,current,past_1,past_2\"><a href=\"http:\/\/caniuse.com\/#feat=scrollintoview\">Can I Use scrollintoview?<\/a><\/div>\n\t\t<script src=\"\/\/cdn.jsdelivr.net\/caniuse-embed\/1.0.1\/caniuse-embed.min.js\"><\/script>\n<h2>Goind further in CSS<\/h2>\n<p>CSS use the same approach to customize the scroll behavior when an element is scrollable. Learn more about it thanks to this article: <a href=\"https:\/\/www.creativejuiz.fr\/blog\/en\/css3\/css-smooth-scrolling-with-scroll-behavior\">CSS Smooth-Scrolling avec Scroll-Behavior<\/a>.<\/p>\n<p>Don&#8217;t hesitate to share your use case in the comments \ud83d\ude42<\/p>\n<div class=\"ciu_embed\" data-feature=\"css-scroll-behavior\" data-periods=\"future_1,current,past_1,past_2\"><a href=\"http:\/\/caniuse.com\/#feat=css-scroll-behavior\">Can I Use css-scroll-behavior?<\/a><\/div>\n\t\t<script src=\"\/\/cdn.jsdelivr.net\/caniuse-embed\/1.0.1\/caniuse-embed.min.js\"><\/script>\n","protected":false},"excerpt":{"rendered":"<p>Not so long ago I saw one of the developers I work with writing a very complex function to allow a smooth scrolling to a lower element on the page. I was quite surprised that he doesn&#8217;t use scrollIntoView() and his behavior parameter and its smooth value. Did you heard of it?<\/p>\n","protected":false},"author":4,"featured_media":6887,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_bluesky_dont_syndicate":"","_bluesky_syndication_accounts":"","_bluesky_syndication_text":"","footnotes":""},"categories":[657,647],"tags":[798,810,808],"coauthors":[597],"class_list":["post-6889","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript-en","category-tutorials","tag-did-you-know","tag-localstorage","tag-smoothscroll-en"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/6889","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/comments?post=6889"}],"version-history":[{"count":3,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/6889\/revisions"}],"predecessor-version":[{"id":6896,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/posts\/6889\/revisions\/6896"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media\/6887"}],"wp:attachment":[{"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/media?parent=6889"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/categories?post=6889"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/tags?post=6889"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.creativejuiz.fr\/blog\/en\/wp-json\/wp\/v2\/coauthors?post=6889"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}