有人可以建议只在悬停时显示非活动标签文本的配方吗? 我通常使用图标+文字作为下面的标签:
{{1}}
使非活动标签仅显示图标,而文字显示在悬停
上答案 0 :(得分:2)
您可以使用label
项目包装文本,并最初通过CSS隐藏它。
然后,在鼠标指针进入时,在显示label
的菜单项上绑定hover
函数,并在鼠标指针离开时隐藏它:
$(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>
&#13;
答案 1 :(得分:2)
如果您将文本包装在一个范围内,那么您可以使用单个CSS声明处理此问题
例如,如果您使用类.tab-title
包装span,则:
.ui-tab:not(.ui-state-active):not(:hover) .tab-title {
display: none; /*or opacity: 0; and animate away */
}
$(function() {
$("#tabs").tabs();
});
&#13;
.ui-tab:not(.ui-state-active):not(:hover) .tab-title {
display: none;
/*or opacity: 0; */
}
&#13;
<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>
&#13;
答案 2 :(得分:0)
至于滑出视野,我使用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%;
}
&#13;
<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>
&#13;
我在这里使用了固定大小的标签,但您可以使用百分比值轻松地将其更改为灵活。