简体   繁体   English

当内容溢出div的固定高度时,如何创建新的div?

[英]How to create new div when the content is overflowing past the fixed height of the div?

CSS CSS

.page{ 
      width: 275px;
      hight: 380px;
      overflow: auto;
     }

HTML HTML

<div class="page">dynamic text</div>

How to create a new div when the dynamic text is overflowing past the fixed height of the div? 当动态文本溢出div的固定高度时,如何创建一个新的div?

Example: 例:

<div class="page">Some dynamic texts will appear here</div>

When the dynamic text is overflowing past the fixed height of the div, the content above will be appear like this. 当动态文本溢出div的固定高度时,上面的内容将如下所示。

<div class="page">Some dynamic</div>
<div class="page">texts will</div>
<div class="page">appear here</div>

I've tried using wordwrap function in PHP wordwrap($dynamic_text, 600, '</div><div class="page">'); 我已经尝试在PHP wordwrap($dynamic_text, 600, '</div><div class="page">');使用wordwrap函数wordwrap($dynamic_text, 600, '</div><div class="page">'); it's can running, but it had a problem when the character was copied from Ms.Words. 它可以运行,但是当从Ms.Words复制角色时它有问题。 So, by detecting the overflowing text, cut it, and then paste it into the new div element is the better solustion, i guess. 所以,通过检测溢出的文本,剪切它,然后将其粘贴到新的div元素是更好的解决,我猜。 But, I don't know how to do this solution using JQuery or Javascript. 但是,我不知道如何使用JQuery或Javascript来做这个解决方案。

Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

You can do it, and it's way more than just a couple lines of code. 你可以做到这一点,它不仅仅是几行代码。 It took a very experienced developer a couple days. 这需要一位经验丰富的开发人员几天。 Sorry, can't share the code. 对不起,无法共享代码。

Javascript: Put the whole content into the div. Javascript:将整个内容放入div中。 You may keep it hidden or out of the DOM for a while. 您可以将其隐藏或暂时保留在DOM之外。 Traverse the div's children. 遍历div的孩子们。 Find the one whose top+scrollHeight exceeds the div's height. 找到top + scrollHeight超过div高度的那个。 Traverse it recursively. 递归遍历它。 Eventually, you will either find an indivisible element, eg, an image, that doesn't fit, or a position within a text node to split the text at. 最终,您将找到一个不可分割的元素,例如图像,不适合,或者在文本节点中找到一个位置来分割文本。 Remove that part and all further elements from the div. 从div中删除该部分和所有其他元素。 Add them to a new one. 将它们添加到新的。

There are a lot of details to address, so it's not simple. 有很多细节需要解决,所以并不简单。 But doable. 但可行。

I just had something similar working today while I was searching for an answer but there doesn't seem to be anything straight forward. 我正在寻找答案的时候,我今天也有类似的工作,但似乎没有任何直接的进展。

Although I am using Array.reduce() you should be able to do this with Array.forEach() or any other iterating code you like. 虽然我使用的是Array.reduce()您应该能够使用Array.forEach()或您喜欢的任何其他迭代代码执行此操作。

words.reduce(function(acc, value) 

This is done by calculating if the element will overflow if we add another word to it before we actually render it. 这是通过计算元素是否会溢出来完成的,如果我们实际渲染它之前添加另一个单词。 The hacky thing here is to add another block element inside of it with visibility: hidden . 这里的hacky是在其中添加另一个block元素,其中visibility: hidden

element.innerHTML = '<div style="visibility: hidden; height: 100%; width=100%">' + textToBeAdded + '</div>';

That way the block element still takes its parents dimensions and the parent element can be checked for overflow. 这样block元素仍然采用其父元素维度,并且可以检查父元素是否溢出。

The way to check for overflow is to compare the element's scrolling height to its height: 检查溢出的方法是将元素的滚动高度与其高度进行比较:

if (element.scrollHeight > element.offsetHeight) 

If it overflows we leave it as is and create a new element and put the current value (word) in the iteration. 如果它溢出,我们保持原样并创建一个新元素并将当前value (字)放在迭代中。 Then we attach it to the same DOM tree as the previous element (as its parent's child... like having a new brother 😜) 然后我们将它附加到前一个元素相同的DOM树 (作为其父母的孩子......就像拥有一个新的兄弟😜)

var newPageEl = document.createElement('div');
newPageEl.classList = 'page';
newPageEl.textContent = word;
parentElement.appendChild(newPageEl);

Hope this makes sense. 希望这是有道理的。

 var page = document.getElementsByClassName('page')[0]; if (page.scrollHeight > page.offsetHeight) { // is overflowing fixOverflow(page); } function fixOverflow(element) { var words = element.textContent.split(' '); // delete previous text content element.textContent = ''; words.reduce(function(acc, value) { // store current element's text var currentElementText = element.textContent.toString().trim(); var textToBeAdded = currentElementText + ' ' + value; element.innerHTML = '<div style="visibility: hidden; height: 100%; width=100%">' + textToBeAdded + '</div>'; if (element.scrollHeight > element.offsetHeight) { // is overflowing with the new word element.innerHTML = ""; // leave the last page element as is element.textContent = currentElementText; // create another element with the new value to be added // ** IMPORTANT replace the memory of the previous element variable element = createPageElement(value); // add it to the same DOM tree as the previous page element page.parentElement.appendChild(element); // could also be document.getElementById('page-container').appendChild(element); } else { // if not overflowing add another word element.innerHTML = currentElementText + ' ' + value; } }, ""); } function createPageElement(text) { // create element with class page var newPageEl = document.createElement('div'); newPageEl.classList = 'page'; newPageEl.textContent = text; return newPageEl; } 

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

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