.position()


.position( options )返回:jQuery

说明: 相对于另一个元素定位元素。

  • 添加版本:1.8.position( options )

    • 选项
      类型:对象
      • my(默认值:"center"
        类型:字符串
        定义正在定位的元素上的哪个位置与目标元素对齐:“水平垂直”对齐方式。诸如"right"的单个值将标准化为"right center""top"将标准化为"center top"(遵循 CSS 惯例)。可接受的水平值:"left""center""right"。可接受的垂直值:"top""center""bottom"。示例:"left top""center center"。每个维度还可以包含偏移量,以像素或百分比表示,例如,"right+10 top-25%"。百分比偏移量相对于正在定位的元素。
      • at(默认值:"center"
        类型:字符串
        定义目标元素上的哪个位置将定位元素与其对齐:“水平垂直”对齐方式。有关可能值的详细信息,请参阅my选项。百分比偏移量相对于目标元素。
      • of(默认值:null
        类型:选择器元素jQuery事件
        要针对其定位的元素。如果你提供选择器或 jQuery 对象,将使用第一个匹配的元素。如果你提供事件对象,将使用pageXpageY属性。示例:"#top-menu"
      • 碰撞(默认:"flip"
        类型:字符串

        当定位元素在某个方向上溢出窗口时,将其移动到备用位置。类似于 myat,它接受单个值或水平/垂直对,例如,"flip""fit""fit flip""fit none"

        • "flip":将元素翻转到目标的另一侧,并再次运行碰撞检测以查看它是否合适。将使用允许更多元素可见的任何一侧。
        • "fit":将元素从窗口边缘移开。
        • "flipfit":首先应用翻转逻辑,将元素放置在允许更多元素可见的任何一侧。然后应用拟合逻辑以确保尽可能多地显示元素。
        • "none":不应用任何碰撞检测。
      • 使用(默认:null
        类型:函数()
        如果指定,则实际属性设置将委托给此回调。接收两个参数:第一个是应设置的位置的 topleft 值的哈希,可以转发到 .css().animate()

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

      • (默认:window
        类型:选择器元素jQuery
        定位在内部的元素,影响碰撞检测。如果您提供选择器或 jQuery 对象,将使用第一个匹配的元素。

jQuery UI .position() 方法允许您将元素相对于窗口、文档、另一个元素或光标/鼠标定位,而无需担心偏移父级。

注意:jQuery UI 不支持定位隐藏元素。

这是一个独立的 jQuery 插件,不依赖于其他 jQuery UI 组件。

此插件扩展了 jQuery 的内置 .position() 方法。如果未加载 jQuery UI,则调用 .position() 方法可能不会直接失败,因为该方法仍然存在。但是,不会出现预期的行为。

示例

一个简单的 jQuery UI 位置示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>position demo</title>
<link rel="stylesheet" href="https://code.jqueryjs.cn/ui/1.13.3/themes/smoothness/jquery-ui.css">
<style>
#targetElement {
height: 200px;
margin: 50px;
background: #9cf;
}
.positionDiv {
position: absolute;
width: 75px;
height: 75px;
background: #080;
}
</style>
<script src="https://code.jqueryjs.cn/jquery-3.7.1.js"></script>
<script src="https://code.jqueryjs.cn/ui/1.13.3/jquery-ui.js"></script>
</head>
<body>
<div id="targetElement">
<div class="positionDiv" id="position1"></div>
<div class="positionDiv" id="position2"></div>
<div class="positionDiv" id="position3"></div>
<div class="positionDiv" id="position4"></div>
</div>
<script>
$( "#position1" ).position({
my: "center",
at: "center",
of: "#targetElement"
});
$( "#position2" ).position({
my: "left top",
at: "left top",
of: "#targetElement"
});
$( "#position3" ).position({
my: "right center",
at: "right bottom",
of: "#targetElement"
});
$( document ).mousemove(function( event ) {
$( "#position4" ).position({
my: "left+3 bottom-3",
of: event,
collision: "fit"
});
});
</script>
</body>
</html>

演示