如何在 Live() 上绑定滚动事件?

发布于 2024-10-13 05:48:47 字数 1430 浏览 8 评论 0原文

不久前,我为某人解决了一个问题 希望他的文本区域变大。我创建了一个函数来侦听该区域的 scrollkeyup 事件并重新计算行数。我想在另一个项目中使用该代码,但出现了问题。文本区域不知道为了解决这个问题,我使用 live 而不是 bind,这样将来的区域也将被绑定。

现在我发现 live 的执行速度比 bind 慢很多。我创建了 jsFiddle 上的简化示例。上面的文本区域的行为符合我的要求,但由于信号延迟(我使用的是 Chrome),新添加的文本区域会闪烁。

如何使livebind一样快?问题在于scroll< /code> 不能与 live 语句一起使用。 有没有办法为live启用scroll是否有一个jQuery事件通知我已经添加了一个新的TextArea,所以我可以使用绑定在新创建的元素上添加 scroll 吗?

我期待您的想法。

编辑:更改了代码的链接。删除了滚动代码。添加了另一个按钮来创建不同的文本区域。问题与“滚动”有关。它不着火。

澄清:我不知道什么函数将创建文本区域。我看到 Chrome 中动态添加的框闪烁。

对于未来的读者:

在 jQuery 1.3.x 中只有以下内容 JavaScript 事件(除了 自定义事件)可以绑定 .live(): 单击、dblclick、keydown、 按键、键盘向上、鼠标按下、鼠标移动、 mouseout、mouseover 和 mouseup。作为 jQuery 1.4 的 .live() 方法 支持自定义事件以及所有 冒泡的 JavaScript 事件。截至 jQuery 1.4.1 甚至可以进行焦点和模糊工作 与现场(映射到更多 适当的、冒泡的、事件集中的 和对焦)。从 jQuery 1.4.1 开始 可以指定悬停事件(映射 mouseenter 和 mouseleave,其中, 依次映射到鼠标悬停和 鼠标移开)。

A while ago I've solve an issue for someone that wanted his textarea to grow. I've made a function that listens to the scroll and keyup events of the area and recalculates the number of rows. I wanted to use the code in another project, but there's a problem. The textarea's are not know To solve this, I'm using live instead of bind, so that future area's will be bound as well.

Now I'm finding that the live executes a lot slower than a bind. I've created a simplified example on jsFiddle. The upper textarea behaves as I want, but newly added ones flicker due to the late signaling (I'm using Chrome).

How can I make the live as fast as the bind? The problem is that the scroll can't be used with a live statement. Is there a way to enable scroll for live? Is there maybe a jQuery event that signals me that a new TextArea has been added, so I can use a bind to add the scroll on the newly created element?

I'm looking forward to your ideas.

EDIT: Changed link to the code. Removed scrollingCode. Added another button to create a different textarea. The problem has to do with 'scroll'. It doesn't fire.

Clarification: I will not know what function will create the textarea's. I see flickering on the dynamically added boxes in Chrome.

For future readers:

In jQuery 1.3.x only the following
JavaScript events (in addition to
custom events) could be bound with
.live(): click, dblclick, keydown,
keypress, keyup, mousedown, mousemove,
mouseout, mouseover, and mouseup
. As
of jQuery 1.4 the .live() method
supports custom events as well as all
JavaScript events that bubble. As of
jQuery 1.4.1 even focus and blur work
with live (mapping to the more
appropriate, bubbling, events focusin
and focusout). As of jQuery 1.4.1 the
hover event can be specified (mapping
to mouseenter and mouseleave, which,
in turn, are mapped to mouseover and
mouseout).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星星的轨迹 2024-10-20 05:48:47

答案很简单。 scroll 可以防止闪烁,因为它会在调整大小的第一刻触发。但是 scrolllive 没有影响(因为它不会冒泡),因此您新创建的文本区域将在 keyup 上调整大小,但会触发稍后(因此闪烁)。

更新:当然我什至可以解决你的问题。你只需要问:) [演示]

$('textarea.autoresize').live('keyup', function() {
    var el = $(this);
    if (!el.data("has-scroll")) {
        el.data("has-scroll", true);
        el.scroll(function(){
           resizeTextArea(el);
        });
    }
    resizeTextArea(el);
});

重点是,它将livebind 混合。 keyup 事件会在所有元素上触发(因为 live),并有条件地添加唯一的 scroll 事件。

更新 2:哦,顺便说一句,您的整个调整大小代码可以更好地编写为:

// resize text area (fixed version of Pointy's)
function resizeTextArea(elem) {
    elem.height(1); elem.scrollTop(0);
    elem.height(elem[0].scrollHeight - elem[0].clientHeight + elem.height())
}​

The answer is simple. scroll is what prevents the flickering, because it fires at the very first moment of resize. But scroll has no effect with live (because it doesn't bubble), so your newly created textareas will be resized on keyup but it fires later (thus the flickering).

Update: Of course I can even solve your problem. You just need to ask :) [Demo]

$('textarea.autoresize').live('keyup', function() {
    var el = $(this);
    if (!el.data("has-scroll")) {
        el.data("has-scroll", true);
        el.scroll(function(){
           resizeTextArea(el);
        });
    }
    resizeTextArea(el);
});

The point is, it mixes live with bind. The keyup event, which fires on all elements (because of live), adds the unique scroll event conditionally.

Update 2: Oh, and by the way your whole resizing code can be better written as:

// resize text area (fixed version of Pointy's)
function resizeTextArea(elem) {
    elem.height(1); elem.scrollTop(0);
    elem.height(elem[0].scrollHeight - elem[0].clientHeight + elem.height())
}​
无畏 2024-10-20 05:48:47

试试这个 (JSFiddle):

$('#Add').click(function(){
    var id = "newtextarea"+Math.floor(Math.random()*1000);
   $('#pane').append($('<textarea class="new" rows="1" cols="40" id="'+id+'"></textarea><br/>'));
    $('textarea:last').focus();
    bindAgain(id);
});

//inital resize
resizeTextArea($('#tst'));

//'live' event
$('textarea.new').bind('keyup scroll', function() {
    resizeTextArea($(this));
});

function bindAgain(id)
{
    $('#'+id).bind('keyup scroll', function() {
    resizeTextArea($(this));
});

}

基本上,它使用动态创建的 ID 重新绑定事件。虽然不像 karim79 的解决方案那么优雅,但它确实有效。

Try this (JSFiddle):

$('#Add').click(function(){
    var id = "newtextarea"+Math.floor(Math.random()*1000);
   $('#pane').append($('<textarea class="new" rows="1" cols="40" id="'+id+'"></textarea><br/>'));
    $('textarea:last').focus();
    bindAgain(id);
});

//inital resize
resizeTextArea($('#tst'));

//'live' event
$('textarea.new').bind('keyup scroll', function() {
    resizeTextArea($(this));
});

function bindAgain(id)
{
    $('#'+id).bind('keyup scroll', function() {
    resizeTextArea($(this));
});

}

Basically, it rebinds the event using a dynamically created ID. Not as elegant as karim79's solution, but it works.

水波映月 2024-10-20 05:48:47

我将它绑定到一个自定义事件,每当重要的事情发生时我都会调用该事件。像这样:

$(body).live('MyCustomEvent', function() {
    $("#MyScrollItem").scroll(function() {
       // Do things here
    }
});

希望有帮助。简短而甜蜜。

I bind it to a custom event that I call whenever something important happens. Like this:

$(body).live('MyCustomEvent', function() {
    $("#MyScrollItem").scroll(function() {
       // Do things here
    }
});

Hope that helps. Short and sweet.

夏夜暖风 2024-10-20 05:48:47

我找到了这个问题的解决方案:问题是 .live 和滚动不起作用。

我的解决方案是使用绑定事件..和超时。超时会给 DOM 时间来更新,例如。

下面的代码用于在滚动到页面底部时加载内容。看一下 setTimeout 和绑定。

$('.list').bind("scroll",function(){
    $('.list').height()));
    if($('.list').scrollTop() >= ($('.list').height()+ $(window).height())){        
      setTimeout(function(){    //Time out is used as there is a possibility that
        last_function();
      },200);   
    }
});

I found a solution to this problem: The problem is where .live and scroll doesnt work.

My solution is to use the bind event.. and Timeout. The timeout will give the DOM time to update eg.

The below code is used to load content when you scroll to the bottom of the page. Take a look at the setTimeout and the bind.

$('.list').bind("scroll",function(){
    $('.list').height()));
    if($('.list').scrollTop() >= ($('.list').height()+ $(window).height())){        
      setTimeout(function(){    //Time out is used as there is a possibility that
        last_function();
      },200);   
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文