JSFiddle - React, Tailwind, and code Playground
by sazakharov
HTML
<div class="wrapper">
<div class="wrapper-inner">
<div class="item">
<span>1</span>
<button>клик</button>
</div>
<div class="item">
<span>2</span>
<button>клик</button>
</div>
</div>
</div>
<div class="notice"></div>
CSS
* {
box-sizing: border-box;
}
.wrapper {
text-align: center;
}
.wrapper-inner {
display: flex;
margin-bottom: 20px;
}
.item {
width: 120px;
}
button {
width: 100px;
height: 30px;
margin-top: 15px;
margin-bottom: 20px;
line-height: 10px;
border: 1px solid #000;
border-radius: 3px;
background-color: transparent;
font-size: 15px;
transition: 0.4s ease;
cursor: pointer;
}
button:focus {
outline: 0;
}
button:hover {
background-color: #000;
color: #fff;
}
span {
display: flex;
justify-content: center;
align-items: center;
width: 100px;
height: 100px;
margin-right: auto;
margin-bottom: 10px;
margin-left: auto;
border: 1px solid #000;
}
.notice {
text-align: left;
}
.notice__info {
display: flex;
align-items: center;
margin-right: 10px;
margin-bottom: 20px;
margin-left: 10px;
}
.notice__info-name, .notice__delete {
display: flex;
justify-content: center;
align-items: center;
height: 40px;
}
.notice__info-name {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 250px;
border: 1px solid #000;
margin-right: 10px;
}
.notice__timer {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 3px;
background: #e74c3c;
}
.notice__delete {
width: 40px;
border: 1px solid #000;
transition: 0.4s ease;
cursor: pointer;
}
.notice__delete:hover {
background-color: #000;
color: #fff;
}
JavaScript
const items = [];
function startTimer(name) {
$timer = $(`.notice__timer[data-name="${name}"]`);
$timer.stop();
$timer.css("width", "100%");
$timer.animate(
{width: 0},
{duration: 10000,
complete: function() {
$(this).closest('.notice__info').remove();
delete items[name];
console.log(`Удален ${name}`);
}
}
);
}
$('button').on('click', function() {
let name = $(this).prev('span').text();
if (items[name]) {
// повторно
startTimer(name);
} else {
// вновь
items[name] = true;
var out = `
<div class="notice__info">
<div class="notice__info-name">
${name}
<div class="notice__timer" data-name="${name}"></div>
</div>
<div class="notice__delete">X</div>
</div>`;
$('.notice').append(out);
startTimer(name);
}
});
$(document).on('click', '.notice__delete', function() {
$(this).parent().remove();
delete items[$(this).siblings('.notice__info-name').text()];
});