Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用带有向上滚动和向下滚动的javascript将文本包装在特定的宽度和高度内_Javascript_Scrollbar_Word Wrap - Fatal编程技术网

如何使用带有向上滚动和向下滚动的javascript将文本包装在特定的宽度和高度内

如何使用带有向上滚动和向下滚动的javascript将文本包装在特定的宽度和高度内,javascript,scrollbar,word-wrap,Javascript,Scrollbar,Word Wrap,我是javascript方面的新手,我正在使用它创建一个交互式网站,但有一个问题。因为我刚刚开始,我所知道的只是如何扭曲我的内容,我不知道如何用javascript编写一个代码,如果wrapText在给定的宽度和高度内溢出,那么该代码的功能就是。将出现一个上下滚动条,帮助导航非sceen的项目 这就是我使用的: function wrapText(text, x, y, maxWidth, lineHeight) { var words = text.split(''); var lin

我是javascript方面的新手,我正在使用它创建一个交互式网站,但有一个问题。因为我刚刚开始,我所知道的只是如何扭曲我的内容,我不知道如何用javascript编写一个代码,如果wrapText在给定的宽度和高度内溢出,那么该代码的功能就是。将出现一个上下滚动条,帮助导航非sceen的项目

这就是我使用的:

function wrapText(text, x, y, maxWidth, lineHeight)
{
  var words = text.split('');
  var line = '';
  for(var n = 0; n < words.length; n++)
  {
    var testLine = line + words[n] + '';
    var metrics = content.measureText(testLine);
    var testWidth = metrics.width;
    if(testWidth > maxWidth)
    {
      context.fillText(line, x, y);
      line = words[n] + '';y += lineHeight;
    }
    else {
           line = textLine;
         }
   }
context.fillText(line, x, y);
}
给你。我认为您的解决方案只需要简单的HTML/CSS:

HTML:

wrapText(currentText, 45, 460, 800, 20);
<div id="foo" class="wrap">...</div>
#foo {
    width: 400px;
    height: 300px;
    background-color: yellowgreen;
}

.wrap {
    overflow-y: auto;
}