项目场景:
如图,需求方要求input框的日期选择器修改成如下格式,默认样式如底部
解决方案:
以下为修改步骤:
- 替换日期选择器icon,并将其移到左边
input[type="date"] {
display: flex;
padding-left: 10px;
padding-right: 550px;
font-size: 16px;
line-height: 24px;
}
input[type="date"]::-webkit-calendar-picker-indicator {
position: relative;
left: -120px;
background-image: url("../assets/img/submitInfo/日期选择器.png");
}
- 隐藏年月日,显示placeholder
input[type="date"]:before {
content: attr(placeholder);
padding-left: 20px;
}
可以看到此时还有如下问题,placeholder成功显示,但是此时默认的年月日还在显示
- 当选择日期后,placeholder隐藏,默认日期格式显示
input[type="date"].selected:before {
content: "" !important;
}
let itemRightDate = document.querySelector("input[type=date][name=date]");
itemRightDate.addEventListener("input", function (e) {
let {
target: { value },
} = e;
if (value.length > 0) {
e.target.classList.add("selected");
} else {
e.target.classList.remove("selected");
}
});
- 针对placeholder显示时默认的年月日显示且被截断
input[type="date"]::-webkit-datetime-edit-year-field {
position: relative;
left: 10px;
}
5. placeholder样式和设计稿不符
input[type="date"]:before {
content: attr(placeholder);
padding-left: 20px;
color: var(--gray-rgba-116116116);
font-size: 16px;
line-height: 22px;
}
可以看到已经满足要求了,录区域视频再转gif太麻烦了,截图也能表现出来~
- 最终完整版样式
input[type="date"] {
display: flex;
padding-left: 10px;
padding-right: 550px;
font-size: 16px;
line-height: 24px;
}
input[type="date"]:before {
content: attr(placeholder);
padding-left: 20px;
color: var(--gray-rgba-116116116);
font-size: 16px;
line-height: 22px;
}
input[type="date"].selected:before {
content: "" !important;
}
input[type="date"]::-webkit-datetime-edit-year-field {
position: relative;
left: 10px;
}
input[type="date"]::-webkit-calendar-picker-indicator {
position: relative;
left: -120px;
background-image: url("../assets/img/submitInfo/日期选择器.png");
}