How to detect when the scroll of an element reaches the end without jQuery?

Asked

Viewed 996 times

0

I’m not able to understand this jQuery code.

How can I convert the following code into Javascript?

$(document).ready(function() {
    $("#table").scroll(function() { 
        if ($(this).scrollTop() + $(this).height() == $(this).get(0).scrollHeight) {
            console.log("final")
        } 
    }); 
});
  • 2

    To do this without jQuery would be somewhat laborious and complicated, although perfectly possible. Wouldn’t rather learn how jQuery works rather than how you would eliminate it?

  • 2

    I can’t use framework, I have to use pure javascript

  • 2

    I believe that someone will answer the question to you, but more importantly to have an answer to this, I will warn you: If you use jQuery, and do not know how to do with Javascript, I believe this is a problem.

  • 3

    You know what this code is doing?

  • 2

    The code will become much more complicated and difficult to understand if you do not use Jquery. One of Jquery’s main objectives is to simplify the selection of DOM elements, which is complicated. See this article: https://code.tutsplus.com/tutorials/from-jquery-to-javascript-a-reference-net-23703

  • @Williamjohnadamtrinity on this I disagree. It’s been a while since jQuery is just about simplifying things. My answer for example, I spent the same amount of lines to do something similar.

  • 2

    @Wallacemaxters "jQuery is just to simplify things" it’s been a while since :) it doesn’t do anything that pure javascript doesn’t do, but anyway, simplifying is enough is not even?

  • 3

    As @Wallacemaxters commented, if you don’t know what the code does, it’s not an aid to convert the code, but rather, "convert that code to me," and even javascript pure will continue without understanding, it would be cool to debug and see what each part does, so you’ll probably be able to convert next time :)

  • 2

    @Wallacemaxters What was the reason for creating Jquery? It is a library written in JS to simplify scritps written in JS (a bit redundant, but that’s what it is). You will tell me that Document.getElementById( 'Test' ).value = 5; it is simpler than $( '#Test' ).val( 5 ); ?

  • 1

    @Giovanniestevam Jquery is not a framework. It is a library. And no, it is not the same.

  • 1

    Anyway, but you understand that you can survive without jQuery, right? The purpose of jQuery was really to facilitate, but I believe that Javascript has features that dispense many things nowadays. See here you don’t need jQuery :D

  • @Wallacemaxters Yes, for this instead of Jquery, use framewok Vanilla JS :) (I hope you understand the reference)

  • @Williamjohnadamtrindade is here What is Vanilla?

Show 8 more comments

1 answer

3


I think something closer would be like below, but I would need to know more details about why you need this code.

window.addEventListener('load', function () {

    document.querySelector("#table").addEventListener('scroll', function() { 

        if (this.scrollTop + this.offsetHeight == this.scrollHeight) {
               console.log('final')
        } 
    }); 

})

Just a small fix: jQuery is Javascript, not a language. In this case, you are not "converting to javascript", but using pure Javascript without the library.

I made a better example to check:

window.addEventListener('load', function () {

    document.querySelector("#table").addEventListener('scroll', function() { 
    
        if (Math.ceil(this.scrollTop) + this.offsetHeight == this.scrollHeight) {
               console.log('final')
        } 
    }); 

})
#table{
   max-height: 150px;

   background-color: #ddd;
   
   overflow: auto;
}
<div id="table">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
</div>

  • 2

    Until it wasn’t as complicated as I thought it would be.

  • Yes, it was simple

  • Note: No Snipet, not working sum, kkkkkk. That’s why I used Math.ceil

  • 2

    As I commented above: Jquery is not a framework, it is a library. Remembering that querySelector is only available after IE9, but as we are in 2018, no one else cares. :)

  • 1

    @Williamjohnadamtrindade already mentioned: https://answall.com/questions/17501

  • @Williamjohnadamtrindade may be wrong, but where did you get that querySelector does not work in IE9? For if it exists since IE8.

  • 2

    @Wallacemaxters how about switching window.addEventListener('load' for document.addEventListener('DOMContentLoaded'?

  • @Guilhermenascimento IE8 the implementation was partial. I only considered the version where the implementation was full. See Caniuse : https://caniuse.com/#feat=queryselector

  • 1

    @Williamjohnadamtrindade you said IE9, in this has no notes in the caniuse, so the comment was still wrong, sorry for the previous correction, but that’s it. Aside from the fact that reading the partial implementation note does not mean that it does not work, or that it works 10% considering the selectors supported by the IE8 CSS itself, probably the partial there is a 90% support, so much so that just read the note, IE8’s problem is only with unrecognized tags (HTML5 tags for example, which was a CSS engine problem in IE8 as well, just like in JS engine), ie other selectors validated in IE8 will be OK ;)

  • @Guilhermenascimento Only in cases where it did not work on IE8 or you used Jquery or Voce you used a pollyfill (e.g., https://gist.github.com/chrisjlee/8960575). The discussion here is over, since when "we can" dispense with Jquery.

  • 1

    @Williamjohnadamtrindade I’m not even getting into the jQueryxVanillaJS discussion, this was your and Wallace’s debate, the focus is, you commented on something and I pointed out that info was a mistake on your part, which I aim to try to collaborate with the community, fix what I see as deception, I know it was not a bad thing, but you must understand and know how to separate the tares from the wheat when someone corrects some information, my comment there exclusively correct some information, which is up to say at most: "Truth, my mistake, then consider only the other part" and we would have the debate ended there. See you around :)

  • @Williamjohnadamtrindade can chat with us on chat?

  • For the record, Wallace, I would == for >=, I believe that this would already solve the problem, without needing to Math.ceil, but I haven’t tested it yet to be sure.

  • @Guilhermenascimento tried this before putting the Math.Ceil :\

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.