11

我正在尝试contentEditable在 Chrome 上集成图像大小调整。我知道 webkit 关于此功能的正常操作存在问题,所以我想知道如果用户使用 Google Chrome(jQuery 插件?Javascript 方法?),集成图像调整大小的最佳替代方法是什么。

<div contenteditable>
  <img src="http://www.thebrandbite.com/wp-content/media/2015/07/apple-7.jpg" width=200 />
</div>

以下是 Firefox 开箱即用的工作方式:

在此处输入图像描述

4

3 回答 3

4

您的选择是:

  1. 使用图书馆。它们肯定存在:例如,YUI 有一个图像调整大小模块。我从未使用过它,但该演示适用于我尝试过的所有桌面浏览器。

  2. 自己编写:为调整大小手柄添加一些可拖动的定位元素。我以前做过,如果你知道自己在做什么,这并不难。

于 2013-10-06T10:29:45.117 回答
4

@Tim-Down 答案可能是更好的解决方案。但是我想扔在那里,如果你不想用它做任何进一步的事情,你可以只用 CSS 调整图像的大小:

http://codepen.io/anon/pen/JEEKqO

    resize: both;
    background-size: contain;
    background-repeat: no-repeat;
    background-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-after.jpg);

body {
  background-color: #1D1F1F;
}

section { 
  display: table;
  margin: 0 auto;
}

div {
  resize: both;
  background-color: white;
  overflow: scroll;
  background-image: url('https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/photoshop-face-after.jpg');
  background-size: cover;
  background-repeat: no-repeat;
} 
<section>
  <button>both</button>
  <button>horizontal</button>
  <button>vertical</button>
  <button>none</button>
<div class="resize"></div>
</section>

于 2017-01-18T14:34:54.663 回答
0

试试这个(来源):

        function enableImageResizeInDiv(id) {
            if (!(/chrome/i.test(navigator.userAgent) && /google/i.test(window.navigator.vendor))) {
                return;
            }
            var editor = document.getElementById(id);
            var resizing = false;
            var currentImage;
            var createDOM = function (elementType, className, styles) {
                let ele = document.createElement(elementType);
                ele.className = className;
                setStyle(ele, styles);
                return ele;
            };
            var setStyle = function (ele, styles) {
                for (key in styles) {
                    ele.style[key] = styles[key];
                }
                return ele;
            };
            var removeResizeFrame = function () {
                document.querySelectorAll(".resize-frame,.resizer").forEach((item) => item.parentNode.removeChild(item));
            };
            var offset = function offset(el) {
                const rect = el.getBoundingClientRect(),
                scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
                scrollTop = window.pageYOffset || document.documentElement.scrollTop;
                return { top: rect.top + scrollTop, left: rect.left + scrollLeft }
            };
            var clickImage = function (img) {
                removeResizeFrame();
                currentImage = img;
                const imgHeight = img.offsetHeight;
                const imgWidth = img.offsetWidth;
                const imgPosition = { top: img.offsetTop, left: img.offsetLeft };
                const editorScrollTop = editor.scrollTop;
                const editorScrollLeft = editor.scrollLeft;
                const top = imgPosition.top - editorScrollTop - 1;
                const left = imgPosition.left - editorScrollLeft - 1;
    
                editor.append(createDOM('span', 'resize-frame', {
                    margin: '10px',
                    position: 'absolute',
                    top: (top + imgHeight - 10) + 'px',
                    left: (left + imgWidth - 10) + 'px',
                    border: 'solid 3px blue',
                    width: '6px',
                    height: '6px',
                    cursor: 'se-resize',
                    zIndex: 1
                }));
    
                editor.append(createDOM('span', 'resizer top-border', {
                    position: 'absolute',
                    top: (top) + 'px',
                    left: (left) + 'px',
                    border: 'dashed 1px grey',
                    width: imgWidth + 'px',
                    height: '0px'
                }));
    
                editor.append(createDOM('span', 'resizer left-border', {
                    position: 'absolute',
                    top: (top) + 'px',
                    left: (left) + 'px',
                    border: 'dashed 1px grey',
                    width: '0px',
                    height: imgHeight + 'px'
                }));
    
                editor.append(createDOM('span', 'resizer right-border', {
                    position: 'absolute',
                    top: (top) + 'px',
                    left: (left + imgWidth) + 'px',
                    border: 'dashed 1px grey',
                    width: '0px',
                    height: imgHeight + 'px'
                }));
    
                editor.append(createDOM('span', 'resizer bottom-border', {
                    position: 'absolute',
                    top: (top + imgHeight) + 'px',
                    left: (left) + 'px',
                    border: 'dashed 1px grey',
                    width: imgWidth + 'px',
                    height: '0px'
                }));
    
                document.querySelector('.resize-frame').onmousedown = () => {
                    resizing = true;
                    return false;
                };
    
                editor.onmouseup = () => {
                    if (resizing) {
                        currentImage.style.width = document.querySelector('.top-border').offsetWidth + 'px';
                        currentImage.style.height = document.querySelector('.left-border').offsetHeight + 'px';
                        refresh();
                        currentImage.click();
                        resizing = false;
                    }
                };
    
                editor.onmousemove = (e) => {
                    if (currentImage && resizing) {
                        let height = e.pageY - offset(currentImage).top;
                        let width = e.pageX - offset(currentImage).left;
                        height = height < 1 ? 1 : height;
                        width = width < 1 ? 1 : width;
                        const top = imgPosition.top - editorScrollTop - 1;
                        const left = imgPosition.left - editorScrollLeft - 1;
                        setStyle(document.querySelector('.resize-frame'), {
                            top: (top + height - 10) + 'px',
                            left: (left + width - 10) + "px"
                        });
    
                        setStyle(document.querySelector('.top-border'), { width: width + "px" });
                        setStyle(document.querySelector('.left-border'), { height: height + "px" });
                        setStyle(document.querySelector('.right-border'), {
                            left: (left + width) + 'px',
                            height: height + "px"
                        });
                        setStyle(document.querySelector('.bottom-border'), {
                            top: (top + height) + 'px',
                            width: width + "px"
                        });
                    }
                    return false;
                };
            };
            var bindClickListener = function () {
                editor.querySelectorAll('img').forEach((img, i) => {
                    img.onclick = (e) => {
                        if (e.target === img) {
                            clickImage(img);
                        }
                    };
                });
            };
            var refresh = function () {
                bindClickListener();
                removeResizeFrame();
                if (!currentImage) {
                    return;
                }
                var img = currentImage;
                var imgHeight = img.offsetHeight;
                var imgWidth = img.offsetWidth;
                var imgPosition = { top: img.offsetTop, left: img.offsetLeft };
                var editorScrollTop = editor.scrollTop;
                var editorScrollLeft = editor.scrollLeft;
                const top = imgPosition.top - editorScrollTop - 1;
                const left = imgPosition.left - editorScrollLeft - 1;
    
                editor.append(createDOM('span', 'resize-frame', {
                    position: 'absolute',
                    top: (top + imgHeight) + 'px',
                    left: (left + imgWidth) + 'px',
                    border: 'solid 2px red',
                    width: '6px',
                    height: '6px',
                    cursor: 'se-resize',
                    zIndex: 1
                }));
    
                editor.append(createDOM('span', 'resizer', {
                    position: 'absolute',
                    top: (top) + 'px',
                    left: (left) + 'px',
                    border: 'dashed 1px grey',
                    width: imgWidth + 'px',
                    height: '0px'
                }));
    
                editor.append(createDOM('span', 'resizer', {
                    position: 'absolute',
                    top: (top) + 'px',
                    left: (left + imgWidth) + 'px',
                    border: 'dashed 1px grey',
                    width: '0px',
                    height: imgHeight + 'px'
                }));
    
                editor.append(createDOM('span', 'resizer', {
                    position: 'absolute',
                    top: (top + imgHeight) + 'px',
                    left: (left) + 'px',
                    border: 'dashed 1px grey',
                    width: imgWidth + 'px',
                    height: '0px'
                }));
            };
            var reset = function () {
                if (currentImage != null) {
                    currentImage = null;
                    resizing = false;
                    removeResizeFrame();
                }
                bindClickListener();
            };
            editor.addEventListener('scroll', function () {
                reset();
            }, false);
            editor.addEventListener('mouseup', function (e) {
                if (!resizing) {
                    const x = (e.x) ? e.x : e.clientX;
                    const y = (e.y) ? e.y : e.clientY;
                    let mouseUpElement = document.elementFromPoint(x, y);
                    if (mouseUpElement) {
                        let matchingElement = null;
                        if (mouseUpElement.tagName === 'IMG') {
                            matchingElement = mouseUpElement;
                        }
                        if (!matchingElement) {
                            reset();
                        } else {
                            clickImage(matchingElement);
                        }
                    }
                }
            });
        }
        enableImageResizeInDiv('edt');
 #edt {
            padding: 2px;
            width: 100%;
            height: 500px;
            border: 1px solid black;
            overflow: scroll;
        }
<!doctype html>
<html>

<body>
    How to enable image resizing inside contenteditable DIV (aka WYSIWYG editor) in Chrome?<br><hr>
    <div id="edt" contenteditable="true">
        <p><b>Green</b><img
                src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIQAAABeCAYAAADi4bzuAAABh0lEQVR4Ae3SMRXAIBAFQYKV6InYKISGgvUwV91v983z/t8aToFTYCqhwF0AiLuGfwABQQoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykABDJYQDBQAoAkRwGEAykwAbutgLcDSwO9gAAAABJRU5ErkJggg==">Red
            <img
                src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIQAAABeCAYAAADi4bzuAAAEQElEQVR4Ae2cPXLcMAyFn0TJXk8mXVKlzY1S5ghpcy3fKCdIxl1+7JVIZsAV1oJ3xg3cEPuYWfNHQsZ47xNIbeHh4dPnCjYqsCkwUgkqsFeAQOzV4BgEghAYBQiEkYMTAkEGjAIEwsjBCYEgA0YBAmHk4IRAkAGjAIEwcnBCIMiAUYBAGDk4IRBkwChAIIwcnBAIMmAUIBBGDk4IBBkwChAIIwcnBIIMGAUIhJGDEwJBBowCBMLIwQmBIANGAQJh5OCEQJABowCBMHJwQiDIgFGAQBg5OCEQZMAoQCCMHJyEB6IMgHy07Ya6xH6nwLQbdzncmz2++EsXcq0qARWQodwj63lbfxnTpQhv+Et3XSHUcDFdjBWP1X+99oZaXcV/1X2FEJeGCiQBop4qgjz9AkkDpTzDIveyMrzOddcVQmFo1UErxNbrtdZvW4VWEVmTCiIfNqtA1xVC93/dKnSOAshxQg3Xo4VUEHkC9ttJ2WCxslzvrGsg1DY1X4zWbUMgEUBkra3LXAO2beZ84NytX/uwayDEaGllBJ62sZ4nkjz6WxPj1w0WOWtIk75q6djuY2cfmi71ELPVewFE5m1tA0SSEki07YbnNxK9xr5zINohsY44jDdIjwW3T8D78YC6nGwfptS2DakGUjF0C9E3DQJwqUDXW0ZLJxfUsuLdfIthGPD49x8O84xjXZBzRtpy3hWMtrKvFJeyXO9K90BM04S8rMgVrU9TAkrBKP+GERXZbA1aJa7X8tcz7x6IY14x3844lgJMM9ZaMaWEYc3nzLUayBlDgJCPNKkaeu1885UP9m9i3UnRXidvZ/xGwUNa8OH7N3z8+gV/UsFSC+r2GpFH4JiAJQEyljaV07ni5VbSnQhv/At3DYRosSwZa8443N0Bv37ix/096jBgupnPQMh98uax72VMGE6a7H8OPf81fKkQaTpgQcG6HjHK+UFaqcCSkcYRadsU9JVULut3FXqe4LZxkk1+dn2GEEPX5elkfBmQ8tDeLOoopKT21oFyOlS2c8P2zaUkLhA0SJ614Kh3IMTBVgFKbvW/lBVD+0Zye+Zt1wzXA6VMWBkun4HuzxCXKXHFowCB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVkxKB8KgXMJZABDTVk9J/bM/jwqGSC8IAAAAASUVORK5CYII=">
        </p>
    </div>
</body>
</html>

于 2022-02-18T22:07:42.433 回答