Tooltip in jQuery
by Faisal Khan Janjua
December 06, 2018
HTML
<div class="dashboard">
<h4>Lines Status <span class="pull-right info_icon">i<span>This is my text</span></span></h4>
</div>
<div class="dashboard">
<h4>Lines Status <span class="pull-right info_icon">i<span>This is my text</span></span></h4>
</div>
<div class="dashboard">
<h4>Lines Status <span class="pull-right info_icon">i<span>This is my text</span></span></h4>
</div>
SCSS
.dashboard {
padding:4px 10px;
position: relative;
margin:30px;
background: #eee;
border-radius: 4px;
min-width: 200px;
&:after {
content:'';
display:table;
clear:both;
}
}
.info_icon {
cursor:pointer;
float:right;
width: 18px;
height: 18px;
padding: 1px;
color: #CCCCCC;
display: block;
line-height: 1;
font-size: 12px;
text-align: center;
border-radius: 50%;
user-select:none;
border: 2px solid #cccc;
& span {
right: 0;
top: -5px;
width: auto;
padding: 6px;
display: none;
white-space: pre;
background: #000;
border-radius: 4px;
position: absolute;
user-select:none;
animation-name: tipUp;
-webkit-animation-name: tipUp;
animation-duration: 200ms;
-webkit-animation-duration: 200ms;
animation-timing-function: ease;
-webkit-animation-timing-function: ease;
visibility: visible !important;
&:after{
top: 100%;
right: 22%;
content: "";
position: absolute;
border-width: 5px;
border-style: solid;
border-color: black transparent transparent transparent;
}
}
&.active span {
display: block;
}
}
@keyframes tipUp {
0% { transform: translateY(100%); }
50%{ transform: translateY(-8%); }
65%{ transform: translateY(4%); }
/* 80%{ transform: translateY(-4%); } */
95%{ transform: translateY(2%); }
100% { transform: translateY(0%); }
}
@-webkit-keyframes tipUp {
0% { -webkit-transform: translateY(100%); }
50%{ -webkit-transform: translateY(-8%); }
65%{ -webkit-transform: translateY(4%); }
/* 80%{ -webkit-transform: translateY(-4%); } */
95%{ -webkit-transform: translateY(2%); }
100% { -webkit-transform: translateY(0%); }
}
JavaScript
jQuery(document).ready(function(){
jQuery(document).on('click', '.dashboard .info_icon', function() {
jQuery('.dashboard .info_icon').not(jQuery(this)).removeClass('active');
jQuery(this).toggleClass('active');
});
jQuery(document).mouseup(function(e){
var container = jQuery(".dashboard .info_icon");
if (!container.is(e.target) && container.has(e.target).length === 0){
container.removeClass('active');
}
});
});