已合作成功的客户
遍及全国及海外
中国
杭州,绍兴,宁波,湖州,嘉兴,温州,台州,上海,北京,南京,苏州,常州,无锡,长沙,青岛,江西,台湾,南宁,海南,成都,哈尔滨,深圳,香港,沈阳 ...
海外
美国,加拿大,丹麦,澳大利亚,新加坡,法国,智利,日本,英国 ...
合作咨询
4001-355-360
placeholder-shown 配合 focus-within 实现 input 输入交互
作者:admin
来源:lanyunwork
时间:2025-12-23
分享到:
使用 :placeholder-shown && :focus-within 实现 input 输入交互。
:focus-within: 是一个CSS 伪类 ,表示一个元素获得焦点,或,该元素的后代元素获得焦点。换句话说,元素自身或者它的某个后代匹配:focus伪类:placeholder-shown: CSS 伪类 在 <input> 或 <textarea> 元素显示 placeholder text 时生效
HTML:
<div class="g-container">
<input type="text" placeholder="输入你想查询的内容" class="g_input_search" >
<button type="button" class="g_button_search">GO</button>
</div>
SCSS:
.g-container {
position: relative;
margin: 100px auto;
display: flex;
flex-wrap: wrap;
width: 500px;
height: 60px;
overflow: hidden;
transition: 0.3s;
& > * {
border: none;
outline: none;
}
.g_input_search {
padding: 0 15px;
height: 100%;
width: 100%;
border: 1px solid #ddd;
font-size: 18px;
box-sizing: border-box;
&:not(:placeholder-shown) {
border: 1px solid #03a9f4;
+ .g_button_search {
opacity: 1;
}
}
&:placeholder-shown {
+ .g_button_search {
opacity: 0;
}
}
}
.g_button_search {
background: #03a9f4;
color: #feffd4;
font-size: 15px;
cursor: pointer;
width: 100px;
height: calc(100% - 20px);
transition: 0.3s;
position: absolute;
right: 10px;
top: 10px;
}
&:focus-within {
transform: translateY(-4px);
border-color: #bbb;
box-shadow: 4px 4px 10px 2px #ddd;
}
}
具体效果:
默认状态:
点击状态:
输入文本状态:
获取方案