Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

html - Prevent horizontal "scrolling" of a text input?

I am working on a format input field in html, css and js.

One example would be a date format field with following pattern: **.**.****. When I am typing in the input fields, Chrome "scrolls" to the left when I reach the end of the box.

How can I prevent this?

enter image description here

The lines that separate each char are made by a background image. With a monospaced font and a set letter-spacing each character goes into a "field". If you reach the size of the input field it scrolls forward and takes the background with it.

To resolve this problem, I put the input element into a div element, limited the div element to a width and added borders and overflow: none; to the div. The result: It works fine in Firefox, Chrome still scrolls the input content to the left.

Am I doing this wrong? Is there an other solution for that? Is there a -webkit property to prevent this? Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same issue and found a CSS solution:

  #input-wrapper {
    border: 1px solid #C9CFD5;
    background-image: url('grid.png'); /* some tile image for the vertical lines */
    width: 200px;
    height: 50px;
    overflow: hidden;
  }
  #input-wrapper input {
    border: 0 none;
    width: 400px;
    height: 50px;
    background: transparent;
    font-size: 30px;
    font-family: "Courier", monospace;
    letter-spacing: 28px;
    text-indent: 18px;
  }
<div id="input-wrapper">
  <input type="text" maxlength="4" length="4" value="1234" />
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...