jquery 位置发生碰撞。检测到碰撞时添加类

标签 jquery jquery-ui position collision

我使用 jquery-ui 的自动完成框进行碰撞,以在输入上方或下方显示自动完成。我想要的是,如果检测到碰撞,则向自动完成元素添加一个类,这样我就可以自定义CSS(如果它位于上方与下方)。这看起来应该很简单,但我似乎想不出任何方法来实现它。

最佳答案

更好的解决方案是在回调中使用第二个参数。 来自 jQueryUi 文档:

The second provides feedback about the position and dimensions of both elements, as well as calculations to their relative position. Both target and element have these properties: element, left, top, width, height. In addition, there's horizontal, vertical and important, giving you twelve potential directions like { horizontal: "center", vertical: "left", important: "horizontal" }.

正如它所说,第二个参数中有一个水平和一个垂直属性,您可以使用它们来确定元素是否翻转。所以你可以写这样的东西:

$("#someId").dialog({
      position: {
      my: 'left top',
      at: 'right top',
      collision: 'flip flip',
      of: $('#' + someElementID),
      using: function (obj,info) {
          if (info.vertical != "top") {
              $(this).addClass("flipped");
          } else {
              $(this).removeClass("flipped");
          }
          if (info.horizontal != "left") {
              $(this).addClass("flipped");
          } else {
              $(this).removeClass("flipped");
          }
          $(this).css({
            left: obj.left + 'px',
            top: obj.top + 'px'
          });
      }
},

关于jquery 位置发生碰撞。检测到碰撞时添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13299718/

相关文章:

jquery - 遍历父级并使用 ($this) 转到其所有子元素

javascript - 在 Meteor 0.8.0 (Blaze) 中实现可选择的项目列表

jquery-ui - 移动 joomla 和 jquery ui 冲突

jquery - 使用 jQuery DatePicker 动态更改 minDate 和 maxDate

php - HTML 表单文本区域。使用 PHP 将数据发送到 MySQL。通知: Undefined index

jquery - 如何在单击 div 后保持其悬停状态?

html - 为什么我的网站在 Safari 中看起来不一样,我该如何解决?

WPF:如何在代码中定位鼠标光标?

css - 在投资组合图库图像上粘贴 'New!' 图标的最简单和最正确的方法

javascript - 在外部点击时禁用关闭模式