最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Event to detect when the text in an <input> is scrolled? - Stack Overflow

programmeradmin0浏览0评论

Is there an event that is fired when the text inside an <input>-tag is scrolled? I mean when the text inside is to long and you move the input caret to the end of it, etc.

There is a scroll event you can catch on the element when that is scrolled in the layout, but that is not what I want.

UPDATE: Here is a fiddle to test the events discussed: /

Is there an event that is fired when the text inside an <input>-tag is scrolled? I mean when the text inside is to long and you move the input caret to the end of it, etc.

There is a scroll event you can catch on the element when that is scrolled in the layout, but that is not what I want.

UPDATE: Here is a fiddle to test the events discussed: http://jsfiddle/lborgman/L8k5ggnk/3/

Share Improve this question edited Dec 30, 2014 at 22:40 Leo asked Dec 30, 2014 at 21:48 LeoLeo 4,4387 gold badges54 silver badges83 bronze badges 6
  • I am curious what you want to use it for. – jbg Commented Dec 30, 2014 at 21:50
  • @jbg, Just a rewrite of plete.ly.js. It uses a "background" <input>-tag to display a hint. I want to scroll the text in the hint when the text in the main <input> is scrolled. – Leo Commented Dec 30, 2014 at 21:53
  • 1 An input does fire a scroll event when the overflowing text "scrolls" via change in overflow, at least in Firefox. – Alexander O'Mara Commented Dec 30, 2014 at 21:57
  • 1 @Leo I'm not sure, but a scroll event doesn't fire in Chrome unfortunately, despite the fact that it has the scrollLeft property. – Alexander O'Mara Commented Dec 30, 2014 at 22:06
  • 1 Here is a reported bug at bugs.chromium/p/chromium/issues/detail?id=1007153, thanks – 4esn0k Commented Sep 24, 2019 at 6:06
 |  Show 1 more ment

2 Answers 2

Reset to default 6

There is no specific event for this that will work across browsers. Firefox does fire a scroll event when the text "scrolls" by changing the insertion point, but Chrome and likely other browsers don't.

Here is a list of events that could change the insertion point.

  • input
  • keydown
  • keyup
  • focus
  • blur
  • click
  • change
  • paste
  • cut
  • scroll
  • wheel
  • dragover

Binding an event listener to all of these should be enough to respond to the insertion point changing. I believe the list above is plete, but if I have missed any, let me know!

Have you tried the Caret Plugin?

You can get the position of the caret with:

pos = $(textarea).caret()

and catch when it changes again. If the latter is different than the first then you know the user has moved inside the input.

发布评论

评论列表(0)

  1. 暂无评论