]> source.dussan.org Git - jquery-ui.git/commitdiff
Position: Expose target and element dimensions to allow further customization, like...
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Fri, 13 Apr 2012 14:05:29 +0000 (16:05 +0200)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Fri, 13 Apr 2012 14:05:29 +0000 (16:05 +0200)
tests/visual/position/position_feedback.html
tests/visual/position/position_feedback_rotate.html [new file with mode: 0644]
ui/jquery.ui.position.js

index 5b50ff32c62a7207a58e0ccbf09b54d7e633ada2..f23d8a2990165b75c649f65c29731cf4b90df016 100644 (file)
        <script type="text/javascript">
        $(function() {
                function using( position, feedback ) {
-                       $(this).offset( position );
-                       $(this).text(feedback.horizontal + " " + feedback.vertical + " " + feedback.important)
                        $(this)
-                               .removeClass("left right top bottom center middle vertical horizontal")
-                               .addClass(feedback.horizontal)
-                               .addClass(feedback.vertical)
-                               .addClass(feedback.important);
+                               .offset( position )
+                               .text( feedback.horizontal + " " + feedback.vertical + " " + feedback.important )
+                               .removeClass( "left right top bottom center middle vertical horizontal" )
+                               .addClass( feedback.horizontal )
+                               .addClass( feedback.vertical )
+                               .addClass( feedback.important );
                }
                var target = $("#target").position({
                        my: "center",
diff --git a/tests/visual/position/position_feedback_rotate.html b/tests/visual/position/position_feedback_rotate.html
new file mode 100644 (file)
index 0000000..a93287e
--- /dev/null
@@ -0,0 +1,105 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+       <meta charset="UTF-8" />
+       <title>Position Visual Test: Default</title>
+       <link rel="stylesheet" href="../visual.css" type="text/css" />
+       <link rel="stylesheet" href="../../../themes/base/jquery.ui.all.css" type="text/css" title="ui-theme" />
+       <script type="text/javascript" src="../../../jquery-1.7.2.js"></script>
+       <script type="text/javascript" src="../../../ui/jquery.ui.core.js"></script>
+       <script type="text/javascript" src="../../../ui/jquery.ui.widget.js"></script>
+       <script type="text/javascript" src="../../../ui/jquery.ui.position.js"></script>
+       <script type="text/javascript" src="../../../ui/jquery.ui.menu.js"></script>
+       <script type="text/javascript">
+       $(function() {
+               function using( position, feedback ) {
+                       var angleRad = Math.atan2( feedback.target.top + feedback.target.height / 2 - feedback.element.top - feedback.element.height / 2, feedback.target.left + feedback.target.width / 2 - feedback.element.left - feedback.element.width / 2 ) * 180 / Math.PI;
+                       var angle = (Math.round( angleRad * 100) / 100 );
+                       $(this)
+                               .offset( position )
+                               .text( feedback.horizontal + " " + feedback.vertical + " " + feedback.important + " " + angle + "°" )
+                               .css({
+                                       "-webkit-transform": "rotate(" + angle + "deg)",
+                                       "-moz-transform": "rotate(" + angle + "deg)",
+                                       "-ms-transform": "rotate(" + angle + "deg)",
+                                       "-o-transform": "rotate(" + angle + "deg)",
+                                       "transform": "rotate(" + angle + "deg)"
+                               });
+               }
+               var target = $("#target").position({
+                       my: "center",
+                       at: "center",
+                       of: window
+               });
+               var element = $(".element");
+               $.each([
+                       "center top-100",
+                       "right+25 top-50",
+                       "right+75 top",
+                       "right+75 center",
+                       "right+75 bottom",
+                       "right+25 bottom+50",
+                       "center bottom+100",
+                       "left-25 bottom+50",
+                       "left-75 bottom",
+                       "left-75 center",
+                       "left-75 top",
+                       "left-25 top-50"
+               ], function(index, direction) {
+                       element.clone().insertAfter(target).position({
+                               my: "center",
+                               at: direction,
+                               of: target,
+                               using: using
+                       })
+               });
+
+               var targetOffset = target.offset();
+               var oppositeElement = element.clone().width(50).appendTo('body');
+               var leftElement = element.clone().width(50).height(150).appendTo('body');
+               var rightElement = element.clone().height(150).width(150).appendTo('body');
+               element.width(150);
+               $(document).on( "mousemove", function(event) {
+                       var base = {
+                               my: "left top",
+                               at: "left top",
+                               of: target,
+                               using: using
+                       }
+                       element.position( $.extend({
+                               offset: (event.pageX - targetOffset.left) + " " + (event.pageY - targetOffset.top)
+                       }, base));
+                       oppositeElement.position( $.extend({
+                               offset: (-1 * (event.pageX - targetOffset.left)) + " " + (-1 * (event.pageY - targetOffset.top))
+                       }, base));
+                       leftElement.position( $.extend({
+                               offset: (-0.9 * (event.pageX - targetOffset.left)) + " " + (0.9 * (event.pageY - targetOffset.top))
+                       }, base));
+                       rightElement.position( $.extend({
+                               offset: (0.9 * (event.pageX - targetOffset.left)) + " " + (-0.9 * (event.pageY - targetOffset.top))
+                       }, base));
+               });
+       });
+       </script>
+       <style>
+               #target, .element {
+                       position: absolute;
+                       border: 1px solid black;
+                       border-radius: 5px;
+                       width: 75px;
+                       height: 25px;
+                       padding: 5px;
+               }
+               #target {
+                       height: 75px;
+               }
+       </style>
+</head>
+<body>
+
+       <div id="target">all around me</div>
+
+       <div class="element"></div>
+
+</body>
+</html>
index 31da2237b991d482bf0ad6317bcf4cb3f834296d..17d43b2a45b5f711020a6d1a8e26509a4034755f 100644 (file)
@@ -241,9 +241,22 @@ $.fn.position = function( options ) {
                                        top = targetOffset.top - props.top,
                                        bottom = (targetOffset.top + targetHeight) - (props.top + elemHeight),
                                        feedback = {
+                                               target: {
+                                                       element: target,
+                                                       left: targetOffset.left,
+                                                       top: targetOffset.top,
+                                                       width: targetWidth,
+                                                       height: targetHeight
+                                               },
+                                               element: {
+                                                       element: elem,
+                                                       left: props.left,
+                                                       top: props.top,
+                                                       width: elemWidth,
+                                                       height: elemHeight
+                                               },
                                                horizontal: right < 0 ? "left" : left > 0 ? "right" : "center",
-                                               vertical: bottom < 0 ? "top" : top > 0 ? "bottom" : "middle",
-                                               target: target
+                                               vertical: bottom < 0 ? "top" : top > 0 ? "bottom" : "middle"
                                        },
                                        max = Math.max,
                                        abs = Math.abs;