0

So, in my HTML I've got a class called .myFeature_info, along with that I've included the following script at the end of the <body> tag.

I'm wondering why the function is not being called while I am scrolling.

Here's the code, I'd love to know the 'why', I'd love to use JS only.

    window.addEventListener('scroll', function(e) {
    function isInViewPort (){
    var myFeature_info = document.getElementsByClassName('myFeature_info')[0];
    var getPos = myFeature_info.getBoundingClientRect();
    console.log(getPos);
  }
});
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Daniel
  • 60
  • 6

1 Answers1

2

You are not calling any function on scroll but defining a function! Separate the definition and call it as you scroll:

function isInViewPort (){
var myFeature_info = document.getElementsByClassName('myFeature_info')[0];
var getPos = myFeature_info.getBoundingClientRect();
console.log(getPos);
}

window.addEventListener('scroll', function(e) {
 isInViewPort()
}
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • Thanks a LOT. I can't believe I didn't catch that. I guess I should take a little break, lol. Thanks, again. – Daniel Mar 10 '18 at 21:55
  • Or, forget about the function name and just put the function body (from `var myFeature_info`, etc) in the event listener. – Mr Lister Mar 12 '18 at 12:11