<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pelumi Aleshinloye</title>
    <description>The latest articles on DEV Community by Pelumi Aleshinloye (@pelumicodes).</description>
    <link>https://dev.to/pelumicodes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F204401%2F6f5ecb2c-c42e-4f92-95f9-0fe105033cf6.png</url>
      <title>DEV Community: Pelumi Aleshinloye</title>
      <link>https://dev.to/pelumicodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pelumicodes"/>
    <language>en</language>
    <item>
      <title>How to test if an element is in the viewport</title>
      <dc:creator>Pelumi Aleshinloye</dc:creator>
      <pubDate>Fri, 13 Dec 2019 17:41:52 +0000</pubDate>
      <link>https://dev.to/pelumicodes/how-to-test-if-an-element-is-in-the-viewport-3jid</link>
      <guid>https://dev.to/pelumicodes/how-to-test-if-an-element-is-in-the-viewport-3jid</guid>
      <description>&lt;p&gt;This article was originally posted on &lt;a href="https://pelumicodes.com"&gt;pelumicodes.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There can be several scenarios that may require you to determine whether an element is currently in the viewport of your browser, you might want to implement lazy loading or animating divs whenever they show up on the user's screen.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We will not be using jQuery :visible selector because it selects elements based on display CSS property or opacity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To do this with jQuery, we first have to define a function &lt;code&gt;isInViewPort&lt;/code&gt;  that checks if the element is in the browser's viewport&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isInViewport&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elementTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;elementBottom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;elementTop&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;outerHeight&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;viewportTop&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;scrollTop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;viewportBottom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;viewportTop&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;elementBottom&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;viewportTop&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;elementTop&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;viewportBottom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;resize&lt;/span&gt; &lt;span class="nx"&gt;scroll&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(.&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;isInViewport&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// code here&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The  &lt;code&gt;elementTop&lt;/code&gt;  returns the top position of the element, &lt;code&gt;offset().top&lt;/code&gt; returns the distance of the current element relative to the top of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLelement/offsetParent"&gt;offsetParent&lt;/a&gt; node.&lt;/p&gt;

&lt;p&gt;To get the bottom position of the element we need to add the height of the element to the &lt;code&gt;offset().top&lt;/code&gt; . The &lt;a href="%C2%A0https://api.jquery.com/outerheight/"&gt;outerHeight&lt;/a&gt; allows us to find the height of the element including the border and padding.&lt;/p&gt;

&lt;p&gt;The  &lt;code&gt;viewportTop&lt;/code&gt;  returns the top of the viewport; the relative position from the scrollbar position to object matched.&lt;/p&gt;

&lt;p&gt;To get the &lt;code&gt;viewportBottom&lt;/code&gt; too we add the height of the window to the  &lt;code&gt;viewportTop&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;isInViewport&lt;/code&gt;  function returns &lt;code&gt;true&lt;/code&gt; if the element is in the viewport, so you can run whatever code you want if the condition is met.&lt;/p&gt;




&lt;p&gt;Plugins that do the job for you&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://vanillajstoolkit.com/helpers/isinviewport/"&gt;Vanila Js helper function&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/customd/jquery-visible"&gt;https://github.com/customd/jquery-visible&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.appelsiini.net/projects/viewport"&gt;http://www.appelsiini.net/projects/viewport&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;cc: &lt;a href="https://stackoverflow.com/questions/20791374/jquery-check-if-element-is-visible-in-viewport/33979503#33979503"&gt;stackoverflow answer&lt;/a&gt;&lt;/p&gt;

</description>
      <category>jquery</category>
      <category>javascript</category>
      <category>angular</category>
      <category>browser</category>
    </item>
  </channel>
</rss>
