课程表

jQuery UI 基础

jQuery UI 主题

jQuery UI 部件库

jQuery UI 参考手册

jQuery UI 实例

工具箱
速查手册

定位(Position)

当前位置:免费教程 » JS/JS库/框架 » jQuery UI

相对窗口、文档、锚、光标/鼠标等元素定位一个元素。

如需了解更多有关 .position() 方法的细节,请查看 API 文档 .position()

默认功能

使用表单控件配置位置,或者拖拽被定位的元素来修改它的偏移量。向四周拖拽父元素来查看碰撞检测。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 定位(Position) - 默认功能</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10. <style>
  11. #parent {
  12. width: 60%;
  13. height: 40px;
  14. margin: 10px auto;
  15. padding: 5px;
  16. border: 1px solid #777;
  17. background-color: #fbca93;
  18. text-align: center;
  19. }
  20. .positionable {
  21. position: absolute;
  22. display: block;
  23. right: 0;
  24. bottom: 0;
  25. background-color: #bcd5e6;
  26. text-align: center;
  27. }
  28. #positionable1 {
  29. width: 75px;
  30. height: 75px;
  31. }
  32. #positionable2 {
  33. width: 120px;
  34. height: 40px;
  35. }
  36. select, input {
  37. margin-left: 15px;
  38. }
  39. </style>
  40. <script>
  41. $(function() {
  42. function position() {
  43. $( ".positionable" ).position({
  44. of: $( "#parent" ),
  45. my: $( "#my_horizontal" ).val() + " " + $( "#my_vertical" ).val(),
  46. at: $( "#at_horizontal" ).val() + " " + $( "#at_vertical" ).val(),
  47. collision: $( "#collision_horizontal" ).val() + " " + $( "#collision_vertical" ).val()
  48. });
  49. }
  50. $( ".positionable" ).css( "opacity", 0.5 );
  51. $( "select, input" ).bind( "click keyup change", position );
  52. $( "#parent" ).draggable({
  53. drag: position
  54. });
  55. position();
  56. });
  57. </script>
  58. </head>
  59. <body>
  60. <div id="parent">
  61. <p>
  62. 这是父元素的位置。
  63. </p>
  64. </div>
  65. <div class="positionable" id="positionable1">
  66. <p>
  67. to position
  68. </p>
  69. </div>
  70. <div class="positionable" id="positionable2">
  71. <p>
  72. to position 2
  73. </p>
  74. </div>
  75. <div style="padding: 20px; margin-top: 75px;">
  76. 定位...
  77. <div style="padding-bottom: 20px;">
  78. <b>my:</b>
  79. <select id="my_horizontal">
  80. <option value="left">left</option>
  81. <option value="center">center</option>
  82. <option value="right">right</option>
  83. </select>
  84. <select id="my_vertical">
  85. <option value="top">top</option>
  86. <option value="center">center</option>
  87. <option value="bottom">bottom</option>
  88. </select>
  89. </div>
  90. <div style="padding-bottom: 20px;">
  91. <b>at:</b>
  92. <select id="at_horizontal">
  93. <option value="left">left</option>
  94. <option value="center">center</option>
  95. <option value="right">right</option>
  96. </select>
  97. <select id="at_vertical">
  98. <option value="top">top</option>
  99. <option value="center">center</option>
  100. <option value="bottom">bottom</option>
  101. </select>
  102. </div>
  103. <div style="padding-bottom: 20px;">
  104. <b>collision:</b>
  105. <select id="collision_horizontal">
  106. <option value="flip">flip</option>
  107. <option value="fit">fit</option>
  108. <option value="flipfit">flipfit</option>
  109. <option value="none">none</option>
  110. </select>
  111. <select id="collision_vertical">
  112. <option value="flip">flip</option>
  113. <option value="fit">fit</option>
  114. <option value="flipfit">flipfit</option>
  115. <option value="none">none</option>
  116. </select>
  117. </div>
  118. </div>
  119. </body>
  120. </html>

我要试一下

图像循环

一个照片浏览器的原型,使用 Position 分别把图片定为在左边、中间、右边,然后循环显示。使用顶部的链接来循环图像,或者在图像的左侧或右侧点击来循环图像。请注意,当调整窗口大小时,会重新定位图像。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI 定位(Position) - 图像循环</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  8. <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9. <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  10. <style>
  11. body {
  12. margin: 0;
  13. }
  14. #container {
  15. overflow: hidden;
  16. position: relative;
  17. height: 400px;
  18. }
  19. img {
  20. position: absolute;
  21. }
  22. </style>
  23. <script>
  24. $(function() {
  25. // 重构部件,去除这些插件方法
  26. $.fn.left = function( using ) {
  27. return this.position({
  28. my: "right middle",
  29. at: "left+25 middle",
  30. of: "#container",
  31. collision: "none",
  32. using: using
  33. });
  34. };
  35. $.fn.right = function( using ) {
  36. return this.position({
  37. my: "left middle",
  38. at: "right-25 middle",
  39. of: "#container",
  40. collision: "none",
  41. using: using
  42. });
  43. };
  44. $.fn.center = function( using ) {
  45. return this.position({
  46. my: "center middle",
  47. at: "center middle",
  48. of: "#container",
  49. using: using
  50. });
  51. };
  52. $( "img:eq(0)" ).left();
  53. $( "img:eq(1)" ).center();
  54. $( "img:eq(2)" ).right();
  55. function animate( to ) {
  56. $( this ).stop( true, false ).animate( to );
  57. }
  58. function next( event ) {
  59. event.preventDefault();
  60. $( "img:eq(2)" ).center( animate );
  61. $( "img:eq(1)" ).left( animate );
  62. $( "img:eq(0)" ).right().appendTo( "#container" );
  63. }
  64. function previous( event ) {
  65. event.preventDefault();
  66. $( "img:eq(0)" ).center( animate );
  67. $( "img:eq(1)" ).right( animate );
  68. $( "img:eq(2)" ).left().prependTo( "#container" );
  69. }
  70. $( "#previous" ).click( previous );
  71. $( "#next" ).click( next );
  72. $( "img" ).click(function( event ) {
  73. $( "img" ).index( this ) === 0 ? previous( event ) : next( event );
  74. });
  75. $( window ).resize(function() {
  76. $( "img:eq(0)" ).left( animate );
  77. $( "img:eq(1)" ).center( animate );
  78. $( "img:eq(2)" ).right( animate );
  79. });
  80. });
  81. </script>
  82. </head>
  83. <body>
  84. <div id="container">
  85. <img src="/wp-content/uploads/2014/03/earth.jpg" width="458" height="308" alt="earth">
  86. <img src="/wp-content/uploads/2014/03/flight.jpg" width="512" height="307" alt="flight">
  87. <img src="/wp-content/uploads/2014/03/rocket.jpg" width="300" height="353" alt="rocket">
  88. <a id="previous" href="#">上一个</a>
  89. <a id="next" href="#">下一个</a>
  90. </div>
  91. </body>
  92. </html>

我要试一下

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号