简体   繁体   English

使用ContentEditable =“true”在元素溢出时向左滚动文本

[英]Scroll Text Left on Overflow for Element with ContentEditable=“true”

I have an editable span where I want the text to scroll to the left when it overflows. 我有一个可编辑的范围,我希望文本在溢出时滚动到左侧。 The behavior I am after is the same as with an input type="text" element. 我所追求的行为与输入type =“text”元素相同。 That is, if you type too much to fit into the element, it starts scrolling to the left so that the last character is always displayed and the beginning characters are hidden. 也就是说,如果你键入太多不适合元素,它会开始向左滚动,以便始终显示最后一个字符并隐藏起始字符。

Here's an example of what I am doing: 这是我正在做的一个例子:

<style>
    .editable {
        position:absolute;
        background-color: lightgoldenrodyellow;
        cursor: text;
        display: inline-block;
        white-space: nowrap;
        top:100px;
        left:100px;
        width:200px;

    }
</style>

<span contenteditable="true" class="editable"></span>

The problem is over-flow results in text outside the element; 问题是元素之外的文本过流导致; I want it to scroll to the left as described above. 我希望它如上所述向左滚动。 Is this possible? 这可能吗?

If anyone is tempted to ask why don't I just use an input element, the reason is I need to set the innerHTML to provide some formatting such as color coding of words. 如果有人想要问我为什么不使用输入元素,原因是我需要设置innerHTML以提供一些格式,例如单词的颜色编码。 That is my motivation for doing this, so if you have an alternative over a contenteditable element, I am open to it. 这是我这样做的动机,所以如果你有一个替代超过一个满足的元素,我愿意接受它。 Thanks. 谢谢。

overflow:hidden helps: overflow:hidden帮助:

 .editable { background-color: lightgoldenrodyellow; cursor: text; display: inline-block; white-space: nowrap; width: 100px; overflow:hidden } 
 <span contenteditable="true" class="editable">type here </span> 

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

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