简体   繁体   English

如何使用滚动背景图像创建可滚动的textarea?

[英]How to create a scrollable textarea with scrolling background image?

I would like to add a background image to the textarea that scrolls along with the content. 我想在textarea中添加一个背景图像,与内容一起滚动。 I'm programming in HTML/JavaScript/CSS specifically for mobile Safari. 我正在使用HTML / JavaScript / CSS编程,专门用于移动Safari。

I've attempted a variety of things but nothing seems to work. 我尝试过各种各样的东西但似乎没什么用。 I tried placing the textarea on top of the image and then scrolling the background image whenever the textarea is scrolled. 我尝试将textarea放在图像的顶部,然后每当滚动textarea时滚动背景图像。 It works more or less fine when I'm typing text, but the native scrollbars (which I don't want to get rid of) make it look a wreck on mobile Safari. 当我输入文本时它或多或少都可以正常工作,但原生滚动条(我不想摆脱它)使它在移动Safari上看起来很糟糕。

I tried using a contentEditable div container but that seemed to throw problems too (again with the scrolling). 我尝试使用contentEditable div容器,但这似乎也引发了问题(再次滚动)。

Is it possible to have a textarea with native scrolling with a background image that scrolls? 是否可以使用带有滚动背景图像的原生滚动的textarea?

A background image can be applied to an input or text element as shown: 背景图像可以应用于输入或文本元素,如下所示:

check the fiddle jsfiddle 检查小提琴jsfiddle

html HTML

<div class="outer">
     <textarea></textarea>
  </div>

css CSS

.outer { width: 310px; height: 250px; padding: 5px; border: 1px solid #666; -webikit-border-radius: 3px; -moz-border-radius: 3px; overflow:auto;overflow-x:hidden  }

textarea{
    background: #fff url('http://www.toddle.com/images/300_words_background.gif') 0 -220px no-repeat;
    width: 302px;padding:5px; height:99em;
}

Note: The difference between the two is that the content within the div (assuming it's coded correctly) will be included as search engine usable text whereas the textarea content will not. 注意:两者之间的区别在于div中的内容(假设它的编码正确)将作为搜索引擎可用文本包含在内,而textarea内容则不包括在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM