我要做一个功能,html中有一个横向长条,内部从左到右是一张logo图片、几个字,一个展开按钮,并且右侧有一个三角图标。点击展开按钮时,横条下方出现一个文字容器,高度根据文字内容显示。文字不直接展示,而是从头到尾一个字一个字的出现,间隔100ms。写出对应的html代码和js代码
HTML代码:
<div class="horizontal-bar">
<div class="logo">
<img src="logo.png" alt="Logo">
</div>
<div class="text">
<p>这是一些文本</p>
</div>
<div class="expand-btn">
展开
<i class="triangle-icon"></i>
</div>
</div>
<div class="text-container"></div>
CSS代码:
.horizontal-bar {
display: flex;
align-items: center;
height: 50px;
background-color: #F5F5F5;
padding: 0 20px;
position: relative;
}
.logo {
margin-right: 10px;
}
.text {
margin-right: 10px;
}
.expand-btn {
display: flex;
align-items: center;
justify-content: center;
color: #333;
cursor: pointer;
}
.triangle-icon {
display: inline-block;
width: 0;
height: 0;
border-style: solid;
border-width: 5px 0 5px 8.7px;
border-color: transparent transparent transparent #333;
margin-left: 5px;
}
.text-container {
display: none;
padding: 10px;
background-color: #FFF;
position: absolute;
left: 0;
bottom: -1px;
width: 100%;
z-index: 999;
}
JavaScript代码:
const expandBtn = document.querySelector('.expand-btn');
const textContainer = document.querySelector('.text-container');
const text = document.querySelector('.text p').innerText;
let index = 0;
expandBtn.addEventListener('click', () => {
textContainer.style.display = 'block';
const interval = setInterval(() => {
if (index < text.length) {
textContainer.innerText += text.charAt(index);
index++;
} else {
clearInterval(interval);
}
}, 100);
});
我要做一个功能,html中有一个横向长条,内部从左到右是一张logo图片、几个字,一个展开按钮,并且右侧有一个三角图标。点击展开按钮时,横条下方出现一个文字容器,高度根据文字内容显示。文字不直接展示,而是从头到尾一个字一个字的出现,间隔100ms。写出对应的html代码和js代码
HTML代码:
<div class="horizontal-bar">
<div class="logo">
<img src="logo.png" alt="Logo">
</div>
<div class="text">
<p>这是一些文本</p>
</div>
<div class="expand-btn">
展开
<i class="triangle-icon"></i>
</div>
</div>
<div class="text-container"></div>
CSS代码:
.horizontal-bar {
display: flex;
align-items: center;
height: 50px;
background-color: #F5F5F5;
padding: 0 20px;
position: relative;
}
.logo {
margin-right: 10px;
}
.text {
margin-right: 10px;
}
.expand-btn {
display: flex;
align-items: center;
justify-content: center;
color: #333;
cursor: pointer;
}
.triangle-icon {
display: inline-block;
width: 0;
height: 0;
border-style: solid;
border-width: 5px 0 5px 8.7px;
border-color: transparent transparent transparent #333;
margin-left: 5px;
}
.text-container {
display: none;
padding: 10px;
background-color: #FFF;
position: absolute;
left: 0;
bottom: -1px;
width: 100%;
z-index: 999;
}
JavaScript代码:
const expandBtn = document.querySelector('.expand-btn');
const textContainer = document.querySelector('.text-container');
const text = document.querySelector('.text p').innerText;
let index = 0;
expandBtn.addEventListener('click', () => {
textContainer.style.display = 'block';
const interval = setInterval(() => {
if (index < text.length) {
textContainer.innerText += text.charAt(index);
index++;
} else {
clearInterval(interval);
}
}, 100);
});
js代码使用最基本的js,不要用额外的库,不要用let