如何使工具提示可见点击一个图标和隐藏时,点击相同的图标?



我想在单击信息图标时在三个不同的地方显示工具提示。并通过再次单击图标禁用工具提示。如何通过使用HTML和jQuery实现这一点?在下面的例子中,我点击添加,它会显示这3个工具提示,然后再次点击添加隐藏它们。

EDIT: Code that I tried

<div id="info-info">
<i class="fa fa-info-circle" ></i>
<div class="tip" data-toggle="tooltip" data-trigger="click">Info 1</div>
<div class="tip1">Info 2</div>
<div class="tip2">Info 3</div>
</div>

JS

$(".info").click(function(){
var left = $("i", this).offset().left + ($("i", this).width() / 2) - ($(".tip", this).width() / 2);
$(".tip", this).toggle();
$(".tip1", this).toggle()
$(".tip2", this).toggle();
});

CSS

#info-info {
float: left;
height: 50px;
padding: 9px 0px 0px 10px;
text-align: left;
letter-spacing: 0;
color: #FFFFFF;
opacity: 1;
text-decoration: none;
font-size: 18px;
}
#info-info .tip {
display: none;
position: absolute;
border: 1px solid #333;
border-radius: 2px;
padding-left: 3px;
margin-left: 300px;
left: 900px;
color: #000000;
}
#info-info .tip1 {
display: none;
position: absolute;
border: 1px solid #333;
border-radius: 2px;
padding-left: 3px;
margin-left: 300px;
left: 1px;
color: #000000;
}
#info-info .tip2 {
display: none;
position: absolute;
border: 1px solid #333;
border-radius: 2px;
padding-left: 3px;
margin-left: 300px;
left: 10px;
color: #000000;
}

当某些元素被点击(或悬停)时,你会想要隐藏/显示一组absolutely定位元素

function toggleTips () {
$(".tooltip").each(function() {
$(this).toggleClass("hidden")
})
}
$("#toggle-tips").on("click", function() {
toggleTips();
})
body {
font-family: Sans-serif
}
table {
border-collapse: collapse;
}
thead {
background: lightgray;
}
table, th, td {
border: 1px solid gray;
}
th, td {
position: relative;
padding: 10px
}
.tooltip {
box-shadow: 3px 3px 10px 0 rgba(0,0,0,.5);
padding: 10px;
position: absolute;
background: white;
white-space: nowrap;
font-weight: normal;
z-index: 2;
}
.hidden {
display: none;
}
button {
margin-top: 10px;
background: lightgray;
padding: 10px;
border: 0;
border-radius: 10px;
}
button:hover {
background: gray;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<thead>
<th>First Name <span class="tooltip hidden">User's first name</span></th>
<th>Last Name</th>
<th>Phone #</th>
</thead>
<tbody>
<td>Jane</td>
<td>Doe</td>
<td>0123456789<span class="tooltip hidden">User's phone number</span></td>
</tbody>
</table>
<button id="toggle-tips">Toggle Tips</button>

想要这样…

$(function(){

$(".info").click(function(){
var left = $("img",this).offset().left + ($("img",this).width()/2) - ($(".tip", this).width()/2);
$(".tip", this).toggle().css({"left":left});

});

});
.info .tip{ 
display: none;
position: absolute;
border: 1px solid #333;
border-radius: 2px;
padding: 3px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="info"><img src="http://blog.flattr.net/wp-content/uploads/2011/09/stackoverflow.png" />

<div class="tip">tool tip text</div>
</div>

来源- http://jsfiddle.net/NPXaf/

相关内容

  • 没有找到相关文章

最新更新