{"id":301019,"date":"2020-03-31T09:00:35","date_gmt":"2020-03-31T09:00:35","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=301019"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=301019","title":{"rendered":"Intersection Observer API: \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/494670\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/p9\/ub\/-0\/p9ub-06qrmugtoafjurwfqm7jp4.jpeg\"><\/p>\n<p>  \u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0434\u0440\u0443\u0437\u044c\u044f!<\/p>\n<h3>\u041e\u0431\u0437\u043e\u0440<\/h3>\n<p>  Intersection Observer API (IOA) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 (target) \u0441 \u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c (root) \u0438\u043b\u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 (viewport). \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u044d\u0442\u043e\u0442 API \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 root \u0438\u043b\u0438 viewport. <\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\u00ab\u043b\u0435\u043d\u0438\u0432\u0430\u044f\u00bb \u0438\u043b\u0438 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/li>\n<li>\u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/li>\n<li>\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0435\u043a\u043b\u0430\u043c\u044b \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u043a\u0430\u0437\u043e\u0432<\/li>\n<li>\u0437\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438\u043b\u0438 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438, \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u0445\u0441\u044f \u0432 \u043f\u043e\u043b\u0435 \u0437\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/li>\n<\/ul>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 IOA \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442-\u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c \u0441 \u0434\u0432\u0443\u043c\u044f \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u2014 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"javascript\">\/\/ \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 let options = {     root: document.querySelector('.scroll-list'),     rootMargin: '5px',     threshold: 0.5 }  \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 let callback = function(entries, observer){     ... }  \/\/ \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c let observer = new IntersectionObserver(callback, options) <\/code><\/pre>\n<p>  \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438:<\/p>\n<ul>\n<li>root \u2014 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0432 \u0440\u043e\u043b\u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0434\u043b\u044f target (\u043f\u0440\u0435\u0434\u043e\u043a \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u043b\u0438 null \u0434\u043b\u044f viewport) <\/li>\n<li>rootMargin \u2014 \u043e\u0442\u0441\u0442\u0443\u043f\u044b \u0432\u043e\u043a\u0440\u0443\u0433 root (margin \u0432 CSS, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u0441\u0435 \u043e\u0442\u0441\u0442\u0443\u043f\u044b \u0440\u0430\u0432\u043d\u044b 0) <\/li>\n<li>threshold \u2014 \u0447\u0438\u0441\u043b\u043e \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0447\u0438\u0441\u0435\u043b, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u044f target \u0438 root <\/li>\n<\/ul>\n<p>  \u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442 observer:<\/p>\n<pre><code class=\"javascript\">let target = document.querySelector('.list-item') observer.observe(target) <\/code><\/pre>\n<p>  \u0412\u044b\u0437\u043e\u0432 callback \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0431 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445, \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u0435\u0434\u0448\u0438\u0445 \u0441 \u0446\u0435\u043b\u0435\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c:<\/p>\n<pre><code class=\"javascript\">let callback = (entries, observer) =&gt; {     entries.forEach(entry =&gt; {         \/\/ entry (\u0437\u0430\u043f\u0438\u0441\u044c) - \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435         \/\/   entry.boundingClientRect         \/\/   entry.intersectionRatio         \/\/   entry.intersectionRect         \/\/   entry.isIntersecting         \/\/   entry.rootBounds         \/\/   entry.target         \/\/   entry.time     }) } <\/code><\/pre>\n<p>  \u0412 \u0441\u0435\u0442\u0438 \u043f\u043e\u043b\u043d\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043f\u043e \u0442\u0435\u043e\u0440\u0438\u0438, \u043d\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u0430\u043b\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u043e\u0432 \u043f\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f IOA. \u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0431\u0435\u043b.<\/p>\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440\u044b<\/h3>\n<p>  <\/p>\n<h4>\u00ab\u041b\u0435\u043d\u0438\u0432\u0430\u044f\u00bb (\u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f) \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c (\u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c) \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u043e \u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c.<\/p>\n<p>  \u041a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">\/\/ \u0436\u0434\u0435\u043c \u043f\u043e\u043b\u043d\u043e\u0439 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b window.onload = () =&gt; {     \/\/ \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438     const options = {         \/\/ \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 - \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430         root: null,         \/\/ \u0431\u0435\u0437 \u043e\u0442\u0441\u0442\u0443\u043f\u043e\u0432         rootMargin: '0px',         \/\/ \u043f\u0440\u043e\u0446\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u044f - \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f         threshold: 0.5     }      \/\/ \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044c     const observer = new IntersectionObserver((entries, observer) =&gt; {         \/\/ \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438-\u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430         entries.forEach(entry =&gt; {             \/\/ \u0435\u0441\u043b\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c\u044b\u043c             if (entry.isIntersecting) {                 const lazyImg = entry.target                 \/\/ \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c - \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u0435\u043b\u044f                 console.log(lazyImg)                 \/\/ \u043c\u0435\u043d\u044f\u0435\u043c \u0444\u043e\u043d \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430                 lazyImg.style.background = 'deepskyblue'                 \/\/ \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435                 observer.unobserve(lazyImg)             }         })     }, options)      \/\/ \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0446\u0438\u043a\u043b\u0430 \u0441\u043b\u0435\u0434\u0438\u043c \u0437\u0430 \u0432\u0441\u0435\u043c\u0438 img \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435     const arr = document.querySelectorAll('img')     arr.forEach(i =&gt; {         observer.observe(i)     }) } <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/wc\/bw\/gj\/wcbwgjht9d-l_jg9jhvkhrfz8ja.png\"><\/p>\n<p>  \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0444\u043e\u043d \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u0431\u0435\u043b\u044b\u0439.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/oy\/4a\/il\/oy4ail-hjedm98mczcp8q4xqphc.png\"><\/p>\n<p>  \u041f\u0440\u0438 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0438 \u0441 \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u043d\u0430\u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443, \u0444\u043e\u043d \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u0431\u0435\u0441\u043d\u043e-\u0433\u043e\u043b\u0443\u0431\u043e\u0439.<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/jOPdEwN\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-1.html\" rel=\"nofollow\">Github<\/a><\/p>\n<h4>\u0417\u0430\u043c\u0435\u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u043c\u0435\u043d\u044f\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435-\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c.<\/p>\n<p>  \u041a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">window.onload = () =&gt; {     const observer = new IntersectionObserver((entries, observer) =&gt; {         entries.forEach(entry =&gt; {             if (entry.isIntersecting) {                 console.log(entry)                 \/\/ \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0435 &quot;data-src&quot;                 entry.target.src = entry.target.dataset.src                 observer.unobserve(entry.target)             }         })     }, { threshold: 0.5 })      document.querySelectorAll('img').forEach(img =&gt; observer.observe(img)) } <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/gi\/pv\/7k\/gipv7krnnqas_kupmxoiihebbwc.png\"><\/p>\n<p>  \u041f\u0435\u0440\u0432\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430. \u0412\u0442\u043e\u0440\u043e\u0435 \u2014 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sw\/os\/ln\/swoslnwz85d5gmihs7ypm3nniv8.png\"><\/p>\n<p>  \u041f\u0440\u0438 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/MWwLYrK\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-2.html\" rel=\"nofollow\">Github<\/a><\/p>\n<h4>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0444\u043e\u043d\u0430 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u043c\u0435\u043d\u044f\u0442\u044c \u0444\u043e\u043d \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u043f\u0440\u0438 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u0442\u0443\u0434\u0430 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e.<\/p>\n<p>  \u041a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">window.addEventListener('load', event =&gt; {     let box = document.querySelector('div')     \/\/ ratio - \u043f\u0440\u043e\u0446\u0435\u043d\u0442 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430     let prevRatio = 0.0      let observer = new IntersectionObserver((entries, observer) =&gt; {         entries.forEach(entry =&gt; {             let curRatio = entry.intersectionRatio                          \/\/ \u043f\u0440\u0438 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0435 \u0446\u0432\u0435\u0442 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u0432\u0435\u0442\u043b\u043e-\u0441\u0438\u043d\u0435\u0433\u043e \u0434\u043e \u0441\u0432\u0435\u0442\u043b\u043e-\u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e             \/\/ \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442 \u043f\u0440\u0438 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0432 \u043e\u0431\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u044b (\u0432\u043d\u0438\u0437 \u0438 \u0432\u0432\u0435\u0440\u0445)            \/\/ \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u0437\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f             curRatio &gt; prevRatio ? entry.target.style.background = `rgba(40,40,190,${curRatio})` : entry.target.style.background = `rgba(190,40,40,${curRatio})`              prevRatio = curRatio         })     }, {         threshold: buildThresholdList()     })      observer.observe(box)          \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0448\u043a\u0430\u043b\u044b \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u044f     \/\/ \u0448\u043a\u0430\u043b\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 20 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0449\u0438\u0445 \u0446\u0432\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430     function buildThresholdList() {         let thresholds = []         let steps = 20          for (let i = 1.0; i &lt;= steps; i++) {             let ratio = i \/ steps             thresholds.push(ratio)         }         return thresholds     } }) <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/33\/c3\/p7\/33c3p7c8bxsbyn-h70xbr77ycwq.png\"><\/p>\n<p>  \u0424\u043e\u043d \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u0432\u0435\u0442\u043b\u043e-\u0441\u0438\u043d\u0435\u0433\u043e\u2026<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ns\/s5\/by\/nss5byni4diubr1zjhuqf5unvvg.png\"><\/p>\n<p>  \u0447\u0435\u0440\u0435\u0437 \u0441\u0438\u043d\u0438\u0439\u2026<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/jc\/xy\/cc\/jcxyccqyaxzyszcd99rmv8r0gju.png\"><\/p>\n<p>  \u0434\u043e \u0441\u0432\u0435\u0442\u043b\u043e-\u043a\u0440\u0430\u0441\u043d\u043e\u0433\u043e.<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/RwPvNMY\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-3.html\" rel=\"nofollow\">Github<\/a><\/p>\n<h4>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0432\u0438\u0434\u0435\u043e<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043d\u043e\u0435 \u0432\u0438\u0434\u0435\u043e \u043d\u0430 \u043f\u0430\u0443\u0437\u0443 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u043d\u043e\u0432\u0430 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u0432\u0438\u0434\u0435\u043e \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430.<\/p>\n<p>  \u041a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">window.onload = () =&gt; {     let video = document.querySelector('video')      let observer = new IntersectionObserver(() =&gt; {         \/\/ \u0435\u0441\u043b\u0438 \u0432\u0438\u0434\u0435\u043e \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043e         if (!video.paused) {             \/\/ \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u043d\u0438\u0435             video.pause()         \/\/ \u0435\u0441\u043b\u0438 \u0432\u0438\u0434\u0435\u043e \u0431\u044b\u043b\u043e \u0437\u0430\u043f\u0443\u0449\u0435\u043d\u043e \u0440\u0430\u043d\u0435\u0435 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u043d\u0438\u044f &gt; 0)         } else if(video.currentTime != 0) {             \/\/ \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u043d\u0438\u0435             video.play()         }     }, { threshold: 0.4 })      observer.observe(video) } <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/hg\/pi\/vn\/hgpivnoh5k5w3jc1hrlthmlipec.png\"><\/p>\n<p>  \u041f\u043e\u043a\u0430 \u0432\u0438\u0434\u0435\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430, \u043e\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/fc\/ve\/jp\/fcvejps5f63bokfpevw3vn5fcos.png\"><\/p>\n<p>  \u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0438\u0434\u0435\u043e \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c \u043d\u0430 40%, \u0435\u0433\u043e \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f. \u041f\u0440\u0438 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u0438 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 &gt; 40% \u0432\u0438\u0434\u0435\u043e, \u0435\u0433\u043e \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/oNXmgPd\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-4.html\" rel=\"nofollow\">Github<\/a><\/p>\n<h4>\u041f\u0440\u043e\u0433\u0440\u0435\u0441\u0441 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u043e \u043c\u0435\u0440\u0435 \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0438 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c.<\/p>\n<p>  \u041a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">\/\/ \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0438 \u043f\u0430\u0440\u0430\u0433\u0440\u0430\u0444\u0430 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0430 let p = document.querySelector('p') \/\/ n - \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 let n = 0  let observer = new IntersectionObserver((entries, observer) =&gt; {     entries.forEach(entry =&gt; {         if(entry.isIntersecting){             \/\/ observer \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u0442 \u0437\u0430 div             \/\/ \u0438 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043e\u0431 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432             \/\/ \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u044d\u0442\u0443 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0432 \u043f\u0430\u0440\u0430\u0433\u0440\u0430\u0444             p.textContent = `${n++} div viewed`             observer.unobserve(entry.target)         }     }) }, {threshold: 0.9})  document.querySelectorAll('div').forEach(div =&gt; observer.observe(div)) <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ga\/fy\/ff\/gafyffz_tgkuiuhjuudi6q4qfke.png\"><\/p>\n<p>  \u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043b\u0430\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0435\u0449\u0435 \u043d\u0435 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sl\/pm\/4-\/slpm4-wrzhfhowjpb7fyjwuaeho.png\"><\/p>\n<p>  \u041f\u0440\u0438 \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u0438 \u043a\u043e\u043d\u0446\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0432 \u043f\u0430\u0440\u0430\u0433\u0440\u0430\u0444 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435 4 div.<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/NWqoPEW\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-5.html\" rel=\"nofollow\">Github<\/a><\/p>\n<h4>\u0411\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0430<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a.<\/p>\n<p>  \u041a\u043e\u0434: <\/p>\n<pre><code class=\"javascript\">let ul = document.querySelector('ul') let n = 1  \/\/ \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 function createLi(){     li = document.createElement('li')     li.innerHTML = `${++n} item`     ul.append(li) }  \/\/ \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0430 \/\/ \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043d\u0435\u0447\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \/\/ \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u0446\u0435\u043b\u0435\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0433\u043e li \/\/ \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u044d\u0442\u0438\u043c \u043d\u043e\u0432\u044b\u043c (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c let observer = new IntersectionObserver((entries, observer) =&gt; {     entries.forEach(entry =&gt; {         if (entry.isIntersecting) {             createLi()         }         observer.unobserve(entry.target)         observer.observe(document.querySelector('li:last-child'))     }) }, {     threshold: 1 })  observer.observe(document.querySelector('li')) <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/hp\/5j\/wr\/hp5jwr9z4phefr2fpmej2qbrg-m.png\"><\/p>\n<p>  \u0418\u043c\u0435\u0435\u043c 12 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/wu\/3_\/dd\/wu3_ddgx3ddnhkvv7u_dlq8w3hc.png\"><\/p>\n<p>  \u041f\u0440\u0438 \u043f\u043e\u043f\u044b\u0442\u043a\u0435 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439) \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0441\u043a\u0440\u044b\u0442\u044b\u0439 \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. \u0418 \u0442\u0430\u043a \u0434\u043e \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/MWwLYZb\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-6.html\" rel=\"nofollow\">Github<\/a><\/p>\n<h4>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432 \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043e\u0442 \u0434\u0440\u0443\u0433\u043e\u0433\u043e.<\/p>\n<p>  \u041a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">\/\/ \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 - \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0438 \u0440\u0435\u0431\u0435\u043d\u043e\u043a \/\/ \u0438 \u043f\u0430\u0440\u0430\u0433\u0440\u0430\u0444 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0448\u0438\u0440\u0438\u043d\u044b \u0440\u0435\u0431\u0435\u043d\u043a\u0430 let info = document.querySelector('.info') let parent = document.querySelector('.parent') let child = document.querySelector('.child') \/\/ \u043e\u0442\u043d\u0438\u043c\u0430\u0435\u043c \u043e\u0442 \u0448\u0438\u0440\u0438\u043d\u044b \u0440\u0435\u0431\u0435\u043d\u043a\u0430 50px \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 child.style.width = parent.offsetWidth - 50 + 'px' \/\/ \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u0448\u0438\u0440\u0438\u043d\u0443 \u0440\u0435\u0431\u0435\u043d\u043a\u0430 \u0432 \u043f\u0430\u0440\u0430\u0433\u0440\u0430\u0444 info.textContent = `child width: ${child.offsetWidth}px`  let options = {     \/\/ \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 \u0434\u043b\u044f \u0440\u0435\u0431\u0435\u043d\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c     root: parent,     threshold: 1 }  let observer = new IntersectionObserver((entries, observer) =&gt; {     entries.forEach(entry =&gt; {         \/\/ \u0435\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c \u0438 \u0440\u0435\u0431\u0435\u043d\u043a\u043e\u043c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 50px         if ((entry.target.parentElement.offsetWidth - entry.target.offsetWidth) &lt; 50) {             \/\/ \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0448\u0438\u0440\u0438\u043d\u0443 \u0440\u0435\u0431\u0435\u043d\u043a\u0430 \u043d\u0430 50px             entry.target.style.width = entry.target.offsetWidth - 50 + 'px'         }     }) }, options)  observer.observe(child)  \/\/ \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u043d\u0435 \u0434\u043e\u0434\u0443\u043c\u0430\u043b\u0441\u044f, \u043a\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u044d\u0444\u0444\u0435\u043a\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e IOA \/\/ \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b \u0435\u0433\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 resize window.addEventListener('resize', () =&gt; {     info.textContent = `child width: ${child.offsetWidth}px`     if ((parent.offsetWidth - child.offsetWidth) &gt; 51) {         child.style.width = child.offsetWidth + 50 + 'px'     } }) <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/34\/pe\/kv\/34pekvhjmlphqphebl7qkvg5leo.png\"><\/p>\n<p>  \u0418\u0441\u0445\u043e\u0434\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/pv\/nb\/-c\/pvnb-cvzrsst6sv2_rtwremxpyy.png\"><\/p>\n<p>  \u041f\u0440\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0438 \u0448\u0438\u0440\u0438\u043d\u044b \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u0448\u0438\u0440\u0438\u043d\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0430\u0432\u043d\u044f\u0435\u0442\u0441\u044f 50px (\u00ab\u043f\u043e\u0447\u0442\u0438\u00bb \u043e\u0431\u0443\u0441\u043b\u043e\u0432\u043b\u0435\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430).<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/NWqoPJP\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-7.html\" rel=\"nofollow\">Github<\/a><\/p>\n<h4>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0435\u0439<\/h4>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430: \u0430\u043d\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u0440\u0438 \u0435\u0433\u043e \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u041a\u043e\u0434:<\/p>\n<pre><code class=\"javascript\">\/\/ \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0411\u0430\u0440\u0442 \u0421\u0438\u043c\u043f\u0441\u043e\u043d \u0438 \u0434\u0432\u0435 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438 \/\/ \u043e\u0434\u043d\u0430 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442 \u0411\u0430\u0440\u0442\u0430 \u0432\u043b\u0435\u0432\u043e \/\/ \u0434\u0440\u0443\u0433\u0430\u044f - \u0432\u043f\u0440\u0430\u0432\u043e let observer = new IntersectionObserver((entries, observer) =&gt; {     entries.forEach(entry =&gt; {         entry.isIntersecting ? entry.target.classList.replace('to-left', 'to-right') : entry.target.classList.replace('to-right', 'to-left')     }) }, {     threshold: .5 })  observer.observe(document.querySelector('img')) <\/code><\/pre>\n<p>  \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ht\/rb\/wg\/htrbwgvco_cyimtj_5gcreb2amc.png\"><\/p>\n<p>  \u041c\u044b \u0432\u0438\u0434\u0438\u043c \u0447\u0430\u0441\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0411\u0430\u0440\u0442\u0430. \u0411\u0430\u0440\u0442 \u043f\u0440\u0438\u0436\u0430\u043b\u0441\u044f \u043a \u043b\u0435\u0432\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/q9\/hk\/hu\/q9hkhu22kircx6sefzyolg7ivzi.png\"><\/p>\n<p>  \u041f\u0440\u0438 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u0438 \u0431\u043e\u043b\u0435\u0435 50% \u0411\u0430\u0440\u0442\u0430 \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430, \u043e\u043d \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443. \u041f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0431\u043e\u043b\u0435\u0435 50% \u0411\u0430\u0440\u0442\u0430 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435.<\/p>\n<p>  <a href=\"https:\/\/codepen.io\/igor_agapov\/pen\/rNVPabw\" rel=\"nofollow\">Codepen<\/a><\/p>\n<p>  <a href=\"https:\/\/github.com\/harryheman\/intersection-observer-examples\/blob\/master\/intersection-example-8.html\" rel=\"nofollow\">Github<\/a><\/p>\n<p>  \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435.<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/494670\/\"> https:\/\/habr.com\/ru\/post\/494670\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/494670\/\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/p9\/ub\/-0\/p9ub-06qrmugtoafjurwfqm7jp4.jpeg\"><\/p>\n<p>  \u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a, \u0434\u0440\u0443\u0437\u044c\u044f!<\/p>\n<h3>\u041e\u0431\u0437\u043e\u0440<\/h3>\n<p>  Intersection Observer API (IOA) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 (target) \u0441 \u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c (root) \u0438\u043b\u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430 (viewport). \u0414\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u044d\u0442\u043e\u0442 API \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u0437\u043e\u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 root \u0438\u043b\u0438 viewport. <\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\u00ab\u043b\u0435\u043d\u0438\u0432\u0430\u044f\u00bb \u0438\u043b\u0438 \u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439<\/li>\n<li>\u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u0430\u044f \u043f\u0440\u043e\u043a\u0440\u0443\u0442\u043a\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b<\/li>\n<li>\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u0435\u043a\u043b\u0430\u043c\u044b \u0434\u043b\u044f \u0446\u0435\u043b\u0435\u0439 \u0440\u0430\u0441\u0447\u0435\u0442\u0430 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u043a\u0430\u0437\u043e\u0432<\/li>\n<li>\u0437\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438\u043b\u0438 \u0430\u043d\u0438\u043c\u0430\u0446\u0438\u0438, \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u0445\u0441\u044f \u0432 \u043f\u043e\u043b\u0435 \u0437\u0440\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f<\/li>\n<\/ul>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-301019","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=301019"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301019\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}