我正在使用包含文本和图像的富文本编辑器。我试图找出一种方法,使文本在图像移动到文档的不同区域时包裹图像。我不完全确定从哪里开始。我的第一个本能结构是文档,它是div的集合(代表段落),然后从指定的div添加/删除图像。但问题是,我不完全确定如何使用触发javascript函数的gui来完成此操作。我最初的想法是计算文档中所有div的高度,宽度和位置,并且随着图像位置的变化,调用相关div的appendChild()
/ removeChild()
方法(我是可能必须动态生成元素ID)。我想知道的是我走在正确的轨道上?或者我应该以另一种方式批准这个问题吗?
仅供参考,我正在使用移动设备,解决方案必须是触摸屏。
答案 0 :(得分:0)
您可以通过使容器对象成为可信的div,并使图像向左(或右)浮动来实现此目的。
如果你想知道图像的位置何时发生变化,你可以设置一个计时器来检查你检查的最后一个版本的div的innerHTML,或者,如果你只有一个图像,你可以检查位置“
查看下面的代码:
var lastText = document.querySelector(".container").innerText;
setInterval(function(){
var currText = document.querySelector(".container").innerText;
if (lastText != currText) { alert("dom changed");}
lastText = currText;
}, 1000);
.img {
float:left;
height:200px;
width:200px;
}
<div style="height:500px;width:500px;display:block;" contenteditable="true" class="container">
What is Lorem Ipsum?
<p>
<img src="https://static.igrice123.rs/slike/12378-3195/kuca-ze-ove-peki-:d.jpg" class="img" style="position:relative">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
<br><br>
Why do we use it?
<br><br>
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
<br><br>
Where does it come from?
<br><br>
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
<br><br>
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
</p>
</div>