@
@
@Richswitch2017-09-24 20:50:11
JavaScript
@Richswitch, 2017-09-24 20:50:11

Compute element coordinates relative to window?

Hey!
I no longer understand at all. So the question is for you...
Adding animation to elements on scroll with onscroll and validation. The check will be as follows:
- if the window has scrolled more than 300px from the top point of the element
- then add animation
The code is as follows:

<div class="item "></div>
  <div class="item "></div>
  <div class="item "></div>

var getCoord = document.getElementsByClassName('item');

document.onscroll = function(e) {
  scrollAdd();
};

function scrollAdd() {
  var elemCoords = getCoord[0].getBoundingClientRect().top;
  console.log(getCoord[0].getBoundingClientRect().top + '   1 element');
  console.log(window.pageYOffset + '   offset');
         
        var someVar =  ****Здесь должно что-то быть**** ;

  if (someVar > 300) {
    console.log('if');
    getCoord[0].classList.add('animation');
  }
};

972a41f7b7f04102898ade1f31200a78.png
UPD the elements themselves are 370px high i.e. element should appear > than 150px

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DTX, 2017-09-24
_

innerHeight - screen height
how much has the user scrolled

if (scrollPos + window.innerHeight > el.centerY) addAnimation()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question