效果:输入栏的字体比较小,当你输入的时候上面弹出来一个大盒子显示你输入的内容
原理:下面是一个文本框,上面是一个盒子(带三角),定位定在上面,当键盘事件触发,上面的盒子就显示,且传入输入的值
HTML:
<div class="search">
<div class="con">
</div>
<input type="text" placeholder="请输入您要查询的快递单号">
</div>
CSS:(主要是制作小三角部分,如果不懂可以看上一篇笔记)
.con {
display: none;
position: relative;
width: 200px;
height: 20px;
font-size: 18px;
box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
padding: 5px 0;
line-height: 20px;
color: #333;
}
.con::before {
content: '';
width: 0;
height: 0;
position: absolute;
top: 28px;
left: 18px;
border: 8px solid #000;
border-style: solid dashed dashed;
border-color: #fff transparent transparent;
}
JS:
var con = document.querySelector('.con');
var input = document.querySelector('input');
//如果用keydown在文本框里面是先触发事件,然后在传入值,所以会少一个
// 如果用keypress就不能使用功能键,上面的内容就不能删除了
input.addEventListener('keyup', function () {
// alert(this.value);
if (this.value == '') {
con.style.display = 'none';
} else {
con.style.display = 'block';
con.innerHTML = this.value;
}
})
// 当我们失去焦点, 盒子就隐藏
input.addEventListener('blur', function () {
con.style.display = 'none';
})
// 获取焦点且输入内容盒子显示
input.addEventListener('focus', function () {
if (this.value !== '') {
con.style.display = 'block';
}
})