E
E
Evgeniy2021-07-18 20:42:06
JavaScript
Evgeniy, 2021-07-18 20:42:06

How to track the distance from the top of the page to the block?

Good afternoon, please tell me:

How can I track the distance from the top of the page to the block when scrolling.
In order for the script to connect when it reaches the block...
Now it connects only if the page is refreshed in the position where the distance is exactly or less than 1400px

const block = document.querySelector('.block');
  
// Получаем верхнюю координату блока относительно окна браузера
const getItemTopCoords = block.getBoundingClientRect().top;
  
if (getItemTopCoords + 100 <= 1400) {
  const script = document.createElement('script');
  script.src = './js/icon.js';
  document.body.appendChild(script);  
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2021-07-18
@Zheleznov

window.addEventListener('scroll', function _scrollhandler() {
  // ... ваш код проверки
  window.removeEventListener('scroll', _scrollhandler)
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question