Learn-Full-Stack / Javascript

0 stars 0 forks source link

Day-1 Intersection , Resizer and Mutation Observer #1

Open purushothaman-source opened 1 year ago

purushothaman-source commented 1 year ago

Intersection Observer

Use cases : from lazy loading images, to scroll based animations

Blog

https://blog.webdevsimplified.com/2022-01/intersection-observer/

Videos

Syntax

const observer = new IntersectionObserver(entries => {
  console.log(entries)
},{ threshold : 0 ,root:<container/> , rootMargin : "50px"})

Resizer Observer

Resizer Observer

const observer = new ResizeObserver(entries => {
  entries.forEach(entry => {
    const isSmall = entry.contentRect.width < 150
    entry.target.style.backgroundColor = isSmall ? "blue" : "orange"
  })
})

observer.observe(document.getElementById("test"))

Blog

https://blog.webdevsimplified.com/2022-01/resize-observer/

Mutation Observer

purushothaman-source commented 1 year ago

Using Intersection Observer we can observer the changes to any html elements ..the callback function will get fired when the elements state changes whether it is moving out side the screen or into the screen this function will get trigger..

Screenshot from 2022-11-09 09-08-59

we will get these field when the callback triggers

Screenshot from 2022-11-09 09-08-42

purushothaman-source commented 1 year ago

Threshold

By default Threshold will be 0.. it means the callback function will get a trigger as soon as 1px get into the screen or move out of the screen..

Possible values : 0 to 1 ..

1 means function will trigger only when full element is visible or full element left the screen

You can also pass an array to threshold which means that the Intersection Observer will fire each time your element passes one of the thresholds passed to it.

const observer = new IntersectionObserver(entries => {
  entires.forEach(entry => {
    entry.target.innerText = `${Math.round(entry.intersectionRatio * 100)}%`
  })
}, { threshold: [0, .25, .5, .75, 1] })
purushothaman-source commented 1 year ago

Root Margin

It is used to shrink or enlarge the container size -ve value will shrin +ve value will enlarge ..this will be useful to fetch or animate the elements before its visble

const observer = new IntersectionObserver(
  changeColor,
  { rootMargin: "50px" }
)
purushothaman-source commented 1 year ago

Root

It is actually used to use the custom container instead of body


const observer = new IntersectionObserver(
  changeColor,
  { root: <container> }
)
``
purushothaman-source commented 1 year ago

Unobserve and Disconnect

It is important to stop observing elements when they no longer need to be observed, such as after they are removed from the page or after lazy loading an image in order to avoid memory leaks or performance issues. This can be done with the unobserve method or the disconnect method which are both methods on the Intersection Observer. The unobserve method takes a single element as its only parameter and it stops observing that single element. The disconnect method takes no parameters and will stop observing all elements.

new IntersectionObserver((entries, observer) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      lazyLoadImage(entry)
      observer.unobserve(entry.target)
    }
  })
})