Draggable And Margin:0 auto;

用户名

jQuery UI可排序/可拖动存在一个问题,如果要拖动的元素使用居中margin:0 auto,则拖动开始于容器的左侧,而不是元素实际位于的中心。

这是一个演示我的意思的演示:http : //codepen.io/anon/pen/YqWRvV只需拖动红色方块即可。

如果margin:0 auto从“ .drag”元素中删除,您将看到拖动正常开始。

如何在居中使用时解决此问题并使元素从实际位置拖动margin:0 auto

这似乎发生在Google Chrome中。

$("#c").sortable();

`.drag {
   margin: 0 auto;
   width: 200px;
   height: 200px;
   background-color: red;
}`

将元素与CSS'calc'居中即可解决此问题,但我的元素宽度可以动态更改。同样,“计算”不如“保证金”得到很好的支持。

将可拖动包装在容器中可以解决此问题,但是HTML更改并不是我想要的解决方案。

更新:所以这不是jQuery的错误。这是Google Chrome的错误。显然是chrome检索了错误的位置,因此jQuery从chrome告诉它元素所在的位置开始拖动。我在控制台中输出了该元素的左侧位置,您可以看到它显然不是13px。http://codepen.io/anon/pen/YqWRvV我想知道是否已报告此错误。

解决方案 http://codepen.io/anon/pen/MyjeJP感谢JulienGrégoire注意:此解决方案可能需要一种刷新助手位置的方法。

朱利安·格雷戈尔

Chrome返回的位置与Firefox不同,但是您可以使用offset获得正确的坐标,而offset是可排序的。问题在于,在mouseStart上,边距已从计算中删除,当您设置了边距时,这可能很有意义。但是使用自动保证金会产生此问题。

您可以添加一个选项来忽略边距并修改_mouseStart像这样:

$("#c").sortable({
  ignoreMargins: true
});

$.ui.sortable.prototype._mouseStart = function(event, overrideHandle, noActivation) {
    ...
    if (!this.options.ignoreMargins) {

      this.offset = {
        top: this.offset.top - this.margins.top,
        left: this.offset.left - this.margins.left
      };
    }
    ...
}

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

编辑:

如果您不想修改插件,则可以使用startstop事件。一个问题是,很难检查是否已将边距设置为自动,因此您需要在可排序的调用中定义它们,这不应该灵活,或者找到一种方法来动态检查哪些边距是自动的。

$("#c").sortable({
  start: function(e, ui) {
    var marginsToSet = ui.item.data().sortableItem.margins;
    // You set the margins here, so they don't go back to 0 
    // once the item is put in absolute position
      ui.item.css('margin-left', marginsToSet.left);
      ui.item.css('margin-top', marginsToSet.top);

  },
  stop: function(e, ui) {
    // Then you need to set the margins back once you stop dragging.
    // Ideally this should be dynamic, but you have to be able
    // to check which margins are set to auto, which as you'll
    // see if you look for some answers on this site, 
    // doesn't look that simple.
    ui.item.css('margin', '20px auto');
  }
});

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

margin 0 auto会使元素居中,为什么margin 1 auto不会做同样的事情?

来自分类Dev

css margin:0 auto在Firefox中不起作用

来自分类Dev

如何在jQuery中使用margin:0 auto?

来自分类Dev

CSS-Divs不在'Auto:0 Margin'居中

来自分类Dev

从margin:auto动画元素。

来自分类Dev

我应该使用margin 0 auto还是将position,top,left对齐以使内容居中

来自分类Dev

使用“ margin:0 auto;” 和背景色,不带内部div

来自分类Dev

尽管我使用了“ margin:0 auto”,但右侧的部分仍多了10px

来自分类Dev

当我使用 margin: 0 auto 时,为什么我的标题中心不显示?

来自分类Dev

为什么在使用“margin: 0 auto”时我的图像不会居中

来自分类Dev

我的 max-width:1140px 不起作用它不能居中我的 margin:0 auto

来自分类Dev

Div不能与margin:auto对齐

来自分类Dev

修复了使用margin-auto滚动条跳跃的问题

来自分类Dev

'margin:auto;`不适用于内联块元素

来自分类Dev

为什么“ margin:auto”不使元素垂直居中?

来自分类Dev

将div中的div居中,而不使用margin auto

来自分类Dev

如果margin:auto不起作用,如何使div对准中心?

来自分类Dev

在while循环中使用margin auto和inline block

来自分类Dev

隐藏溢出不适用于 flexbox 和 margin auto

来自分类Dev

Django SET SQL_AUTO_IS_NULL = 0

来自分类Dev

CSS:left:auto不会覆盖left:0:

来自分类Dev

使div以margin 0自动响应

来自分类Dev

jQuery Draggable投掷错误

来自分类Dev

Set bounds for draggable element

来自分类Dev

jQuery Draggable禁用样式

来自分类Dev

在`Droppable`之后追加`Draggable`

来自分类Dev

自动拖动“ .draggable”元素

来自分类Dev

HTML / CSS div ::为什么height:auto = 0?

来自分类Dev

BootStrap CSS margin 0自动不在中心

Related 相关文章

  1. 1

    margin 0 auto会使元素居中,为什么margin 1 auto不会做同样的事情?

  2. 2

    css margin:0 auto在Firefox中不起作用

  3. 3

    如何在jQuery中使用margin:0 auto?

  4. 4

    CSS-Divs不在'Auto:0 Margin'居中

  5. 5

    从margin:auto动画元素。

  6. 6

    我应该使用margin 0 auto还是将position,top,left对齐以使内容居中

  7. 7

    使用“ margin:0 auto;” 和背景色,不带内部div

  8. 8

    尽管我使用了“ margin:0 auto”,但右侧的部分仍多了10px

  9. 9

    当我使用 margin: 0 auto 时,为什么我的标题中心不显示?

  10. 10

    为什么在使用“margin: 0 auto”时我的图像不会居中

  11. 11

    我的 max-width:1140px 不起作用它不能居中我的 margin:0 auto

  12. 12

    Div不能与margin:auto对齐

  13. 13

    修复了使用margin-auto滚动条跳跃的问题

  14. 14

    'margin:auto;`不适用于内联块元素

  15. 15

    为什么“ margin:auto”不使元素垂直居中?

  16. 16

    将div中的div居中,而不使用margin auto

  17. 17

    如果margin:auto不起作用,如何使div对准中心?

  18. 18

    在while循环中使用margin auto和inline block

  19. 19

    隐藏溢出不适用于 flexbox 和 margin auto

  20. 20

    Django SET SQL_AUTO_IS_NULL = 0

  21. 21

    CSS:left:auto不会覆盖left:0:

  22. 22

    使div以margin 0自动响应

  23. 23

    jQuery Draggable投掷错误

  24. 24

    Set bounds for draggable element

  25. 25

    jQuery Draggable禁用样式

  26. 26

    在`Droppable`之后追加`Draggable`

  27. 27

    自动拖动“ .draggable”元素

  28. 28

    HTML / CSS div ::为什么height:auto = 0?

  29. 29

    BootStrap CSS margin 0自动不在中心

热门标签

归档