简体   繁体   English

代码镜像。 禁用垂直滚动条

[英]CodeMirror . Disabling Vertical scroll bar

I am currently using CodeMirror to edit CODE in text area in browser.我目前正在使用 CodeMirror 在浏览器的文本区域中编辑 CODE。 If i have more than 20 lines of code, it is adding a vertical scroll bar to right.如果我有 20 多行代码,它会在右侧添加一个垂直滚动条。 But i do not need this scroll bar.但我不需要这个滚动条。 Instead i need the editor size to grow vertically.相反,我需要编辑器大小垂直增长。

Can anyone help ?任何人都可以帮忙吗?

In CodeMirror 3, there is an option to disable the scrollbars : scrollbarStyle: "null"在 CodeMirror 3 中,有一个禁用滚动条的选项: scrollbarStyle: "null"

From the documentation :从文档:

scrollbarStyle: string滚动条样式:字符串

Chooses a scrollbar implementation.选择滚动条实现。 The default is "native", showing native scrollbars.默认为“native”,显示本机滚动条。 The core library also provides the "null" style, which completely hides the scrollbars.核心库还提供了“空”样式,完全隐藏了滚动条。 Addons can implement additional scrollbar models.插件可以实现额外的滚动条模型。

Combining this with :将此与:

And then controlling the height/width of the parent div works well然后控制父div的高度/宽度效果很好

The CodeMirror doco talks about a style CodeMirror-scroll which controls whether a scrollbar appears, and whether the textarea expands to fit the content. CodeMirror doco讨论了一种样式CodeMirror-scroll ,它控制是否出现滚动条,以及 textarea 是否扩展以适应内容。 Specifically it says:具体说:

CodeMirror-scroll Whether the editor scrolls (overflow: auto + fixed height). CodeMirror-scroll编辑器是否滚动(溢出:自动+固定高度)。 By default, it does.默认情况下,确实如此。 Setting the CodeMirror class to have height: auto and giving this class overflow-x: auto;将 CodeMirror 类设置为具有 height: auto 并赋予该类 overflow-x: auto; overflow-y: hidden;溢出-y:隐藏; will cause the editor to resize to fit its content.将导致编辑器调整大小以适应其内容。

Thus the idea is to add to your own CSS something like:因此,我们的想法是添加到您自己的 CSS 中,例如:

.CodeMirror {
  border: 1px solid #eee;
  height: auto;
}
.CodeMirror-scroll {
  overflow-y: hidden;
  overflow-x: auto;
}

as illustrated here in the official demo that accompanies the documentation on the style CodeMirror-scroll .如图所示这里在陪伴风格CodeMirror滚动文档的官方演示。

What worked for me:什么对我有用:

For my personal situation using CodeMirror 2.34 all I did was add the following style to my own stylesheet:对于我使用 CodeMirror 2.34 的个人情况,我所做的只是将以下样式添加到我自己的样式表中:

div.CodeMirror-scroll { height: auto!important; overflow: visible; }

CodeMirror 3:代码镜像 3:

In my brief initial testing of CodeMirror 3, both the above techniques didn't work and I still got the scrollbars.在我对 CodeMirror 3 的简短初始测试中,上述两种技术都不起作用,我仍然得到了滚动条。 Interesting, since you'd think the official doco on subject would be valid - unless CodeMirror 3 has changed its styles a bit and the doco hasn't caught up yet...有趣的是,因为您认为有关主题的官方文档是有效的 - 除非 CodeMirror 3 稍微改变了它的样式并且文档还没有赶上......

For me wrapping codemirror textarea on an element with display:flex , fixed the unwanted horizontal scroll issue:对于我用display:flex将 codemirror textarea 包装在一个元素上,修复了不需要的水平滚动问题:

<div style='display: flex'>
  <textarea></textarea>
</div>
<script>
//codemirror setup
</script>

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

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