根据鼠标位置绑定js事件:是否是性能杀手?
我已经这样做了: http://jsfiddle.net/CS273/4/
在此小提琴上,当鼠标到达右侧时文本输入区域中,它会更改光标(指针)并创建一个单击事件侦听器。 当鼠标离开右侧区域时,同一事件侦听器将被销毁。
我将其称为“动态”事件侦听器绑定,也许它已经有一个名称了。
这样做到底好还是不好?
I've done this :
http://jsfiddle.net/CS273/4/
On this fiddle, when the mouse reaches the right area of the text input, it changes the cursor (pointer) and creates a click event listener.
When the mouse gets out of the right area, this same event listener is destroyed.
I would call that "on the fly" event listener binding, perhaps it already has a name.
Is it a good thing to do that or not ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在所有条件相同的情况下,您可能永远不会注意到性能受到影响,但这是一种有点奇怪的方式来做您想做的事情。
我建议重新处理您的 HTML,以便“x”不是输入的一部分,您只需附加一个普通的事件处理程序即可。
示例: http://jsfiddle.net/cwolves/CS273/6/
All things being equal, you'll probably never notice a performance hit, but this is a somewhat odd way to do what you want.
I'd suggest re-working your HTML so that the 'x' isn't part of the input and you can just attach a normal event handler.
example: http://jsfiddle.net/cwolves/CS273/6/
请参阅您的小提琴的此更新:http://jsfiddle.net/maniator/CS273/5/
我制作了一个浮动 div 来为您处理点击。
这里有一些颜色,所以你可以看到它: http://jsfiddle.net/maniator/CS273/7 /
您需要的唯一js:
您可以将
cursor:pointer;
添加到.hover
的css中,使其看起来与您当前的版本相同。See this update to your fiddle: http://jsfiddle.net/maniator/CS273/5/
I made a floating div that handles the click for you.
Here it is with some color so you see it: http://jsfiddle.net/maniator/CS273/7/
The only js you need:
You can add
cursor: pointer;
to the css of.hover
to have it look the same as your current version.