Search code examples
javascriptcodemirror

Determine if CodeMirror is scrolled to bottom


I'm building a live-preview editor with CodeMirror. I need to determine if the CodeMirror editor is scrolled to the very bottom so that I can scroll the preview to the bottom as well.

How can I determine this?


Solution

  • You need the scroller element in codeMirror, then bind a function on scroll event.

    jsfiddle

    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: "text/html"
    });    
    
    var scrollElement = editor.getScrollerElement();
      console.log(scrollElement )
      $(scrollElement).bind('scroll', function(e) {
          var elem = $(e.currentTarget);
          if (elem[0].scrollHeight - elem.scrollTop() == elem.outerHeight()) {
              console.log("bottom");
          }      
      });