Skip to content

Instantly share code, notes, and snippets.

@Ankhena
Created April 9, 2021 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ankhena/ada4232ccaf70c7df019ab7a13cd1b0d to your computer and use it in GitHub Desktop.
Save Ankhena/ada4232ccaf70c7df019ab7a13cd1b0d to your computer and use it in GitHub Desktop.
Tooltip
<div class="tooltip">
<button class="tooltip__btn" type="button" aria-label="Подробности"></button>
<div class="tooltip__text">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium, deserunt, distinctio dolorum eaque eius eveniet ipsam mollitia nostrum pariatur quibusdam quisquam quo quod reprehenderit velit voluptas? Magnam sequi totam voluptate!
</div>
</div>
// tooltip
let tooltip = document.querySelectorAll('.tooltip');
let tooltipBtn = document.querySelectorAll('.tooltip__btn');
tooltipBtn.forEach(btn => btn.addEventListener('click', (e) => {
tooltip.forEach(tooltip => tooltip.classList.remove('tooltip--open'));
e.target.parentElement.classList.toggle('tooltip--open');
}))
// закроем по клику мимо
document.addEventListener('mouseup', function (evt) {
if (evt.target.closest('.tooltip') === null) {
tooltip.forEach(tooltip => tooltip.classList.remove('tooltip--open'));
}
});
// end tooltip
.tooltip {
position: relative;
display: inline-block;
}
.tooltip--open {
.tooltip__text {
position: absolute;
top: calc(100% + 12px);
left: 50%;
transform: translateX(-50%);
display: block;
width: max-content;
max-width: 250px;
padding: 12px;
border-radius: 12px;
font-size: 12px;
box-shadow: 0 0 16px 0 #C1CDDF;
background: white;
z-index: 2;
}
}
.tooltip__btn {
@extend .btn;
@extend .btn--only-icon;
@extend .btn--question;
}
.tooltip__text {
display: none;
}
///////
.btn {
display: inline-block;
padding: 0.6em 1em;
border: none;
border-radius: 4px;
font-size: 18px;
font-family: inherit;
font-weight: inherit;
line-height: normal;
text-decoration: none;
text-align: center;
cursor: pointer;
&:focus {
outline: none;
opacity: 0.8;
}
}
.btn--only-icon {
padding: 0;
background-color: transparent;
border-color: transparent;
}
.btn--question {
display: inline-flex;
justify-content: center;
align-items: center;
width: 1em;
height: 1em;
border: 1.5px solid #dbdbdb;
border-radius: 50%;
color: #dbdbdb;
&::before {
content: "?";
font-size: 0.6em;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment