【问题标题】:How does 'containment' in draggable widget work可拖动小部件中的“包含”如何工作
【发布时间】:2013-12-11 13:14:31
【问题描述】:

结帐thisjsfiddle

我在这里要做的是通过父 div 显示图像。

<div id="main">
    <img src="http://....">
</div>

父元素比图像小并且溢出设置为隐藏。 要拖动图像,我使用 jQuery UI 中的Draggable widget。

我希望能够通过拖动来显示整个图像,但我不想看到#main 的背景颜色。所以我应该无法将图像拖到很远的方向。 我认为这可以使用 'containment' 属性来完成,但我无法让它发挥作用。

例如,如果#main 是 200 x 200 并且图像是 300x300,我会说它应该看起来像这样

$(img).draggable({containment: [0,0,-100,-100]});

有什么建议为什么 jsfiddle 不起作用以及如何解决这个问题?

【问题讨论】:

    标签: javascript jquery-ui draggable jquery-ui-draggable


    【解决方案1】:

    我必须考虑容器的宽度、位置、边框和图像尺寸。

    var img = $('img'), 
        pos = { offset: $('#main').offset(), 
                height: $('#main').height(), 
                width: $('#main').width() 
        };
    img.draggable({ 
        containment: [
            pos.offset.left + pos.width + 1 - img.width(), 
            pos.offset.top + pos.height + 1 - img.height(), 
            pos.offset.left + 1, 
            pos.offset.top + 1 ]
    });
    

    Here is an updated jsfiddle.

    【讨论】:

      猜你喜欢
      • 2023-01-16
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 2017-05-31
      • 2011-12-03
      • 1970-01-01
      相关资源
      最近更新 更多