如何在 Live() 上绑定滚动事件?
不久前,我为某人解决了一个问题 希望他的文本区域变大。我创建了一个函数来侦听该区域的 scroll
和 keyup
事件并重新计算行数。我想在另一个项目中使用该代码,但出现了问题。文本区域不知道为了解决这个问题,我使用 live
而不是 bind
,这样将来的区域也将被绑定。
现在我发现 live
的执行速度比 bind
慢很多。我创建了 jsFiddle 上的简化示例。上面的文本区域的行为符合我的要求,但由于信号延迟(我使用的是 Chrome),新添加的文本区域会闪烁。
如何使问题在于live
与bind
一样快?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 The problem is that the live
as fast as the bind
?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,
. As
keypress, keyup, mousedown, mousemove,
mouseout, mouseover, and mouseup
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
答案很简单。
scroll
可以防止闪烁,因为它会在调整大小的第一刻触发。但是scroll
对live
没有影响(因为它不会冒泡),因此您新创建的文本区域将在keyup
上调整大小,但会触发稍后(因此闪烁)。更新:当然我什至可以解决你的问题。你只需要问:) [演示]
重点是,它将
live
与bind
混合。keyup
事件会在所有元素上触发(因为live
),并有条件地添加唯一的scroll
事件。更新 2:哦,顺便说一句,您的整个调整大小代码可以更好地编写为:
The answer is simple.
scroll
is what prevents the flickering, because it fires at the very first moment of resize. Butscroll
has no effect withlive
(because it doesn't bubble), so your newly created textareas will be resized onkeyup
but it fires later (thus the flickering).Update: Of course I can even solve your problem. You just need to ask :) [Demo]
The point is, it mixes
live
withbind
. Thekeyup
event, which fires on all elements (because oflive
), adds the uniquescroll
event conditionally.Update 2: Oh, and by the way your whole resizing code can be better written as:
试试这个 (JSFiddle):
基本上,它使用动态创建的 ID 重新绑定事件。虽然不像 karim79 的解决方案那么优雅,但它确实有效。
Try this (JSFiddle):
Basically, it rebinds the event using a dynamically created ID. Not as elegant as karim79's solution, but it works.
我将它绑定到一个自定义事件,每当重要的事情发生时我都会调用该事件。像这样:
希望有帮助。简短而甜蜜。
I bind it to a custom event that I call whenever something important happens. Like this:
Hope that helps. Short and sweet.
我找到了这个问题的解决方案:问题是 .live 和滚动不起作用。
我的解决方案是使用绑定事件..和超时。超时会给 DOM 时间来更新,例如。
下面的代码用于在滚动到页面底部时加载内容。看一下 setTimeout 和绑定。
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.