【问题标题】:Resizing issue with interact js使用交互 js 调整大小问题
【发布时间】:2019-08-14 00:12:44
【问题描述】:

我正在使用 interactjs 创建一个屏幕设计示例,但我在调整大小时遇到​​了问题。当我将盒子的大小调整到两侧容器的边缘时,我不能再缩小盒子了。这是我的问题的示例:http://codepen.io/anon/pen/ORmVZk
我该如何解决这个问题。谢谢...

js:

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

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

    // update the posiion attributes
    target.setAttribute('data-x', x);
    target.setAttribute('data-y', y);
}

var klas=0;
addDiv = function(el){
    var cls="div-"+klas;
  var color = getRandomColor();
    $(".designScreenContainer").append("<div class='dBox "+cls+" "+el+"' style='background-color:"+color+"'></div>");
    interact("."+cls)
        .draggable({
            // enable inertial throwing
            inertia: true,
            // keep the element within the area of it's parent
            restrict: {
                restriction: "parent",
                endOnly: true,
                elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
            },
            onmove: window.dragMoveListener,
            snap: {
                targets: [
                    interact.createSnapGrid({ x: 5, y: 5 })
                ],
                range: Infinity,
                relativePoints: [ { x: 0, y: 0 } ]
            },
        })
        .resizable({
            // preserveAspectRatio: true,
            edges: { left: true, right: true, bottom: true, top: true },
            restrict: {
                // endOnly: true,
                restriction: '.designScreenContainer',
                elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
            }
        })
        .on('resizemove', function (event) {
            var target = event.target,
                x = (parseFloat(target.getAttribute('data-x')) || 0),
                y = (parseFloat(target.getAttribute('data-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;

            target.style.webkitTransform = target.style.transform =
                'translate(' + x + 'px,' + y + 'px)';

            target.setAttribute('data-x', x);
            target.setAttribute('data-y', y);
            target.textContent = Math.round(event.rect.width) + '×' + Math.round(event.rect.height);
        });

    klas++;
}

function getRandomColor() {
    var letters = '0123456789ABCDEF'.split('');
    var color = '#';
    for (var i = 0; i < 6; i++) {
        color += letters[Math.floor(Math.random() * 16)];
    }
    return color;
}

html:

  <div class="row">
  <div class="col s9 designScreenContainer grey darken-4" style="height:200px;margin:10px auto;float:none;"></div>
</div>


<button onclick="addDiv()">Add New Box</button>

【问题讨论】:

    标签: resize interact.js


    【解决方案1】:

    这里有点晚了,但我遇到了同样的问题。似乎为可拖动的interactjs打破了限制界限,

    删除这一行,它应该可以工作

    elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
    

    或改成

    elementRect: { top: 1, left: 1, bottom: 1, right: 1 }
    

    【讨论】:

    • 感谢您的回复,但我已将“interact.js”更改为“fabric.js”。
    • @MtMy 真有趣!我从“fabric.js”更改为“interact.js”。也许你可以看看这个错误并告诉我它为什么这样做,这样我就可以回去了。 stackoverflow.com/questions/62457067/…
    猜你喜欢
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 2011-07-05
    • 2013-04-15
    相关资源
    最近更新 更多