JQuery UI 选项卡:非活动选项卡的文本仅在悬停时显示

JQuery UI tabs: inactive tab's text shown only on hover

有人可以推荐一个仅在悬停时显示非活动选项卡文本的方法吗? 我通常使用图标 + 文本作为标签,如下所示:

<a href="#myLeaves"><span class="ui-icon ui-icon-home"></span>Leaves</a></li>

以便非活动选项卡仅显示图标,而悬停时显示文本

您可以用 label 项目包裹您的文本,并最初通过 CSS 隐藏它。

然后,在你的菜单项上绑定一个hover函数,当鼠标指针进入时显示label,当鼠标指针离开时隐藏它:

$(function() {
  $("#menu a").hover(function() {
    $(this).find("label").show();
  }, function() {
    if (!$(this).hasClass("active")) {
      $(this).find("label").hide();
    }
  });
});
#menu a:not(.active) label {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0-rc.2/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.0-rc.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<div id="menu">
  <a href="#home" class="active"><span class="ui-icon ui-icon-home"></span><label>home</label></a>
  </li>
  <a href="#flag"><span class="ui-icon ui-icon-flag"></span><label>flag</label></a>
  </li>
  <a href="#search"><span class="ui-icon ui-icon-search"></span><label>search</label></a>
  </li>
</div>

如果将文本包装在一个范围内,则可以使用单个 CSS 声明来处理。
例如,如果您用 class .tab-title 包装跨度,则:

.ui-tab:not(.ui-state-active):not(:hover) .tab-title {
  display: none; /*or opacity: 0; and animate away */
}

$(function() {
  $("#tabs").tabs();
});
.ui-tab:not(.ui-state-active):not(:hover) .tab-title {
  display: none;
  /*or opacity: 0; */
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0-rc.2/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.0-rc.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<div id="tabs">
  <ul>
    <li><a href="#tabs-1"><span class="ui-icon ui-icon-home"></span><span class="tab-title">Nunc tincidunt</span></a>
    </li>
    <li><a href="#tabs-2"><span class="ui-icon ui-icon-flag"></span><span class="tab-title">Proin dolor</span></a>
    </li>
    <li><a href="#tabs-3"><span class="ui-icon ui-icon-search"></span><span class="tab-title">Aenean lacinia</span></a>
    </li>
  </ul>
  <div id="tabs-1">
    <p>Proin elit arcu, rutrum commodo, vehicula tempus, commodo a, risus. Curabitur nec arcu. Donec sollicitudin mi sit amet mauris. Nam elementum quam ullamcorper ante. Etiam aliquet massa et lorem. Mauris dapibus lacus auctor risus. Aenean tempor ullamcorper
      leo. Vivamus sed magna quis ligula eleifend adipiscing. Duis orci. Aliquam sodales tortor vitae ipsum. Aliquam nulla. Duis aliquam molestie erat. Ut et mauris vel pede varius sollicitudin. Sed ut dolor nec orci tincidunt interdum. Phasellus ipsum.
      Nunc tristique tempus lectus.</p>
  </div>
  <div id="tabs-2">
    <p>Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean
      aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat.
      Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.</p>
  </div>
  <div id="tabs-3">
    <p>Mauris eleifend est et turpis. Duis id erat. Suspendisse potenti. Aliquam vulputate, pede vel vehicula accumsan, mi neque rutrum erat, eu congue orci lorem eget lorem. Vestibulum non ante. Class aptent taciti sociosqu ad litora torquent per conubia
      nostra, per inceptos himenaeos. Fusce sodales. Quisque eu urna vel enim commodo pellentesque. Praesent eu risus hendrerit ligula tempus pretium. Curabitur lorem enim, pretium nec, feugiat nec, luctus a, lacus.</p>
    <p>Duis cursus. Maecenas ligula eros, blandit nec, pharetra at, semper at, magna. Nullam ac lacus. Nulla facilisi. Praesent viverra justo vitae neque. Praesent blandit adipiscing velit. Suspendisse potenti. Donec mattis, pede vel pharetra blandit, magna
      ligula faucibus eros, id euismod lacus dolor eget odio. Nam scelerisque. Donec non libero sed nulla mattis commodo. Ut sagittis. Donec nisi lectus, feugiat porttitor, tempor ac, tempor vitae, pede. Aenean vehicula velit eu tellus interdum rutrum.
      Maecenas commodo. Pellentesque nec elit. Fusce in lacus. Vivamus a libero vitae lectus hendrerit hendrerit.</p>
  </div>
</div>

至于滑出视野,我会使用 overflow: hidden 的灵​​活宽度,如下所示:

* {
  box-sizing: border-box;
}
a {
  display: inline-block;
  border: 1px solid gray;
  position: relative;
  padding: 10px 10px 10px 28px;
  white-space: nowrap;
}
a > label {
  display: inline-block;
  overflow: hidden;
  width: 0;
  transition: width 0.5s;
}
a > span.ui-icon {
  position: absolute;
  margin-top: 0;
  left: 2px;
}

a:hover > label, a.active > label {
  width: 100%;
}
<link href="https://code.jquery.com/ui/1.12.0-rc.2/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0-rc.2/jquery-ui.min.js"></script>


<a class="active" href="#myLeaves"><span class="ui-icon ui-icon-home"></span><label>Leaves</label></a>
<a href="#flag"><span class="ui-icon ui-icon-flag"></span><label>long label</label></a>
<a href="#search"><span class="ui-icon ui-icon-search"></span><label>another long label</label></a>

我在这里使用了固定大小的标签,但您可以使用百分比值轻松地将其更改为灵活。