{"id":335,"date":"2018-09-20T08:19:50","date_gmt":"2018-09-20T08:19:50","guid":{"rendered":"http:\/\/www.projectimmerse.com\/?p=335"},"modified":"2020-06-17T07:51:51","modified_gmt":"2020-06-17T07:51:51","slug":"smooth-scroll-with-jquery","status":"publish","type":"post","link":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/","title":{"rendered":"Smooth Scroll with jQuery"},"content":{"rendered":"<p>Every now and then I get some really cool feature request, nothing too big &#8211; just something simple yet effective. Anchor links aren&#8217;t talked about much these days but they&#8217;re so useful in situations where you run into a really long website with a lot of content (or anything), you have to keep scrolling down just to get to the footer. Worse yet, what if the contact form is down at the footer &#8211; that&#8217;s a lot of wasted time, space and potential customers lost. Not a good thing. <\/p>\n<p>The solution and probably the most simple and effective way is to add an anchor link. This can easily be done with plain HTML.<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\r\n&lt;a class=&quot;scrollContact&quot; href=&quot;#scrollContact&quot; title=&quot;Fill Out Our Contact Form!&quot;&gt;Fill Out Our Contact Form!&lt;\/a&gt;\r\n\r\n&lt;div id=&quot;scrollContact&quot;&gt;\r\n\/\/ Contact form here\r\n&lt;\/div&gt;\r\n\r\n<\/pre>\n<p>So when a visitor clicks on that hyperlink, the page will immediately &#8220;leapfrog&#8221; down to the contact form way down at the footer. Pretty neat. <\/p>\n<p>In my situation though, I used jQuery to dynamically create the element and attach some cool scrolling effect to it. Their templating system was a little restrictive, so I&#8217;m glad jQuery came to the rescue. <\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\njQuery(document).ready(function() {\r\n\r\n  jQuery(&quot;.custom-featured-img&quot;).append('&lt;a class=&quot;scrollContact&quot; href=&quot;#scrollContact&quot; title=&quot;Fill Out Our Contact Form!&quot;&gt;Fill Out Our Contact Form!&lt;\/a&gt;');\r\n\r\n    jQuery(&quot;.scrollContact&quot;).click(function(e) {\r\n\te.preventDefault();\r\n\t\r\n        jQuery(&quot;html, body&quot;).animate({ scrollTop: jQuery(jQuery(this).attr(&quot;href&quot;)).offset().top }, 500);\r\n   });\r\n   \r\n});\r\n\r\n<\/pre>\n<p>To break it down, I injected  an anchor link element within a div containing &#8220;.custom-featured-img&#8221; so I can position it absolute. This way I can have more control on it&#8217;s positioning relative to its parent element &#8211; &#8220;.custom-featured-img&#8221;.<\/p>\n<p>Using jQuery I use the append method to do that which I just described, which is the same thing as appending an element. <\/p>\n<p>Then using the click method, I attached this event handler to the anchor link itself and within the function call we prevent the default click behavior from firing by calling &#8220;e.preventDefault()&#8221;. <\/p>\n<p>At this point, we have a working anchor link &#8211; we could walk away and call it a day. But with just one more line of code we can animate its default behavior by using the animate method. The scrollTop property handles most of the work for us. The second argument you see &#8220;500&#8221; is a value in milliseconds, so adjust this accordingly. <\/p>\n<p>Can&#8217;t see myself ever leaving an anchor link without applying some animation to it &#8211; it&#8217;s a cheap price to pay for something quite valuable. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every now and then I get some really cool feature request, nothing too big &#8211; just something simple yet effective. Anchor links aren&#8217;t talked about much these days but they&#8217;re so useful in situations where you run into a really long website with a lot of content (or anything), you have to keep scrolling down &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Smooth Scroll with jQuery&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1156,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Smooth Scroll with jQuery - Project Immerse<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Smooth Scroll with jQuery - Project Immerse\" \/>\n<meta property=\"og:description\" content=\"Every now and then I get some really cool feature request, nothing too big &#8211; just something simple yet effective. Anchor links aren&#8217;t talked about much these days but they&#8217;re so useful in situations where you run into a really long website with a lot of content (or anything), you have to keep scrolling down &hellip; Continue reading &quot;Smooth Scroll with jQuery&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/\" \/>\n<meta property=\"og:site_name\" content=\"Project Immerse\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-20T08:19:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-17T07:51:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/09\/smooth-scroll-using-jquery.png\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"projectimmerse\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"projectimmerse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/\"},\"author\":{\"name\":\"projectimmerse\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"headline\":\"Smooth Scroll with jQuery\",\"datePublished\":\"2018-09-20T08:19:50+00:00\",\"dateModified\":\"2020-06-17T07:51:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/\"},\"wordCount\":469,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/smooth-scroll-using-jquery.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/\",\"name\":\"Smooth Scroll with jQuery - Project Immerse\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/smooth-scroll-using-jquery.png\",\"datePublished\":\"2018-09-20T08:19:50+00:00\",\"dateModified\":\"2020-06-17T07:51:51+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/smooth-scroll-using-jquery.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/09\\\/smooth-scroll-using-jquery.png\",\"width\":750,\"height\":750,\"caption\":\"Smooth Scroll with jQuery\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/smooth-scroll-with-jquery\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Smooth Scroll with jQuery\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\",\"name\":\"Project Immerse\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\",\"name\":\"projectimmerse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"caption\":\"projectimmerse\"},\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/author\\\/projectimmerse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Smooth Scroll with jQuery - Project Immerse","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:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/","og_locale":"en_US","og_type":"article","og_title":"Smooth Scroll with jQuery - Project Immerse","og_description":"Every now and then I get some really cool feature request, nothing too big &#8211; just something simple yet effective. Anchor links aren&#8217;t talked about much these days but they&#8217;re so useful in situations where you run into a really long website with a lot of content (or anything), you have to keep scrolling down &hellip; Continue reading \"Smooth Scroll with jQuery\"","og_url":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/","og_site_name":"Project Immerse","article_published_time":"2018-09-20T08:19:50+00:00","article_modified_time":"2020-06-17T07:51:51+00:00","og_image":[{"width":750,"height":750,"url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/09\/smooth-scroll-using-jquery.png","type":"image\/png"}],"author":"projectimmerse","twitter_card":"summary_large_image","twitter_misc":{"Written by":"projectimmerse","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#article","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/"},"author":{"name":"projectimmerse","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"headline":"Smooth Scroll with jQuery","datePublished":"2018-09-20T08:19:50+00:00","dateModified":"2020-06-17T07:51:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/"},"wordCount":469,"commentCount":0,"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/09\/smooth-scroll-using-jquery.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/","url":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/","name":"Smooth Scroll with jQuery - Project Immerse","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#primaryimage"},"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/09\/smooth-scroll-using-jquery.png","datePublished":"2018-09-20T08:19:50+00:00","dateModified":"2020-06-17T07:51:51+00:00","author":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"breadcrumb":{"@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#primaryimage","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/09\/smooth-scroll-using-jquery.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/09\/smooth-scroll-using-jquery.png","width":750,"height":750,"caption":"Smooth Scroll with jQuery"},{"@type":"BreadcrumbList","@id":"https:\/\/www.projectimmerse.com\/blog\/smooth-scroll-with-jquery\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.projectimmerse.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Smooth Scroll with jQuery"}]},{"@type":"WebSite","@id":"https:\/\/www.projectimmerse.com\/blog\/#website","url":"https:\/\/www.projectimmerse.com\/blog\/","name":"Project Immerse","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.projectimmerse.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5","name":"projectimmerse","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","caption":"projectimmerse"},"url":"https:\/\/www.projectimmerse.com\/blog\/author\/projectimmerse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":7,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":448,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/335\/revisions\/448"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media\/1156"}],"wp:attachment":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}