为什么下拉元素在悬停在每一行上时会向左移动?

Why the dropdown elements gets shifted to left on hovering on each row?

我使用 jQueryUI 的 selectMenu 小部件创建了一个下拉列表,如下图所示:

下面给出了 HTML 代码。

<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Selectmenu - Custom Rendering</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>

<body>
  <label for="options">Select an Option:</label>
    <select id="options">
  <optgroup label="PREFERRED OPTIONS">
    <option data-short="L" data-price="[=10=].00">Standard Screw Adjustment</option>
    <option data-short="K" data-price="[=10=].00">Standard Screw Adjustment</option>
  </optgroup>
  <optgroup label="STANDARD OPTIONS">
    <option data-short="C" data-price=".00" >Tamper Resistant - Factory Set</option>
    <option data-short="K" data-price=".00" >Handknob</option>
  </optgroup>
  <optgroup label="ADDITIONAL OPTIONS">
    <option data-short="F" data-price="-.00">Hex Head Screw with Locknut</option>
    <option data-short="G" data-price=".00">Hex Head Screw with Locknut</option>
    <option data-short="H" data-price=".00" >Hex Head Screw with Locknut</option>
  </optgroup>
</select>

</body>

</html>

这是此小部件的 CSS。

.ui-selectmenu-category {
  color: #5f5f5f;
  padding: 0.5em 0.25em;
}

.ui-menu-item .ui-menu-item-wrapper {
  display: inline-block;
  padding: 1em 2px;
}

.ui-selectmenu-menu .ui-menu.customicons .ui-menu-item-wrapper {
  padding: 0.5em 0 0.5em 3em;
}
.ui-selectmenu-menu .ui-menu.customicons .ui-menu-item .ui-icon {
  height: 24px;
  width: 14px;
  top: 0.1em;
}


.ui-menu-item .ui-menu-item-wrapper.ui-state-active {
  border-width: 1px 0px 1px 0px;
  border-color: #cccccc;
  background-color: #e4ebf1;
  color: #000;
}

.ui-menu-item .ui-menu-item-wrapper.ui-state-active.short {
  color: #2e6d99;
}

.ui-menu-item div.ui-menu-item-wrapper {
  width: 290px;
}

.ui-menu-item .short {
  color: #2e6d99;
  font-weight: strong;
  width: 30px;
  padding-left: 0.5em;

}

.ui-menu-item .price {
  font-weight: strong;
  width: 75px;
  margin-right: -6px;

}


.overflow {
  height: 200px;
}

下面给出了 Javascript 代码。

$(function() {
  $.widget("custom.mySelectMenu", $.ui.selectmenu, {
    _renderMenu: function(ul, items) {
      var that = this,
        currentCategory = "";
      $.each(items, function(index, item) {
        var li, name, short, price;
        if (item.optgroup != currentCategory) {
          ul.append(
            "<li class='ui-selectmenu-category'>" +
              item.optgroup +
              "</li>"
          );
          currentCategory = item.optgroup;
        }
        li = that._renderItemData(ul, item);
        console.log(ul);
        name = li.text();
        short = item.element.data("short");
        price = item.element.data("price");
        console.log(li, short, price);
        li.prepend(
          $("<span>", {
            class: "short"
          }).html(short)
        );

        li.append(
          $("<span>", {
            class: "price"
          }).html(price)
        );
        if (item.optgroup) {
          li.attr("aria-label", item.optgroup + " : " + item.label);
        }
      });
    }
  });

  $("#options").mySelectMenu({
    width: 300
  });
  $("#options")
    .mySelectMenu("menuWidget")
    .addClass("overflow");
});

现在,我的问题是,每当我将光标悬停在任何行上时,该悬停行的元素就会在其右侧被选中。但我希望我的元素固定在该位置。我知道问题与 .ui-state-active class 有关。我已经尝试了很多我知道的技巧,但 none 对我有用。我是 CSS 的初学者。请 help.Mentioned 下面是描述这些元素如何移动的图像。

任何人都可以提供有关如何解决此问题的任何想法吗?

在您的 css 文件中,您需要在 .ui-state-active class 中添加边距 0。悬停导致出现 -1 的边距。之后,当你上下悬停时,你需要阻止它跳跃。为此,您可以删除所有边框属性,并添加 outline: 1px solid #ccc 属性。将您当前的 .ui-menu-item .ui-menu-item-wrapper.ui-state-active 块替换为此处的块应该可以解决您的问题(至少他们在您的 codepen 中做到了)

.ui-menu-item .ui-menu-item-wrapper.ui-state-active {
  //border-width: 1px 0px 1px 0px;
  margin: 0;
  //border-color: #cccccc;
  border: none;
  outline: 1px solid #ccc;
  background-color: #e4ebf1;
  color: #000;
}

要为更高分辨率固定宽度,您可以将 min-width 属性 添加到 .ui-selectmenu-category

.ui-selectmenu-category {
  color: #5f5f5f;
  padding: 0.5em 0.25em;
  min-width: 290px;
}