jQuery Position API中的“使用”选项

时间:2019-07-01 17:42:25

标签: jquery-ui position jquery-ui-tooltip jquery-widgets

在jQuery文档中,反馈参数给出了水平,垂直和重要位置,并为您提供了12个可能的方向,例如{水平:“中心”,垂直:“左侧”,重要:“水平”}。

参考:https://api.jqueryui.com/1.12/position/

可能的12个方向是什么?如何到达12个方向?重要的是什么?

根据jQuery ui代码,我只能看到设置了这些值。

水平-左,右,中心 垂直-顶部,底部,中间 重要-水平/垂直

没有为垂直设置值“左”。

1 个答案:

答案 0 :(得分:0)

可以肯定的是:

Horizontal: "left", "center", "right"
Vertical:   "top", "middle", "bottom"
Important:  "horizontal", "vertical"
  

第二个元素提供有关两个元素的位置和尺寸以及它们相对位置的计算的反馈。 targetelement都具有以下属性:elementlefttopwidthheight。此外,还有horizontalverticalimportant,为您提供十二个潜在的方向,例如{ horizontal: "center", vertical: "left", important: "horizontal" }

我认为important是另外两个方向(总共6个方向)中哪个优先。 6 x 2 = 12?我不确定他们的逻辑。还不清楚如何使用它。我创建了以下内容进行测试。

$(function() {
  function makeDiv(event) {
    var d = $("<div>", {
      id: "position" + ($("[id^='position']", event.target).length + 1),
      class: "positionDiv circle"
    }).appendTo($(event.target));
    d.position({
      my: "center",
      of: event,
      using: function(pos, props) {
        console.log(pos, props);
        var tg = props.target,
          el = props.element;
        $(this).css(pos);
        $(this).html("T: " + pos.top + "<br />L: " + pos.left + "<br />H: " + props.horizontal +"<br />V: " + props.vertical + "<br />I: " + props.important);
      }
    });
  }
  $("#position1").position({
    my: "center",
    at: "center",
    of: "#targetElement"
  });
  $("#targetElement").click(makeDiv);
});
#targetElement {
  height: 200px;
  margin: 10px;
  background: #9cf;
}

.positionDiv {
  position: absolute;
  width: 75px;
  height: 75px;
  background: #080;
}

.positionDiv.circle {
  border-radius: 50%;
  text-align: center;
  font-family: 'Arial', sans-serif;
  font-size: 12px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="targetElement">
  <div class="positionDiv" id="position1"></div>
</div>

并非您要找的答案。希望对您有所帮助。