ā¹ļø Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://medium.com/talk-like/detecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2 |
| Last Crawled | 2026-04-12 19:54:25 (5 days ago) |
| First Indexed | 2017-11-27 04:40:50 (8 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Detecting if an element is in the Viewport : jQuery | by Boy with Silver Wings | Talk Like | Medium |
| Meta Description | Detecting if an element is in the Viewport : jQuery Detect if an element is visible in the view port. Now why would any one in the right mind want to do that? Well you are reading this, so may be you ⦠|
| Meta Canonical | null |
| Boilerpipe Text | 2 min read
Jan 1, 2017
Detect if an element is visible in the view port. Now why would any one in the right mind want to do that? Well you are reading this, so may be you know why.
Press enter or click to view image in full size
Small Experiment on Codepen
I have to warn you that there are already plugins that help us do the same with jQuery like
https://github.com/customd/jquery-visible
http://www.appelsiini.net/projects/viewport
While all these exist, how many plugins shall we ship with our website? How is the user going to cache them all? Letās take a peek into how this is done.
PS: Iām not going to be talking about jQuery :visible selector. This selector selects elements based on display CSS property or opacity.
We are going to be discussing how to detect if an element is visible on screen in real time.
Here is a codepen demo of what we will be doing.
This started with
this question
on Stackoverflow for which my answer is
here
.
It has been made to suit the question with some borrowed code from
this stackoverflow answer
.
Get Boy with Silver Wingsās stories inĀ yourĀ inbox
Join Medium for free to get updates fromĀ thisĀ writer.
Remember me for faster sign in
Enough with the How I got here lesson. Getting to the JS part.
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();
var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();
return elementBottom > viewportTop && elementTop < viewportBottom;
};
This forms the part of JS that concerns us most. To check if an element is in the viewport, we need four variables.
These being:
elementTop : Top position of the element.
offset function is used to find the position relative to parent element(body in this case) and top refers to the distance from the top.
elementBottom: Bottom position of element
To find the bottom position of element we have to add the height of the element to the elementTop variable. Luckily for us jQuery provides a method called
outerHeight
which allows us to find the height of element including border, padding and optionally padding.
viewportTop: Top of the Viewport
Can be found with the
scrollTop
function on window object. Conveniently returns the relative position from the scrollbar position to object matched, which in this case is our window.
viewportBottom: Bottom of Viewport
Same as elementBottom. We add height of window to the viewportTop to find bottom.
All that is left is to find if the element is within the window coordinates.
]
$(window).on(āresize scrollā, function() {
//Code here
});
By wrapping the elements required on a resize, scroll event, we have successfully build the platform to find if an element is in the viewport or not.
Happy Coding |
| Markdown | [Sitemap](https://medium.com/sitemap/sitemap.xml)
[Open in app](https://play.google.com/store/apps/details?id=com.medium.reader&referrer=utm_source%3DmobileNavBar&source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2Ftalk-like%2Fdetecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)
[Medium Logo](https://medium.com/?source=post_page---top_nav_layout_nav-----------------------------------------)
Get app
[Write](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Fnew-story&source=---top_nav_layout_nav-----------------------new_post_topnav------------------)
[Search](https://medium.com/search?source=post_page---top_nav_layout_nav-----------------------------------------)
Sign up
[Sign in](https://medium.com/m/signin?operation=login&redirect=https%3A%2F%2Fmedium.com%2Ftalk-like%2Fdetecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2&source=post_page---top_nav_layout_nav-----------------------global_nav------------------)

[Talk Like](https://medium.com/talk-like?source=post_page---publication_nav-7751dd6af89c-a6a4405a3ea2---------------------------------------)
Ā·
Follow publication
[](https://medium.com/talk-like?source=post_page---post_publication_sidebar-7751dd6af89c-a6a4405a3ea2---------------------------------------)
A Matter of Space and Time
Follow publication
1
# Detecting if an element is in the Viewport : jQuery
[](https://medium.com/@agney?source=post_page---byline--a6a4405a3ea2---------------------------------------)
[Boy with Silver Wings](https://medium.com/@agney?source=post_page---byline--a6a4405a3ea2---------------------------------------)
Follow
2 min read
Ā·
Jan 1, 2017
1\.3K
13
[Listen](https://medium.com/m/signin?actionUrl=https%3A%2F%2Fmedium.com%2Fplans%3Fdimension%3Dpost_audio_button%26postId%3Da6a4405a3ea2&operation=register&redirect=https%3A%2F%2Fmedium.com%2Ftalk-like%2Fdetecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2&source=---header_actions--a6a4405a3ea2---------------------post_audio_button------------------)
Share
Detect if an element is visible in the view port. Now why would any one in the right mind want to do that? Well you are reading this, so may be you know why.
Press enter or click to view image in full size

Small Experiment on Codepen
I have to warn you that there are already plugins that help us do the same with jQuery like
1. <https://github.com/customd/jquery-visible>
2. <http://www.appelsiini.net/projects/viewport>
While all these exist, how many plugins shall we ship with our website? How is the user going to cache them all? Letās take a peek into how this is done.
PS: Iām not going to be talking about jQuery :visible selector. This selector selects elements based on display CSS property or opacity.
We are going to be discussing how to detect if an element is visible on screen in real time.
Here is a codepen demo of what we will be doing.
This started with [this question](http://stackoverflow.com/questions/41416863/changing-color-when-specific-div-is-visible) on Stackoverflow for which my answer is [here](http://stackoverflow.com/a/41417072/4374566).
It has been made to suit the question with some borrowed code from [this stackoverflow answer](http://stackoverflow.com/a/33979503/4374566).
## Get Boy with Silver Wingsās stories in your inbox
Join Medium for free to get updates from this writer.
Subscribe
Subscribe
Remember me for faster sign in
Enough with the How I got here lesson. Getting to the JS part.
```
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();return elementBottom > viewportTop && elementTop < viewportBottom;
};
```
This forms the part of JS that concerns us most. To check if an element is in the viewport, we need four variables.
These being:
- elementTop : Top position of the element.
- offset function is used to find the position relative to parent element(body in this case) and top refers to the distance from the top.
- elementBottom: Bottom position of element
- To find the bottom position of element we have to add the height of the element to the elementTop variable. Luckily for us jQuery provides a method called [outerHeight](http://api.jquery.com/outerheight/) which allows us to find the height of element including border, padding and optionally padding.
- viewportTop: Top of the Viewport
- Can be found with the [scrollTop](https://api.jquery.com/scrollTop/) function on window object. Conveniently returns the relative position from the scrollbar position to object matched, which in this case is our window.
- viewportBottom: Bottom of Viewport
- Same as elementBottom. We add height of window to the viewportTop to find bottom.
All that is left is to find if the element is within the window coordinates.
```
]
$(window).on(āresize scrollā, function() { //Code here });
```
By wrapping the elements required on a resize, scroll event, we have successfully build the platform to find if an element is in the viewport or not.
**Happy Coding**
[Learn](https://medium.com/tag/learn?source=post_page-----a6a4405a3ea2---------------------------------------)
[Jquery](https://medium.com/tag/jquery?source=post_page-----a6a4405a3ea2---------------------------------------)
[JavaScript](https://medium.com/tag/javascript?source=post_page-----a6a4405a3ea2---------------------------------------)
[Javascript Tips](https://medium.com/tag/javascript-tips?source=post_page-----a6a4405a3ea2---------------------------------------)
1\.3K
1\.3K
13
[](https://medium.com/talk-like?source=post_page---post_publication_info--a6a4405a3ea2---------------------------------------)
[](https://medium.com/talk-like?source=post_page---post_publication_info--a6a4405a3ea2---------------------------------------)
Follow
[Published in Talk Like](https://medium.com/talk-like?source=post_page---post_publication_info--a6a4405a3ea2---------------------------------------)
[47 followers](https://medium.com/talk-like/followers?source=post_page---post_publication_info--a6a4405a3ea2---------------------------------------)
Ā·[Last published Jan 1, 2017](https://medium.com/talk-like/detecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2?source=post_page---post_publication_info--a6a4405a3ea2---------------------------------------)
A Matter of Space and Time
Follow
[](https://medium.com/@agney?source=post_page---post_author_info--a6a4405a3ea2---------------------------------------)
[](https://medium.com/@agney?source=post_page---post_author_info--a6a4405a3ea2---------------------------------------)
Follow
[Written by Boy with Silver Wings](https://medium.com/@agney?source=post_page---post_author_info--a6a4405a3ea2---------------------------------------)
[150 followers](https://medium.com/@agney/followers?source=post_page---post_author_info--a6a4405a3ea2---------------------------------------)
Ā·[160 following](https://medium.com/@agney/following?source=post_page---post_author_info--a6a4405a3ea2---------------------------------------)
Engineer. Driven by Passion.
Follow
## Responses (13)

Write a response
[What are your thoughts?](https://medium.com/m/signin?operation=register&redirect=https%3A%2F%2Fmedium.com%2Ftalk-like%2Fdetecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2&source=---post_responses--a6a4405a3ea2---------------------respond_sidebar------------------)
Cancel
Respond
[](https://medium.com/@cdeliens?source=post_page---post_responses--a6a4405a3ea2----0-----------------------------------)
[Christophe Deliens](https://medium.com/@cdeliens?source=post_page---post_responses--a6a4405a3ea2----0-----------------------------------)
[Jan 12, 2018](https://medium.com/@cdeliens/nicely-done-3c414c2b98ec?source=post_page---post_responses--a6a4405a3ea2----0-----------------------------------)
```
Nicely done! In my case, I needed a function to see if the element was fully visible in the viewport (to decide whether to scroll to make it fully visible or not). So I forked your Pen and adapted: <https://codepen.io/chdeliens/pen/BJxeQG>Thanks!
```
40
1 reply
Reply
[](https://medium.com/@tomasx?source=post_page---post_responses--a6a4405a3ea2---------------------------------------)
[Tomas](https://medium.com/@tomasx?source=post_page---post_responses--a6a4405a3ea2---------------------------------------)
[Oct 5, 2018](https://medium.com/@tomasx/here-is-a-version-of-this-plugin-where-you-can-pass-percentage-of-element-on-screen-between-0-and-212d98f461ed?source=post_page---post_responses--a6a4405a3ea2---------------------------------------)
[56 Here is a version of this plugin where you can pass percentage of element on screen between 0 and 1. For example, passing 0.5 will return true when 50% of the element is on screen. Default is 100%. \$.fn.isInViewport = function(y) {](https://medium.com/@tomasx/here-is-a-version-of-this-plugin-where-you-can-pass-percentage-of-element-on-screen-between-0-and-212d98f461ed?source=post_page---post_responses--a6a4405a3ea2---------------------------------------)
[](https://medium.com/@rkirkner?source=post_page---post_responses--a6a4405a3ea2----2-----------------------------------)
[Rob Kirkner](https://medium.com/@rkirkner?source=post_page---post_responses--a6a4405a3ea2----2-----------------------------------)
[Feb 7, 2018 (edited)](https://medium.com/@rkirkner/copy-n-pasters-beware-23a5550384ef?source=post_page---post_responses--a6a4405a3ea2----2-----------------------------------)
*\$(window).on(āresize scrollā, function() { //Code here });*
```
Copy n pastersā beware! the resize scroll are smart quotes and will not work in a js file. Need to use straight quotes instead.
```
4
Reply
See all responses
## More from Boy with Silver Wings and Talk Like

[](https://medium.com/@agney?source=post_page---author_recirc--a6a4405a3ea2----0---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
[Boy with Silver Wings](https://medium.com/@agney?source=post_page---author_recirc--a6a4405a3ea2----0---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
[Sufiyum Sujayathayum ā A non magical love storySufiyum Sujathayum is a Malayalam movie directed by Naranipuzha Shanavas and produced by Friday Film House. It stars Aditi Rao Hydari, Devā¦](https://medium.com/@agney/sufiyum-sujayathayum-a-non-magical-love-story-c9dd80834468?source=post_page---author_recirc--a6a4405a3ea2----0---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
Jul 9, 2020
[A clap icon 86](https://medium.com/@agney/sufiyum-sujayathayum-a-non-magical-love-story-c9dd80834468?source=post_page---author_recirc--a6a4405a3ea2----0---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)

[](https://medium.com/talk-like?source=post_page---author_recirc--a6a4405a3ea2----1---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
In
[Talk Like](https://medium.com/talk-like?source=post_page---author_recirc--a6a4405a3ea2----1---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
by
[Boy with Silver Wings](https://medium.com/@agney?source=post_page---author_recirc--a6a4405a3ea2----1---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
[Most Beautiful Youtubeās Cover Singers](https://medium.com/talk-like/most-beautiful-youtubes-cover-singers-2ae22f4c40df?source=post_page---author_recirc--a6a4405a3ea2----1---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
Jun 28, 2015
[A clap icon 3](https://medium.com/talk-like/most-beautiful-youtubes-cover-singers-2ae22f4c40df?source=post_page---author_recirc--a6a4405a3ea2----1---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)

[](https://medium.com/talk-like?source=post_page---author_recirc--a6a4405a3ea2----2---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
In
[Talk Like](https://medium.com/talk-like?source=post_page---author_recirc--a6a4405a3ea2----2---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
by
[Boy with Silver Wings](https://medium.com/@agney?source=post_page---author_recirc--a6a4405a3ea2----2---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
[Must Have Rainmeter Skins](https://medium.com/talk-like/must-have-rainmeter-skins-b77ff9aae174?source=post_page---author_recirc--a6a4405a3ea2----2---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
Dec 19, 2014
[A clap icon 12A response icon 2](https://medium.com/talk-like/must-have-rainmeter-skins-b77ff9aae174?source=post_page---author_recirc--a6a4405a3ea2----2---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)

[](https://medium.com/@agney?source=post_page---author_recirc--a6a4405a3ea2----3---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
[Boy with Silver Wings](https://medium.com/@agney?source=post_page---author_recirc--a6a4405a3ea2----3---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
[Captain America: Civil War Spoiler-Full review](https://medium.com/@agney/captain-america-civil-war-spoiler-full-review-dda17f63e9bb?source=post_page---author_recirc--a6a4405a3ea2----3---------------------5833c8d9_460b_4824_b3b0_71461f4d02b8--------------)
May 10, 2016
[See all from Boy with Silver Wings](https://medium.com/@agney?source=post_page---author_recirc--a6a4405a3ea2---------------------------------------)
[See all from Talk Like](https://medium.com/talk-like?source=post_page---author_recirc--a6a4405a3ea2---------------------------------------)
## Recommended from Medium

[](https://medium.com/@emilyhustlenyc?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[Emily](https://medium.com/@emilyhustlenyc?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[I Failed Uberās System Design Interview Last Month. Hereās Every Question They Asked.It was much harder and the rejection email taught me more than any LeetCode grind ever could.](https://medium.com/@emilyhustlenyc/i-failed-ubers-system-design-interview-last-month-here-s-every-question-they-asked-bdaf1bd6e64b?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
Feb 20
[A clap icon 1.7KA response icon 44](https://medium.com/@emilyhustlenyc/i-failed-ubers-system-design-interview-last-month-here-s-every-question-they-asked-bdaf1bd6e64b?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)

[](https://medium.com/@michalmalewicz?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[Michal Malewicz](https://medium.com/@michalmalewicz?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[The End of Dashboards and Design SystemsDesign is becoming quietly human again.](https://medium.com/@michalmalewicz/the-end-of-dashboards-and-design-systems-5d98ec9de627?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
Nov 26, 2025
[A clap icon 6.8KA response icon 283](https://medium.com/@michalmalewicz/the-end-of-dashboards-and-design-systems-5d98ec9de627?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)

[](https://medium.com/towards-artificial-intelligence?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
In
[Towards AI](https://medium.com/towards-artificial-intelligence?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
by
[Shreyas Naphad](https://medium.com/@shreyasnaphad?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[If You Understand These 5 AI Terms, Youāre Ahead of 90% of PeopleMaster the core ideas behind AI without getting lost](https://medium.com/towards-artificial-intelligence/if-you-understand-these-5-ai-terms-youre-ahead-of-90-of-people-c7622d353319?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
Mar 29
[A clap icon 9KA response icon 180](https://medium.com/towards-artificial-intelligence/if-you-understand-these-5-ai-terms-youre-ahead-of-90-of-people-c7622d353319?source=post_page---read_next_recirc--a6a4405a3ea2----0---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)

[](https://medium.com/write-a-catalyst?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
In
[Write A Catalyst](https://medium.com/write-a-catalyst?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
by
[Dr. Patricia Schmidt](https://medium.com/@creatorschmidt?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[As a Neuroscientist, I Quit These 5 Morning Habits That Destroy Your BrainMost people do \#1 within 10 minutes of waking (and it sabotages your entire day)](https://medium.com/write-a-catalyst/as-a-neuroscientist-i-quit-these-5-morning-habits-that-destroy-your-brain-3efe1f410226?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
Jan 14
[A clap icon 46KA response icon 941](https://medium.com/write-a-catalyst/as-a-neuroscientist-i-quit-these-5-morning-habits-that-destroy-your-brain-3efe1f410226?source=post_page---read_next_recirc--a6a4405a3ea2----1---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)

[](https://medium.com/gitconnected?source=post_page---read_next_recirc--a6a4405a3ea2----2---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
In
[Level Up Coding](https://medium.com/gitconnected?source=post_page---read_next_recirc--a6a4405a3ea2----2---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
by
[Kusireddy](https://medium.com/@kusireddy?source=post_page---read_next_recirc--a6a4405a3ea2----2---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[I Stopped Using ChatGPT for 30 Days. What Happened to My Brain Was Terrifying.91% of you will abandon 2026 resolutions by January 10th. Hereās how to be in the 9% who actually win.](https://medium.com/gitconnected/i-stopped-using-chatgpt-for-30-days-what-happened-to-my-brain-was-terrifying-70d2a62246c0?source=post_page---read_next_recirc--a6a4405a3ea2----2---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
Dec 28, 2025
[A clap icon 12.9KA response icon 491](https://medium.com/gitconnected/i-stopped-using-chatgpt-for-30-days-what-happened-to-my-brain-was-terrifying-70d2a62246c0?source=post_page---read_next_recirc--a6a4405a3ea2----2---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)

[](https://medium.com/illumination?source=post_page---read_next_recirc--a6a4405a3ea2----3---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
In
[ILLUMINATION](https://medium.com/illumination?source=post_page---read_next_recirc--a6a4405a3ea2----3---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
by
[Sufyan Maan, M.Eng](https://medium.com/@sufyanmaan?source=post_page---read_next_recirc--a6a4405a3ea2----3---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[I Woke Up at 4:30 AM Every Day for 30 Days ā Here Is What Nobody Tells YouHere is what actually happened, from someone who did it & tracked everything.](https://medium.com/illumination/i-woke-up-at-4-30-am-every-day-for-30-days-here-is-what-nobody-tells-you-054bf0160903?source=post_page---read_next_recirc--a6a4405a3ea2----3---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
Apr 4
[A clap icon 7.6KA response icon 329](https://medium.com/illumination/i-woke-up-at-4-30-am-every-day-for-30-days-here-is-what-nobody-tells-you-054bf0160903?source=post_page---read_next_recirc--a6a4405a3ea2----3---------------------14ab1347_299e_488c_8150_00c31505ebf5--------------)
[See more recommendations](https://medium.com/?source=post_page---read_next_recirc--a6a4405a3ea2---------------------------------------)
[Help](https://help.medium.com/hc/en-us?source=post_page-----a6a4405a3ea2---------------------------------------)
[Status](https://status.medium.com/?source=post_page-----a6a4405a3ea2---------------------------------------)
[About](https://medium.com/about?autoplay=1&source=post_page-----a6a4405a3ea2---------------------------------------)
[Careers](https://medium.com/jobs-at-medium/work-at-medium-959d1a85284e?source=post_page-----a6a4405a3ea2---------------------------------------)
[Press](mailto:pressinquiries@medium.com)
[Blog](https://blog.medium.com/?source=post_page-----a6a4405a3ea2---------------------------------------)
[Privacy](https://policy.medium.com/medium-privacy-policy-f03bf92035c9?source=post_page-----a6a4405a3ea2---------------------------------------)
[Rules](https://policy.medium.com/medium-rules-30e5502c4eb4?source=post_page-----a6a4405a3ea2---------------------------------------)
[Terms](https://policy.medium.com/medium-terms-of-service-9db0094a1e0f?source=post_page-----a6a4405a3ea2---------------------------------------)
[Text to speech](https://speechify.com/medium?source=post_page-----a6a4405a3ea2---------------------------------------) |
| Readable Markdown | [](https://medium.com/@agney?source=post_page---byline--a6a4405a3ea2---------------------------------------)
2 min read Jan 1, 2017
Detect if an element is visible in the view port. Now why would any one in the right mind want to do that? Well you are reading this, so may be you know why.
Press enter or click to view image in full size

Small Experiment on Codepen
I have to warn you that there are already plugins that help us do the same with jQuery like
1. <https://github.com/customd/jquery-visible>
2. <http://www.appelsiini.net/projects/viewport>
While all these exist, how many plugins shall we ship with our website? How is the user going to cache them all? Letās take a peek into how this is done.
PS: Iām not going to be talking about jQuery :visible selector. This selector selects elements based on display CSS property or opacity.
We are going to be discussing how to detect if an element is visible on screen in real time.
Here is a codepen demo of what we will be doing.
This started with [this question](http://stackoverflow.com/questions/41416863/changing-color-when-specific-div-is-visible) on Stackoverflow for which my answer is [here](http://stackoverflow.com/a/41417072/4374566).
It has been made to suit the question with some borrowed code from [this stackoverflow answer](http://stackoverflow.com/a/33979503/4374566).
Get Boy with Silver Wingsās stories in your inbox
Join Medium for free to get updates from this writer.
Remember me for faster sign in
Enough with the How I got here lesson. Getting to the JS part.
```
$.fn.isInViewport = function() {
var elementTop = $(this).offset().top;
var elementBottom = elementTop + $(this).outerHeight();var viewportTop = $(window).scrollTop();
var viewportBottom = viewportTop + $(window).height();return elementBottom > viewportTop && elementTop < viewportBottom;
};
```
This forms the part of JS that concerns us most. To check if an element is in the viewport, we need four variables.
These being:
- elementTop : Top position of the element.
- offset function is used to find the position relative to parent element(body in this case) and top refers to the distance from the top.
- elementBottom: Bottom position of element
- To find the bottom position of element we have to add the height of the element to the elementTop variable. Luckily for us jQuery provides a method called [outerHeight](http://api.jquery.com/outerheight/) which allows us to find the height of element including border, padding and optionally padding.
- viewportTop: Top of the Viewport
- Can be found with the [scrollTop](https://api.jquery.com/scrollTop/) function on window object. Conveniently returns the relative position from the scrollbar position to object matched, which in this case is our window.
- viewportBottom: Bottom of Viewport
- Same as elementBottom. We add height of window to the viewportTop to find bottom.
All that is left is to find if the element is within the window coordinates.
```
]
$(window).on(āresize scrollā, function() { //Code here });
```
By wrapping the elements required on a resize, scroll event, we have successfully build the platform to find if an element is in the viewport or not.
**Happy Coding** |
| Shard | 77 (laksa) |
| Root Hash | 13179037029838926277 |
| Unparsed URL | com,medium!/talk-like/detecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2 s443 |