【问题标题】:jQuery UI Position to set "right" and "bottom" instead of "left" and "top"jQuery UI 位置设置“右”和“下”而不是“左”和“上”
【发布时间】:2012-02-29 15:06:18
【问题描述】:

好的,这是我的问题。 使用 jQuery UI 位置。可以相对于屏幕上的另一个元素定位一个元素。它在被定位的元素上设置 left 和 top css 属性,将其定位在屏幕上。

我想要做的不是设置left和top,而是设置right和bottom,这样如果定位的元素增长或缩小,它会在正确的方向增长/缩小。

让我详细介绍一下。 好的,我想要的是如果一个元素位于它的右边,那么我想设置rightcss 属性而不是left,如果一个元素位于它的底部,那么我想设置bottom而不是top。我可以使用 jQuery UI Position 的 using 属性来做到这一点,但我遇到了碰撞检测问题。如果碰撞检测设置为flip 并且元素被翻转,我想检测这一点并确定是否需要设置right 而不是leftbottom 而不是top。查看下面的代码,以更好地了解我想要做什么。

$('#somediv').position({
    my: 'right bottom',
    at: 'right top',
    of: $('#someotherdiv'),
    offset: '0 5',
    collision: 'flip flip',
    using: function(pos) {

      // Figure out the right and bottom css properties 
      var right = $(window).width() - pos.left - $(this).outerWidth(true);
      var bottom = $(window).height() - pos.top - $(this).outerHeight(true);

      // Position the element so that right and bottom are set.
      $(this).css({left: '', right: right, top: '', bottom: bottom});  
    }
});

这很好用,除非 div 从碰撞检测中翻转。如果它被水平翻转,我想设置left而不是right,如果它垂直翻转我想设置top而不是bottom

理想的解决方案是如果有办法(在using 函数中)判断元素是否被翻转以及朝什么方向翻转。那么,有没有人想知道一个元素是否被碰撞检测翻转了?

【问题讨论】:

  • 有人吗?我把问题说清楚了吗?

标签: javascript jquery jquery-ui


【解决方案1】:

好的,想通了....这是我试图解释我如何使它工作的尝试。 您需要做的是在using 函数中再次调用位置。在没有碰撞检测的情况下调用一次,在有碰撞检测的情况下调用一次。如果位置发生变化,则将其翻转。这是一些带有 cmets 的示例代码。

$('#somediv').position({
    my: 'right bottom',
    at: 'right top',
    of: $('#someotherdiv'),
    offset: '0 5',
    collision: 'flip flip',
    using: function (pos1) {

        // OK, we got a position once inside the pos1 variable,
        // let's position it again this time without collision detection.
        $(this).position({
            my: 'right bottom',
            at: 'right top',
            of: $('#someotherdiv'),
            offset: '0 5',
            collision: 'none none',
            using: function(pos2) {
                var hpos = 'right';
                var vpos = 'bottom';

                // Check to see if it was flipped horizontal
                if (pos1.left != pos2.left) {
                    /* It was flipped horizontally so position is now
                       my: 'left bottom',
                       at: 'left top'
                    */
                    hpos = 'left';
                }

                // Check to see if it was flipped vertical
                if (pos1.top != pos2.top) {
                    /* It was flipped vertically */
                    vpos = 'top';
                }

                // Figure out the right and bottom css properties 
                var right = $(window).width() - pos1.left - $(this).outerWidth(true);
                var bottom = $(window).height() - pos1.top - $(this).outerHeight(true);

                // Set the horizontal position
                if (hpos == 'right') {
                    $(this).css({left: '', right: right});
                } else {
                    $(this).css({left: pos1.left, right: ''});
                }

                // Set the vertical position
                if (vpos == 'bottom') { 
                    $(this).css({top: '', bottom: bottom});
                } else {
                    $(this).css({top: pos1.top, bottom: ''});
                }
            }
        });
    }
});

如果有人有更有效的想法,请告诉我。谢谢。

【讨论】:

    猜你喜欢
    • 2012-05-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 2014-10-26
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多