将焦点图像元素放入可信的div

时间:2018-05-27 08:36:33

标签: javascript contenteditable

我需要修改contenteditable div中的粘贴图像:按比例调整大小,添加边框等等...也许这可以通过添加className来实现,这将改变必要的CSS属性。 问题是我不知道如何引用聚焦,即活动,点击图像或任何元素。

这是我尝试使用的HTML

<!DOCTYPE html>
<html>

<body>

  <div contenteditable="true">This is a div. It is editable. Try to change this text.</div>

</body>

</html>

2 个答案:

答案 0 :(得分:0)

使用css,html和javascript

您的可修改内容应位于具有ID或类名称的父div中,您甚至可以为图像提供不同的div等。

然后就像拥有这样的css一样简单:

#editableContentDiv img {
     imgProperty: value;
}

然后你可以像这样使用javascript:

function insertImage(){
    var $img = document.querySelector("#editableContentDiv img");
    $img.setAttribute('class','resize-drag');      
}

最终,如果您在同一img内有多个div,则可以使用querySelectorAll代替querySelector,然后遍历内部的img代码相同的div

我相信,至少应该让你知道从哪里开始你想要的东西。

类似的例子

我发现这个要点似乎有类似的东西想要你想要但有点复杂。

&#13;
&#13;
function resizeMoveListener(event) {
  var target = event.target,
    x = (parseFloat(target.dataset.x) || 0),
    y = (parseFloat(target.dataset.y) || 0);

  // update the element's style
  target.style.width = event.rect.width + 'px';
  target.style.height = event.rect.height + 'px';

  // translate when resizing from top or left edges
  x += event.deltaRect.left;
  y += event.deltaRect.top;
  updateTranslate(target, x, y);
}

function dragMoveListener(event) {
  var target = event.target,
    // keep the dragged position in the data-x/data-y attributes
    x = (parseFloat(target.dataset.x) || 0) + event.dx,
    y = (parseFloat(target.dataset.y) || 0) + event.dy;
  updateTranslate(target, x, y);
}

function updateTranslate(target, x, y) {
  // translate the element
  target.style.webkitTransform =
    target.style.transform =
    'translate(' + x + 'px, ' + y + 'px)';

  // update the position attributes
  target.dataset.x = x;
  target.dataset.y = y;
}


function insertImage() {
  var $img = document.createElement('img');
  $img.setAttribute('src', 'https://vignette.wikia.nocookie.net/googology/images/f/f3/Test.jpeg/revision/latest?cb=20180121032443');
  $img.setAttribute('class', 'resize-drag');
  document.querySelector(".container-wrap").appendChild($img);

  var rect = document.querySelector(".container-wrap").getBoundingClientRect();
  $img.style.left = rect.left;
  $img.style.top = rect.top;
}

function dataTransfer() {

  //cleanup
  var $out = document.querySelector(".out-container-wrap");
  while ($out.hasChildNodes()) {
    $out.removeChild($out.lastChild);
  }

  //get source
  var source = document.querySelector('.container-wrap')

  //get data
  var data = getSource(source);

  //add data to target
  setSource($out, data);

}

/**
 * Get data from source div
 */
function getSource(source) {
  var images = source.querySelectorAll('img');
  var text = source.querySelector('div').textContent;

  //build the js object and return it.
  var data = {};
  data.text = text;

  data.image = [];

  for (var i = 0; i < images.length; i++) {
    var img = {}
    img.url = images[i].src
    img.x = images[i].dataset.x;
    img.y = images[i].dataset.y;
    img.h = images[i].height;
    img.w = images[i].width;

    data.image.push(img)
  }
  return data;
}

function setSource(target, data) {

  //set the images.
  for (var i = 0; i < data.image.length; i++) {

    var d = data.image[i];

    //build a new image
    var $img = document.createElement('img');
    $img.src = d.url;
    $img.setAttribute('class', 'resize-drag');
    $img.width = d.w;
    $img.height = d.h;
    $img.dataset.x = d.x;
    $img.dataset.y = d.y;


    var rect = target.getBoundingClientRect();
    $img.style.left = parseInt(rect.left);
    $img.style.top = parseInt(rect.top);
    //transform: translate(82px, 52px)
    $img.style.webkitTransform = $img.style.transform = 'translate(' + $img.dataset.x + 'px,' + $img.dataset.y + 'px)';
    //$img.style.setProperty('-webkit-transform', 'translate('+$img.dataset.x+'px,'+$img.dataset.y+'px)');
    target.appendChild($img);
  }

  //make a fresh div with text content
  var $outContent = document.createElement('div')
  $outContent.setAttribute('class', 'out-container-content');
  $outContent.setAttribute('contenteditable', 'true');
  $outContent.textContent = data.text;

  target.appendChild($outContent);
}

interact('.resize-drag')
  .draggable({
    onmove: dragMoveListener,
    inertia: true,
    restrict: {
      restriction: "parent",
      endOnly: true,
      elementRect: {
        top: 0,
        left: 0,
        bottom: 1,
        right: 1
      }
    }
  })
  .resizable({
    edges: {
      left: true,
      right: true,
      bottom: true,
      top: true
    },
    onmove: resizeMoveListener
  })
&#13;
.resize-drag {
  z-index: 200;
  position: absolute;
  border: 2px dashed #ccc;
}

.out-container-content,
.container-content {
  background-color: #fafcaa;
  height: 340px;
}

#btnInsertImage {
  margin-bottom: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="http://code.interactjs.io/interact-1.2.4.min.js"></script>
</head>

<body>
  <button id="btnInsertImage" onclick="insertImage()">Insert Image</button>
  <div class="container-wrap">
    <div class="container-content" contenteditable="true">This is the content of the container. The content is provided along with the image. The image will be moved around and resized as required. The image can move within the boundary of the container.</div>
  </div>
  <button id="btnSubmit" onclick="dataTransfer()">Submit</button>
  <div class="out-container-wrap">
  </div>
</body>

</html>
&#13;
&#13;
&#13;

Source

答案 1 :(得分:0)

这是我尝试过的,它似乎正在起作用 - 至少在我的系统上,我制作的片段不幸地不起作用。

&#13;
&#13;
function getSelImg(){
var curObj;
window.document.execCommand('CreateLink',false, 'http://imageselectionhack/');
   allLinks =   window.document.getElementsByTagName('A');
   for(i = 0; i < allLinks.length; i++)
    {
      if (allLinks[i].href == 'http://imageselectionhack/')
       {
         curObj = allLinks[i].firstChild;
         window.document.execCommand('Undo'); // undo link
         break;
       }
    }
 
 if ((curObj) && (curObj.tagName=='IMG'))
 {
   //do what you want to...
	 curObj.style.border = "thick solid #0000FF";
 }
}	
&#13;
<!DOCTYPE html>
<html>

<body>
<input type="button" onclick="getSelImg()" value="set img border">
<div contenteditable="true">This is a div.<br>
It is editable.<br>
Try to paste an image here:<br>
###########################<br>
<br>
<br>
<br>
###########################
</div>

</body>

</html>
&#13;
&#13;
&#13;