{"id":166,"date":"2014-08-14T11:42:59","date_gmt":"2014-08-14T17:42:59","guid":{"rendered":"http:\/\/laubsterboy.com\/blog\/?p=166"},"modified":"2014-08-14T11:42:59","modified_gmt":"2014-08-14T17:42:59","slug":"javascript-scroll-into-view","status":"publish","type":"post","link":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/","title":{"rendered":"JavaScript Scroll Into View"},"content":{"rendered":"<p>There are times when you may want to add interactivity to your website based on the scroll position relative to an elements position but don&#8217;t want to use yet another jQuery plugin. This can be useful to check to see if an element has scrolled into view. So, below I&#8217;ve added a very simple function that accepts a jQuery selector string as its only parameter and then returns an object that contains position information about the desired element. All position attributes are boolean values so they can easily be used with conditional logic. The first is &#8220;visible&#8221; which will be set to true if the desired element is currently visible on the screen (partially or wholly). The second is &#8220;above&#8221; which will be set to true if the desired element is currently above what is\u00a0visible on the screen. The last is &#8220;below&#8221; which will be set to true if the desired element is currently below what is visible on the screen.<\/p>\n<pre class=\"lang:js decode:true\">function elementVisible(element) {\n\tvar returnValue = {};\n\tvar docViewTop = $(window).scrollTop();\n\tvar docViewBottom = docViewTop + $(window).height();\n\tvar elementTop = $(element).offset().top;\n\tvar elementBottom = elementTop + $(element).height();\n\t    \n\treturnValue.visible = ((elementBottom &gt;= docViewTop &amp;&amp; elementBottom &lt;= docViewBottom) || (elementTop &lt;= docViewBottom &amp;&amp; elementTop &gt;= docViewTop) || (elementTop &lt;= docViewTop &amp;&amp; elementBottom &gt;= docViewBottom));\n\treturnValue.above = (elementBottom &lt; docViewTop);\n\treturnValue.below = (elementTop &gt; docViewBottom);\n\t    \n\treturn returnValue;\n}<\/pre>\n<p>This function can easily be used in combination with a the jQuery scroll method.<\/p>\n<pre class=\"lang:default decode:true \">(function($) {\n\tvar myDesiredElementVisibility;\n\t\n\t$(window).scroll(function(e) {\n\t\tvar myDesiredElementVisibility = elementVisible('#my-desired-element');\n\t\tif (myDesiredElementVisibility.visible) {\n\t\t\t\/\/ Element is visible\n\t\t}\n\t\tif (myDesiredElementVisibility.above) {\n\t\t\t\/\/ Element is above the view\n\t\t}\n\t\tif (myDesiredElementVisibility.below) {\n\t\t\t\/\/ Element is below the view\n\t\t}\n\t});\n})(jQuery);<\/pre>\n<p>As you can see it&#8217;s easy to check if an element has scrolled into view by checking if it&#8217;s visible, above, or below the view. This could be used to add or remove classes to an element, and perhaps used to set an elements position to be fixed. There are jQuery plugins, such as <a href=\"http:\/\/janpaepke.github.io\/ScrollMagic\/\">ScrollMagic<\/a>, which provide many more features and allow for much more complex interactivity, but for simple scroll based interactions the above function is quite useful.<\/p>\n<p>As always please use the comments area to share your thoughts or suggestions for improvement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times when you may want to add interactivity to your website based on the scroll position relative to an elements position but don&#8217;t want to use yet another jQuery plugin. This can be useful to check to see if an element has scrolled into view. So, below I&#8217;ve added a very simple function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2,3],"tags":[34,35,36,37,56,57],"class_list":["post-166","post","type-post","status-publish","format-standard","hentry","category-code-snippet","category-guide","tag-interaction","tag-interactivity","tag-javascript","tag-jquery","tag-scroll","tag-scrollmagic"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JavaScript Scroll Into View Interaction<\/title>\n<meta name=\"description\" content=\"The JavaScript Scroll Into View guide explains how to use a simple JavaScript function to check if an element has scrolled into view.\" \/>\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.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Scroll Into View Interaction\" \/>\n<meta property=\"og:description\" content=\"The JavaScript Scroll Into View guide explains how to use a simple JavaScript function to check if an element has scrolled into view.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/\" \/>\n<meta property=\"og:site_name\" content=\"John Russell Blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-14T17:42:59+00:00\" \/>\n<meta name=\"author\" content=\"John Russell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@laubsterboy\" \/>\n<meta name=\"twitter:site\" content=\"@laubsterboy\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Russell\" \/>\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.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/\"},\"author\":{\"name\":\"John Russell\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\"},\"headline\":\"JavaScript Scroll Into View\",\"datePublished\":\"2014-08-14T17:42:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/\"},\"wordCount\":278,\"commentCount\":0,\"keywords\":[\"Interaction\",\"Interactivity\",\"JavaScript\",\"jQuery\",\"Scroll\",\"ScrollMagic\"],\"articleSection\":[\"Code Snippet\",\"Guide\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/\",\"url\":\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/\",\"name\":\"JavaScript Scroll Into View Interaction\",\"isPartOf\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#website\"},\"datePublished\":\"2014-08-14T17:42:59+00:00\",\"author\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\"},\"description\":\"The JavaScript Scroll Into View guide explains how to use a simple JavaScript function to check if an element has scrolled into view.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.johnrussell.dev\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Scroll Into View\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#website\",\"url\":\"https:\/\/www.johnrussell.dev\/blog\/\",\"name\":\"John Russell Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.johnrussell.dev\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\",\"name\":\"John Russell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g\",\"caption\":\"John Russell\"},\"url\":\"https:\/\/www.johnrussell.dev\/blog\/author\/laubsterboy\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Scroll Into View Interaction","description":"The JavaScript Scroll Into View guide explains how to use a simple JavaScript function to check if an element has scrolled into view.","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.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Scroll Into View Interaction","og_description":"The JavaScript Scroll Into View guide explains how to use a simple JavaScript function to check if an element has scrolled into view.","og_url":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/","og_site_name":"John Russell Blog","article_published_time":"2014-08-14T17:42:59+00:00","author":"John Russell","twitter_card":"summary_large_image","twitter_creator":"@laubsterboy","twitter_site":"@laubsterboy","twitter_misc":{"Written by":"John Russell","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#article","isPartOf":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/"},"author":{"name":"John Russell","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c"},"headline":"JavaScript Scroll Into View","datePublished":"2014-08-14T17:42:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/"},"wordCount":278,"commentCount":0,"keywords":["Interaction","Interactivity","JavaScript","jQuery","Scroll","ScrollMagic"],"articleSection":["Code Snippet","Guide"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/","url":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/","name":"JavaScript Scroll Into View Interaction","isPartOf":{"@id":"https:\/\/www.johnrussell.dev\/blog\/#website"},"datePublished":"2014-08-14T17:42:59+00:00","author":{"@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c"},"description":"The JavaScript Scroll Into View guide explains how to use a simple JavaScript function to check if an element has scrolled into view.","breadcrumb":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.johnrussell.dev\/blog\/2014\/08\/javascript-scroll-into-view\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.johnrussell.dev\/blog\/"},{"@type":"ListItem","position":2,"name":"JavaScript Scroll Into View"}]},{"@type":"WebSite","@id":"https:\/\/www.johnrussell.dev\/blog\/#website","url":"https:\/\/www.johnrussell.dev\/blog\/","name":"John Russell Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.johnrussell.dev\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c","name":"John Russell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g","caption":"John Russell"},"url":"https:\/\/www.johnrussell.dev\/blog\/author\/laubsterboy\/"}]}},"_links":{"self":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts\/166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/comments?post=166"}],"version-history":[{"count":0,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts\/166\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/media?parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/categories?post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/tags?post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}