Jquery UI可排序检测轴向上和向下

时间:2015-03-27 15:12:33

标签: javascript jquery jquery-ui resizable

我正在努力检测轴N是向上还是向下移动?而且我想要内部的物体(h2和p),保持在位置上。有人可以帮忙吗!

http://jsfiddle.net/zerolfc/y62awe4u/2/

<div class="ss-zone-object" style="top: 68px; width:300px; height: 500px; left: 20px;">
    <h2 style="top: 100px; width: 60px;">Title</h2>
    <p style="left: 122px; top: 120px; width: 60px;">Summary</p>
</div>

 $(function () {
    $('.ss-zone-object').resizable({
        grid: [1, 1],
        handles: 'n,s',
        resize: function (e, ui) {
            var handle_axis = $(this).data('ui-resizable').axis;

            $(this).find('h2, p').each(function (mk, mv) {

                var obj_top = parseInt($(this).position().top);
                if (handle_axis === 'n') { // Axis N, moving down then minus one
                    $(this).css('top', (obj_top - 1) + 'px');
                } else if (handle_axis === 's') {

                }
            });

        },
        stop: function (e, ui) {}
    });
});

3 个答案:

答案 0 :(得分:1)

Oke我有例如,这是:

&#13;
&#13;
$(function () {
    var s = $('h2').position().top;
    var p = $('h2').position().top;
    var height_font_s = 68; // define manual
    var height_font_p = 88; // define manual
    $('.ss-zone-object').resizable({
        grid: [1, 1],
        handles: 'n,s',
        resize: function (e, ui) {
            var handle_axis = $(this).data('ui-resizable').axis;
            var t = $('.ss-zone-object').position().top;
            $(this).find('h2').css('top', (-(t-(s+height_font_s)))+'px');
            $(this).find('p').css('top', (-(t-(p+height_font_p)))+'px');
        },
        stop: function (e, ui) {}
    });

})
&#13;
.ss-zone-object {
    background-color: #ccc;
    position:absolute;
}
.ss-zone-object h2, .ss-zone-object p {
    position:absolute;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<div class="ss-zone-object" style="top: 68px; width:300px; height: 500px; left: 20px;">
     <h2 style="top: 100px; width: 60px;">Title</h2>

    <p style="left: 122px; top: 120px; width: 60px;">Summary</p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

不确定你在这里想要实现的目标。

  

我正在努力检测轴N是向上还是向下移动?

如果要检测轴(N或S),您可以在resize事件处理程序中检查以下内容:

var handle_axis = ui.originalPosition.top === ui.position.top ? "s" : "n";
  

并且我也希望对象(h2和p)在里面,保持在位置上。

当调整南轴的大小时,没有问题,因为元素位于contianer的顶部。

调整北轴的大小时,两个元素的顶部位置永远不会相对于父级发生变化。但是,您可以使用ui.originalSize和ui.size来查找新的顶部位置。或者只是设定位置固定。

答案 2 :(得分:0)

我设法通过添加临时克隆div来实现。

http://jsfiddle.net/zerolfc/y62awe4u/9/

$('.ss-zone-clone').resizable({
        grid: [1, 1],
        handles: 'n,s',
        containtment: 'parent',
        start: function (e, ui) {
            $(this).css({
                'background': 'rgba(255,255,255,0.5)',
                    'z-index': 1
            });
        },
        resize: function (e, ui) {
            var handle_axis = $(this).data('ui-resizable').axis;
        },
        stop: function (e, ui) {
            $(this).css({
                'background': 'rgba(255,255,255,0.5)',
                    'z-index': 0
            });

            var h2_top = $('.ss-zone-object').find('h2').position().top;

            var orig_top = ui.originalPosition.top;
            var cur_top = ui.position.top;
            var new_top;

            new_top = (cur_top - orig_top);

            $('.ss-zone-object').css({
                'height': ui.size.height + 'px',
                    'top': cur_top + 'px'
            });

            $('.ss-zone-object').find('h2').css('top', (h2_top - new_top));

        }
    });