JqueryUI - Position an element relative to the window, document, another element, or the cursor/mouse.


SUBMITTED BY: jlolk3r

DATE: Jan. 21, 2016, 2:32 a.m.

FORMAT: HTML

SIZE: 2.6 kB

HITS: 781

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI Position - Image Cycler</title>
  6. <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  7. <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  8. <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  9. <link rel="stylesheet" href="/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. function left( element, using ) {
  26. element.position({
  27. my: "right middle",
  28. at: "left+25 middle",
  29. of: "#container",
  30. collision: "none",
  31. using: using
  32. });
  33. }
  34. function right( element, using ) {
  35. element.position({
  36. my: "left middle",
  37. at: "right-25 middle",
  38. of: "#container",
  39. collision: "none",
  40. using: using
  41. });
  42. }
  43. function center( element, using ) {
  44. element.position({
  45. my: "center middle",
  46. at: "center middle",
  47. of: "#container",
  48. using: using
  49. });
  50. }
  51. left( $( "img:eq(0)" ) );
  52. center( $( "img:eq(1)" ) );
  53. right( $( "img:eq(2)" ) );
  54. function animate( to ) {
  55. $( this ).stop( true, false ).animate( to );
  56. }
  57. function next( event ) {
  58. event.preventDefault();
  59. center( $( "img:eq(2)" ), animate );
  60. left( $( "img:eq(1)" ), animate );
  61. right( $( "img:eq(0)" ).appendTo( "#container" ) );
  62. }
  63. function previous( event ) {
  64. event.preventDefault();
  65. center( $( "img:eq(0)" ), animate );
  66. right( $( "img:eq(1)" ), animate );
  67. left( $( "img:eq(2)" ).prependTo( "#container" ) );
  68. }
  69. $( "#previous" ).click( previous );
  70. $( "#next" ).click( next );
  71. $( "img" ).click(function( event ) {
  72. $( "img" ).index( this ) === 0 ? previous( event ) : next( event );
  73. });
  74. $( window ).resize(function() {
  75. left( $( "img:eq(0)" ), animate );
  76. center( $( "img:eq(1)" ), animate );
  77. right( $( "img:eq(2)" ), animate );
  78. });
  79. });
  80. </script>
  81. </head>
  82. <body>
  83. <div id="container">
  84. <img src="images/earth.jpg" width="458" height="308" alt="earth">
  85. <img src="images/flight.jpg" width="512" height="307" alt="flight">
  86. <img src="images/rocket.jpg" width="300" height="353" alt="rocket">
  87. <a id="previous" href="#">Previous</a>
  88. <a id="next" href="#">Next</a>
  89. </div>
  90. </body>
  91. </html>

comments powered by Disqus