简体   繁体   English

检测滚动条何时出现在德克萨斯州

[英]Detecting when a scrollbar appears on texarea

I know this question has been asked here . 我知道这里已经问这个问题。 Basically the solution works but they are using an old version of jquery and the solution was implemented using deprecated functions. 基本上,该解决方案有效,但它们使用的是旧版本的jquery,并且该解决方案是使用不推荐使用的功能实现的。 So I tried to migrate to the new version, according to answers and questions from this question . 因此,根据该问题的答案和问题,我尝试迁移到新版本。

So basically the solution using the old jquery version is this one. 因此,基本上,使用旧版jquery的解决方案是此解决方案。

$('textarea.paginate').live('keydown', function(event) {
    if (this.clientHeight < this.scrollHeight) {
        alert("Please Something);
    }
});

This was my migration to the on function, but neither way works. 这是我向on函数的迁移,但两种方法均无效。 Demo of the example: http://jsbin.com/saxay/1/edit 该示例的演示: http : //jsbin.com/saxay/1/edit

What I'm doing wrong? 我做错了什么?

$("textarea").on('keyup','.note-codable',function(event){
  if(event.keyCode == 13) { }
  if (this.clientHeight < this.scrollHeight) {
    alert("Please Something");
  }
});

$(document).on('keyup','.note-codable',function(event){
  if(event.keyCode == 13) { }
  if (this.clientHeight < this.scrollHeight) {
    alert("Please Something");
  }
});

UPDATED 更新

try this approach: DEMO 试试这个方法: DEMO

$.fn.hasVerticalScrollBar = function() {
    if (this[0].clientHeight < this[0].scrollHeight) {
        return true
    } else {
        return false
    }
};

$(document).on('keydown','.note-codable',function(event){
    if(event.keyCode == 13) { }
    if ($(this).hasVerticalScrollBar()) {
        alert("Please Something");
    }
});

also if you notice I've changed the keyup event to keydown which is better in my opinion 'cause when the user holds their finger down on a button the code wouldn't be fired if it is on the keyup event. 另外,如果您注意到我将keyup事件更改为keydown ,这在我看来更好,因为用户将手指按住按钮时,如果在keyup事件上不会触发代码。

Fixed your problem 解决了您的问题

You just had to use .on at the place of live in your first approach. 您只需在第一种方法中在live位置使用.on

$('textarea.paginate').on('keydown', function(event) {

    // scrollbars apreared
    if (this.clientHeight < this.scrollHeight) {

       alert("Please add a new card for having a better format. Remember this is a WYSIWYG");

    }

});

DEMO 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM