SlideShare a Scribd company logo
1 of 29
INTERSECTION
OBSERVER
Hello!
I AM JO FRANCHETTI
Web developer advocate @samsunginternet
@thisisjofrank
https://medium.com/@jofranchetti
SAMSUNG INTERNET
◦ Our team speaks at events and helps to
support communities
◦ We produce code demos and blog posts
◦ Our team is super friendly and love to
work on projects with users of our
browser, come speak to me after the talk.
SAMSUNG INTERNET
◦ Web browser for android phones
◦ Specific UX for our users (privacy, VR)
◦ 3rd most popular mobile browser
◦ Available on android 5.0+ devices
https://samsunginter.net/
INTERSECTION
OBSERVER
WHAT’S THE PROBLEM?
Large number of images on the page
Infinite scrolling
3rd party page-impression scripts cause bloat
Difficult/process intensive to calculate
?
Sluggish, janky
interactions
Large
downloads
No need to
watch/poll for
intersections
No need for 3rd
party libraries
Intersection
Observer API
“
The Intersection Observer API provides
a way to asynchronously observe
changes in the intersection of a target
element with an ancestor element or
with a top-level document's viewport.
EXAMPLE
https://codepen.io/thisisjofrank/pen/pdQzMr
LAZY LOADING IMAGES
https://www.livenation.co.uk/
https://medium.com/@aganglada/intersection-observer-in-
action-efc118062366
Creating an intersection observer
var options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
}
var observer = new IntersectionObserver(callback, options);
Creating an intersection observer
Root
Target
Creating an intersection observer
var options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
}
var observer = new IntersectionObserver(callback, options);
Creating an intersection observer
Root Margin
Target
Creating an intersection observer
var options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
}
var observer = new IntersectionObserver(callback, options);
Creating an intersection observer
Callback executed!
Callback executed!
Creating an intersection observer
var options = {
root: document.querySelector('#scrollArea'),
rootMargin: '0px',
threshold: 1.0
}
var observer = new IntersectionObserver(callback, options);
var target = document.querySelector('#element');
observer.observe(target);
Creating an intersection observer
var callback = function(entries, observer) {
entries.forEach(entry => {
// entry.boundingClientRect
// entry.intersectionRatio
// entry.intersectionRect
// entry.isIntersecting
// entry.rootBounds
// entry.target
// entry.time
});
}
THIS IS A SLIDE TITLE
Here you have:
◦ A list of items
◦ And some text
◦ But remember not to overload
your slides with content
Your audience will listen to you or
read the content, but won’t do both.
DOWNSIDES?
Not Pixel perfect
Not low latency
Values will be ‘out of date’ by the time you get to use them
Spending too much time in the callback will cause lag
Differing levels of support across browsers
?
v
But wait! There’s
more!
DeltaV conf
deltavconf.com
GO FORTH AND
OBSERVE
INTERSECTIONS
Thank you!
Any questions?
@thisisjofrank
@samsunginternet

More Related Content

Similar to Intersection observer for js oxford

Mobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptMobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptfranksvalli
 
Building frameworks: from concept to completion
Building frameworks: from concept to completionBuilding frameworks: from concept to completion
Building frameworks: from concept to completionRuben Goncalves
 
2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...
2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...
2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...César Hernández
 
GraalVM and MicroProfile - A Polyglot Microservices Solution
GraalVM and MicroProfile - A Polyglot Microservices SolutionGraalVM and MicroProfile - A Polyglot Microservices Solution
GraalVM and MicroProfile - A Polyglot Microservices SolutionRoberto Cortez
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Visual Engineering
 
Mobile Device APIs
Mobile Device APIsMobile Device APIs
Mobile Device APIsJames Pearce
 
Detecting headless browsers
Detecting headless browsersDetecting headless browsers
Detecting headless browsersSergey Shekyan
 
Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....
Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....
Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....Patrick Lauke
 
Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...
Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...
Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...Codemotion
 
3 Tips to Deliver Fast Performance Across Mobile Web
3 Tips to Deliver Fast Performance Across Mobile Web3 Tips to Deliver Fast Performance Across Mobile Web
3 Tips to Deliver Fast Performance Across Mobile WebDynatrace
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsJim Jeffers
 
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
Ecto and Phoenix: Doing web with Elixir - Yurii BodarevEcto and Phoenix: Doing web with Elixir - Yurii Bodarev
Ecto and Phoenix: Doing web with Elixir - Yurii BodarevElixir Club
 
Ecto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With ElixirEcto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With ElixirYurii Bodarev
 
Front-end. Global domination
Front-end. Global dominationFront-end. Global domination
Front-end. Global dominationStfalcon Meetups
 
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.jsDrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.jsVladimir Roudakov
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch
 
BrowserArchitecture_ClientSide.pdf
BrowserArchitecture_ClientSide.pdfBrowserArchitecture_ClientSide.pdf
BrowserArchitecture_ClientSide.pdfMuhammadBilal187526
 
Beyond MVC: from Model to Domain
Beyond MVC: from Model to DomainBeyond MVC: from Model to Domain
Beyond MVC: from Model to DomainJeremy Cook
 

Similar to Intersection observer for js oxford (20)

Mobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScriptMobile HTML, CSS, and JavaScript
Mobile HTML, CSS, and JavaScript
 
Building frameworks: from concept to completion
Building frameworks: from concept to completionBuilding frameworks: from concept to completion
Building frameworks: from concept to completion
 
2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...
2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...
2018 (codeone) Graal VM and MicroProfile a polyglot microservices solution [d...
 
GraalVM and MicroProfile - A Polyglot Microservices Solution
GraalVM and MicroProfile - A Polyglot Microservices SolutionGraalVM and MicroProfile - A Polyglot Microservices Solution
GraalVM and MicroProfile - A Polyglot Microservices Solution
 
Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.Workshop 4: NodeJS. Express Framework & MongoDB.
Workshop 4: NodeJS. Express Framework & MongoDB.
 
Progressive Web Apps
Progressive Web AppsProgressive Web Apps
Progressive Web Apps
 
Mobile Device APIs
Mobile Device APIsMobile Device APIs
Mobile Device APIs
 
Detecting headless browsers
Detecting headless browsersDetecting headless browsers
Detecting headless browsers
 
Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....
Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....
Handys und Tablets - Webentwicklung jenseits des Desktops - WebTech Mainz 12....
 
Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...
Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...
Web is the New Mobile: Building Progressive Web Apps - Erica Stanley - Codemo...
 
3 Tips to Deliver Fast Performance Across Mobile Web
3 Tips to Deliver Fast Performance Across Mobile Web3 Tips to Deliver Fast Performance Across Mobile Web
3 Tips to Deliver Fast Performance Across Mobile Web
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
 
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
Ecto and Phoenix: Doing web with Elixir - Yurii BodarevEcto and Phoenix: Doing web with Elixir - Yurii Bodarev
Ecto and Phoenix: Doing web with Elixir - Yurii Bodarev
 
Ecto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With ElixirEcto and Phoenix: Doing Web With Elixir
Ecto and Phoenix: Doing Web With Elixir
 
Front-end. Global domination
Front-end. Global dominationFront-end. Global domination
Front-end. Global domination
 
Frontend. Global domination.
Frontend. Global domination.Frontend. Global domination.
Frontend. Global domination.
 
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.jsDrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
DrupalCon Dublin 2016 - Automated browser testing with Nightwatch.js
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
BrowserArchitecture_ClientSide.pdf
BrowserArchitecture_ClientSide.pdfBrowserArchitecture_ClientSide.pdf
BrowserArchitecture_ClientSide.pdf
 
Beyond MVC: from Model to Domain
Beyond MVC: from Model to DomainBeyond MVC: from Model to Domain
Beyond MVC: from Model to Domain
 

More from Joanna Franchetti

More from Joanna Franchetti (8)

Web bluetooth
Web bluetoothWeb bluetooth
Web bluetooth
 
Lego in a washing machine
Lego in a washing machineLego in a washing machine
Lego in a washing machine
 
CSS in JS for CSS lovers
CSS in JS for CSS loversCSS in JS for CSS lovers
CSS in JS for CSS lovers
 
Box model
Box modelBox model
Box model
 
Your CSS is Awesome
Your CSS is AwesomeYour CSS is Awesome
Your CSS is Awesome
 
How to make your design process agile
How to make your design process agileHow to make your design process agile
How to make your design process agile
 
Don't Panic - Starting Out in Web Development
Don't Panic - Starting Out in Web DevelopmentDon't Panic - Starting Out in Web Development
Don't Panic - Starting Out in Web Development
 
Smacss and bem
Smacss and bemSmacss and bem
Smacss and bem
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Intersection observer for js oxford

Editor's Notes

  1. Hello everyone, and happy pie day! lovely to meet you all and thank you for inviting me to speak! I'm Jo, You can find me @thisisjofrank on Twitter. I'm a web developer advocate at Samsung Internet. If you don’t know what a developer advocate is, well ...
  2. We speak at and run events to support web communities, We made demos and blog posts to explain new APIs and techniques We love it when people are excited about the samsung internet browser, I’ve got some stickers here if anyone wants them. If you’ve /not/ heard of samsung internet well...
  3. We’re a web browser for android phones anyone on 5+ can download it from the play store We’re really keen on privacy and security. We have tracking and ad blockers built into the browser if you want to use them. We’ve got a VR version of the browser for the Gear VR, if you’re interested in VR come talk to me after too! We’re also the third most popular mobile browser, so if you’re not testing in Samsung Internet, then maybe you should consider it Being a person with travel anxiety, I managed to forget to bring you any stickers today, but I'll happily send a pair of socks to anyone who comes up to me after and tells me that they do test in Samsung internet! Anyway, what we’re here to talk about is Intersection observer
  4. So, why do we need or want this new API?
  5. So first of all let’s talk about some common problems that we face as web developers Often we work on sites that have a large number of images, these can slow the loading time of the page right down, especially if they’ve not been optimised You might want to use Infinite scrolling, you often see it in social media services, or e-commerce If you’ve got ads on your page, the Interactive Advertising Bureau has a policy that ads must be 50% visible for more than a continuous second. They use ‘page impression’ scripts to calculate this and these can often be very large and ruin your download size. Maybe you’re triggering an animation or call to action at a particular point that the user has scrolled to. Calculating the position of the scroll and whether or not an element is in view is very process intensive. You can hook up to the scroll event, or use a periodic timer and call getBoundingClientRect() on that element. This approach, however, is painfully slow as each call to getBoundingClientRect() forces the browser to re-layout the entire page and will introduce considerable jank to your website. So Consider a web page that uses infinite scrolling. It uses a 3rd party library to manage the advertisements placed periodically throughout the page, It has animated graphics here and there, and uses another library that draws notification boxes and the like. Each of these has its own intersection detection routines, all running on the main thread. The developer may not even realize this is happening, since they're using two libraries that they may know very little about the inner workings of. As the user scrolls the page, these intersection detection routines are firing constantly, resulting in an experience that leaves the user frustrated with the web site, the browser, and their computer. What do we end up with?
  6. Horrible sluggish feeling user experience And ...
  7. Large downloads for our users, frustrating if they’re paying for data. What do we want?
  8. We don’t want to have to do too much work on the main thread, we don’t want to have to poll, and therefore use processor power to calculate when an element is in view or not
  9. We want to be able to control our own code and its file size. How many of you have used libraries or third party scripts that you have no idea what they’re doing, you just know they work? You don’t know if they’re using optimal techniques, you don’t understand how to fix them if they stop working. We want to understand and be able to manage all of the code on our site, We want no more third party scripts than are absolutely necessary. What can give us all of this?
  10. The new api that lets developers check whether an element is visible within the viewport, or any other scrollable parent element
  11. To quote the spec -
  12. The Intersection Observer API allows you to configure a callback that is called whenever the target element ( which is the one we’re observing), intersects either the device viewport or a specified element; for the purpose of this API, this is called the root element or root. Typically, you'll want to watch for intersection changes with regard to the document's viewport. Whether you're using the viewport or some other element as the root, the API works the same way. It will execute the callback function that you provide, whenever the target element crosses a specified amount of intersection with the root. The degree of intersection between the target element and its root is the intersection ratio, which will be given as a value between 0 and 1. I’ve got a visial representation of the intersection ratio here -> https://codepen.io/thisisjofrank/pen/pdQzMr Here you can see the intersection ratio, that is how much of the element is viewable, as a percentage. So, what might we want to use this for?
  13. One use case would be Lazy loading images I’m sure many of you have worked on websites that contain lots of images. And maybe you didn’t manage the content yourself, it was managed by other people who were uploading those images? And maybe those people didn’t know or care about image optimisation, or sensible sizes for web images And maybe they uploaded over 50 images that were 3000 pixels wide? And you had to wait literal seconds for your page to load? Or the content would jump about as images loaded on the page? I used to work on the Livenation website. There are many /many/ of images on the Live Nation homepage, and sometimes these images are uploaded at such high quality, that we ended up delivering images with very large file sizes. Which made the page incredibly slow to finish loading. And most of the images that were slowing the page down weren’t even in view yet, especially for the mobile users As far as your users are concerned, they don’t really care about whether the images below the fold are loaded, they might never actually scroll that far. Users want the bits of the page that they can see to load quickly and the rest to load when/if they need it. So, how do we use IntersectionObserver?
  14. You create the intersection observer by calling its constructor and passing it a reference to a callback function to be run whenever a threshold is crossed in one direction or the other: A threshold of 1.0 means that when 100% of the target is visible within the element specified by the root option, the callback is invoked. The options object passed into the IntersectionObserver() constructor let you control the circumstances under which the observer's callback is invoked. It has the following fields: root The element that is used as the viewport for checking visibility of the target. Must be an ancestor of the target. Defaults to the browser viewport if not specified or if null. Illustrated ***
  15. We’ve got our target, that we’re observing And our root
  16. Next up in our options object is the rootMargin rootMargin Margin around the root. It essentially allows you to either grow or shrink the area used for intersections. Can have values similar to the CSS margin property, e.g. "10px 20px 30px 40px" (top, right, bottom, left). And that will define when the callback will be invoked. These can be negative numbers too, if you want. If the root element is specified, the values can be percentages. Defaults to all zeros To illustrate
  17. This is our root margin
  18. The callback would trigger here
  19. If you set it to a negative number then it won’t trigger til you get to the intersection with the margin
  20. Then the final piece of our options object is the threshold threshold Indicates at what percentage of the target's visibility the observer's callback should be executed. If you want to detect when visibility passes the 50% mark, you can use a value of 0.5. If you want the callback run every time visibility passes another 25%, you could specify an array [0, 0.25, 0.5, 0.75, 1]. The default is 0 (meaning as soon as even one pixel is visible, the callback will be run). A value of 1.0 means that the threshold isn't considered passed until every pixel is visible.
  21. Lets say you set it to 0.5, in this instance it would fire here and here
  22. Then, once you’ve created the observer, you need to give it a target element to watch Whenever the target meets a threshold specified for the IntersectionObserver, the callback is invoked. The callback receives a list of IntersectionObserverEntry objects. Lets have a look at those.
  23. boundingClientRect is the result of getBoundingClientRect() called on the observed element. Which will give you the size of an element and its position relative to the viewport. intersectionRatio tells you how much of the element is visible. intersectionRect is the intersection of these two rectangles and effectively tells you which part of the observed element is visible. If we were going to, for example check whether an image should be in view, and then load, it we could check the isIntersecting entry and then create the image. rootBounds is the result of calling getBoundingClientRect() on the root element It gives you the bounds of the containing element You can see them here https://codepen.io/thisisjofrank/pen/aYdoxe So, back to our lazy loading images example. Because we want the images to load smoothly we can lay out each image on the page, but give them all the same very tiny source image. We then start the loading of the correct images when they hit an intersection with the browser viewport. Once the image is loaded then we can set the source of that image to be the correct url. We used a blur animation to distract the user while the load is happening. So, now you’re all ready to use intersection observer, we need to consider one more thing... what’s the support like?
  24. Dammit IE11. And safari! https://caniuse.com/#feat=intersectionobserver But never fear, there are polyfills for intersection observer We can check the support for Intersection Observer by checking if it is on the window object. If it is, then there is no need for the polyfill, if not, load our polyfill file into the page. So that’s intersection observer, there are a few other things that should be mentioned though, for example, what shouldn’t intersection observer be used for?
  25. Something to keep in mind is that IntersectionObservers are intentionally neither pixel perfect nor low latency. Using them to implement things like scroll-dependent animations are gonna fail, as the data will be out of date by the time you’ll get to use it. Doing lots of work in the callback will cause your site to lag You may find yourself having to check different entries for different browsers, so maybe check isIntersecting along with checking whether the intersectionRatio is greater than 0.
  26. If you are interested in performance, or want tips on how to improve your site’s performance, or even just want to know why people are talking about performance all the time. I’m running a conference in may, 10 & 11 in LONDON. Your very own seren will be speaking there about accessibility and how it relates to performance. Tickets are on sale now! Go check it out.