Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

intersection observer api

let options = {
  root: document.querySelector('#scrollArea'),
  rootMargin: '0px',
  threshold: 1.0
}

let observer = new IntersectionObserver(callback, options);
// 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.
Comment

intersection observer API

//first we need to create new intersection observer
//we pass there callback function and object of options
//callback function will get called every time observed (target) element 
//intersecting the root element at the threshold that we defined.
//function will called with 2 arguments: threshold entries and observer
const header = document.querySelector(".header");
const navHeight = nav.getBoundingClientRect().height;

const stickyNav = function (entries) {
  const [entry] = entries;
  console.log(entry);
  if (!entry.isIntersecting) {
    nav.classList.add("sticky");
  } else {
    nav.classList.remove("sticky");
  }
};

const obsOptions = {
  root: null,
  threshold: 0, //when element is hidden from viewport
  rootMargin: `-${navHeight}px`,
};

const headerObserver = new IntersectionObserver(stickyNav, obsOptions);
headerObserver.observe(header);

//Stop observing
//Observer is argument in callback function
observer.unobserve(entry.target);
Comment

intersection observer api root

const obsCallback = function (entries) {
  entries.forEach(el => console.log(el));
}

const obsOption = {
  root: null,
  threshold: 0.1,
}

const observer = new IntersectionObserver(obsCallback, obsOption);
observer.observe(section1); //Target Section
Comment

PREVIOUS NEXT
Code Example
Javascript :: legend on click use default chartjs 
Javascript :: Check the render method of `App` 
Javascript :: longitud objeto javascript 
Javascript :: ios react native detect locale 
Javascript :: coderbyte first factorial solutions 
Javascript :: reduce to calculate sum react 
Javascript :: javascript telegram bot 
Javascript :: expresiones ternarias javascript 
Javascript :: cypress visible 
Javascript :: dynamic regex javascript 
Javascript :: arrow functions in javascript 
Javascript :: createelement with id 
Javascript :: importing svg into cra 
Javascript :: how to loop through a map in js 
Javascript :: how to build jquery post data 
Javascript :: how to use the match function in javascript for regex 
Javascript :: connect node server with knex database 
Javascript :: react if statement 
Javascript :: how to make a alert popup message in javascript 
Javascript :: react native flex 2 columns per row 
Javascript :: axios defaults headers common 
Javascript :: js base64 encode 
Javascript :: get url 
Javascript :: capitalize each word from string in react 
Javascript :: remove storybook from project 
Javascript :: js range array 
Javascript :: salvar no localStorage react 
Javascript :: math captcha 
Javascript :: HashRouter 
Javascript :: how to set selected value of dropdown in javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =