【问题标题】:When I drag an image that has been rotated with css, it will drag in a non-rotated manner(original)?当我拖动已经用css旋转的图像时,它会以非旋转方式拖动(原始)?
【发布时间】:2019-05-22 03:54:27
【问题描述】:

我正在制作一个 CSS 网格来显示图像,图像旋转 60 度以进行对角线查看。问题是我希望用户能够在网格中拖放图像以移动图像,但是当他们拖动图像时,它就像根本没有旋转一样拖动。注意:我没有使用画布元素,我使用的是纯元素。

我已尝试使用以下方法之一研究解决此问题的方法:jQuery、Javascript、React JS、CSS,但没有找到任何干净/甚至有效的解决方案。我考虑过重新捕获所有旋转状态的图像,但这将花费我正在处理的图像数量。

我通过 css 旋转图像

    behavior:url(-ms-transform.htc);
    /* Firefox */
    -moz-transform:rotate(315deg);
    /* Safari and Chrome */
    -webkit-transform:rotate(315deg);
    /* Opera */
    -o-transform:rotate(315deg);
    /* IE9 */
    -ms-transform:rotate(315deg);
    /* IE6,IE7 */
    filter: progid:DXImageTransform.Microsoft.Matrix(sizingMethod='auto expand', M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476);
    /* IE8 */
    -ms-filter: "progid:DXImageTransform.Microsoft.Matrix(SizingMethod='auto expand', M11=0.7071067811865476, M12=-0.7071067811865475, M21=0.7071067811865475, M22=0.7071067811865476)";

我只想让用户拖动图像,并让它在拖动时保持旋转到位。非常感谢任何帮助。

【问题讨论】:

  • 可以将它们绘制、旋转到画布上并移动它们。也可以将每个包装在一个 SVG 中并拖动它。
  • @Ouroborus 这是一个好主意,如果一切都失败了,我可能最终会这样做。感谢您的建议。
  • 能否请您添加您的拖放代码。
  • @SamyakJain 我没有拖放代码,因为我无法成功地想出任何可以远程工作的东西。我只是通过鼠标光标拖动图像。

标签: javascript jquery reactjs css


【解决方案1】:

你可以通过javascripts处理-

覆盖浏览器的拖动事件。这将使您的旋转 css 保留在那里。

ball.ondragstart = function() {
                return false;
                };

完整的拖放代码 -

<img src="./samyak/images/0.jpeg" style="cursor: pointer; position: absolute; z-index: 1000; left: 777px; top: 39px; transition: auto 0s ease 0s;" width="40" height="40" id="ball">
<script>
            ball = document.getElementById("ball");
            ball.style.transform="rotate(30deg)"
            ball.ondragstart = function() {
            return false;
            };
            ball.onmousedown = function(event) { // (1) start the process

            // (2) prepare to moving: make absolute and on top by z-index
            ball.style.position = 'absolute';
            ball.style.zIndex = 1000;
            // move it out of any current parents directly into body
            // to make it positioned relative to the body
            document.body.append(ball);
            // ...and put that absolutely positioned ball under the cursor

            moveAt(event.pageX, event.pageY);

            // centers the ball at (pageX, pageY) coordinates
            function moveAt(pageX, pageY) {
            ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
            ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
            }

            function onMouseMove(event) {
            moveAt(event.pageX, event.pageY);
            }

            // (3) move the ball on mousemove
            document.addEventListener('mousemove', onMouseMove);

            // (4) drop the ball, remove unneeded handlers
            ball.onmouseup = function() {
            document.removeEventListener('mousemove', onMouseMove);
            ball.onmouseup = null;
            };

        }
</script>

【讨论】:

  • 这并没有达到我想要的效果,但稍加编辑它就非常适合我的项目。我完全回答了这个问题,所以这个答案是正确的。
猜你喜欢
  • 2012-12-02
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多