简体   繁体   English

Codemirror自动调整到给定数量的行

[英]Codemirror autoresize upto a given number of lines

I'm trying to allow codemirror to autoresize up to a given number of lines or pixel height. 我正在尝试允许codemirror自动调整到给定数量的行或像素高度。 After this max-size is reached the widget should add scrollbars. 达到此最大大小后,小部件应添加滚动条。

CodeMirror.fromTextArea(document.getElementById("code"), {
   lineNumbers: true,
   viewportMargin: 50

}); });

max-height is not working 最大高度不起作用

.CodeMirror {
 border: 1px solid #eee;
 height: 100%;
}

http://jsfiddle.net/qzjo4ejh/ http://jsfiddle.net/qzjo4ejh/

You should give .CodeMirror a height: auto style, and put the max-height on .CodeMirror-scroll . 您应该为.CodeMirror一个height: auto样式,并将max-height放在.CodeMirror-scroll The embedded editor on the project page does this (view source, it's right near the top). 项目页面上的嵌入式编辑器执行此操作(查看源代码,它位于顶部附近)。

What about something like that : 这样的事情怎么样:

var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("code"), {
   lineNumbers: true,
   viewportMargin: 50
});

var height;
if(myCodeMirror.lineCount() > 5) {
    height = 50;
} else {
    height = 20 * myCodeMirror.lineCount();
}
myCodeMirror.setSize(500, height);

This is an example. 这是一个例子。 You can handle an event to dynamically resize your CodeMirror when lines change. 您可以处理事件以在行更改时动态调整CodeMirror的大小。

This is what worked for me. 这对我有用。 The CodeMirror-scroll was causing it to size itself way too big. CodeMirror-scroll导致它的大小太大。 You don't need to do anything with viewportMargin despite what the docs say. 尽管文档说的是,您不需要对viewportMargin执行任何操作。

.CodeMirror {
    border: 1px solid #eee;
    height: auto;
    max-height:100px;
}

.CodeMirror-scroll {
    height: auto;
    max-height:100px;
}

Fiddle 小提琴

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

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