当前位置:网站首页>【js组件】自定义select
【js组件】自定义select
2022-07-06 23:26:00 【KatyLight】
单个HTML页面直接运行。
下面是代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
body{
background-color: black;
color: white;
}
.KatyLight-select {
position: relative;
width: 200px;
display: inline-block;
text-align: center;
user-select: none;
color: #3b5b65;
font-size: .8rem;
}
.KatyLight-select > .title {
padding: .5rem 1rem;
background-color: transparent;
display: block;
box-shadow: inset 0 0 0 2px #96e4ff;
color: #96e4ff;
}
.KatyLight-select > .title:empty::after{
content: '请选择';
color: #3b5b65;
}
.KatyLight-select > .KatyLight-select-list {
display: none;
}
.KatyLight-select.active > .KatyLight-select-list {
display: block;
}
.KatyLight-select-list > ul {
list-style: none;
margin: 0;
position: absolute;
top:.5rem;
padding-inline-start: 0;
left: 0;
width: 100%;
box-shadow: inset 0 0 0 2px #96e4ff;
}
.KatyLight-select-list > ul > li {
padding:.5rem 0;
}
.KatyLight-select-list {
position: relative;
}
.KatyLight-select-list > ul > li:hover{
color: #96e4ff;
}
.KatyLight-select-list > ul > li.active {
color: #96e4ff;
background-color: #96e4ff22;
}
.KatyLight-select > .del {
position: absolute;
right: 1rem;
top: .5rem;
z-index: 22;
height: 2rem;
width: 2rem;
}
.KatyLight-select > .del.active {
display: block;
}
.KatyLight-select > .del {
display: none;
color: white;
}
.KatyLight-select > .del::after {
content: 'x';
}
</style>
</head>
<script>
class KatyLightSelect {
constructor(params) {
this.target = params.target
this.data = params.data
this.title = this.target.querySelector('.title')
this.del = this.target.querySelector('.del')
this.ul = this.target.querySelector('ul')
this.active = false
this.cIndex = -1
this.outClickZ = this.outClick.bind(this)
document.addEventListener('click', this.outClickZ)
this.del.dataset.del = '1'
this.title.dataset.title = '1'
this.domCreate()
}
domCreate() {
this.li = []
for (let i = 0; i < this.data.length; i++) {
let li = document.createElement('li')
li.dataset.index = i.toString()
li.dataset.option = 'K'
li.innerText = this.data[i].label
this.ul.append(li)
this.li.push(li)
}
}
titleTrigger() {
this.view(!this.active)
}
view(t) {
this.active = t
if (t) {
this.target.classList.add('active')
} else {
this.target.classList.remove('active')
}
}
getVal() {
if (this.cIndex === -1) {
return ''
} else {
return this.data[this.cIndex].value
}
}
delTrigger() {
this.choose(-1)
}
outClick(e) {
if (!this.target.contains(e.target)) {
this.view(false)
} else {
if (e.target.dataset.option) {
this.choose(parseInt(e.target.dataset.index))
this.view(false)
} else if (e.target.dataset.title) {
this.titleTrigger()
} else if (e.target.dataset.del) {
this.delTrigger()
}
}
}
choose(ii) {
if (this.cIndex !== -1) {
this.li[this.cIndex].classList.remove('active')
}
this.cIndex = ii
if (this.cIndex !== -1) {
this.li[this.cIndex].classList.add('active')
this.title.innerText = this.data[this.cIndex].label
this.del.classList.add('active')
} else {
this.title.innerText = ''
this.del.classList.remove('active')
}
}
}
window.onload = () => {
let zz = new KatyLightSelect({
target: document.querySelector('.KatyLight-select'),
data: [
{ label: 'a1', value: 'b1' },
{ label: 'a2', value: 'b2' },
{ label: 'a3', value: 'b3' },
{ label: 'a4', value: 'b4 ' },
{ label: 'a5', value: 'b5' },
{ label: 'a6', value: 'b6' }
]
})
setTimeout(() => {
console.log(zz.getVal())
}, 1500)
}
</script>
<body>
<div class="KatyLight-select">
<span class="title"></span>
<span class="del"></span>
<div class="KatyLight-select-list">
<ul>
</ul>
</div>
</div>
</body>
</html>
边栏推荐
- IMS data channel concept of 5g vonr+
- Knapsack problem unrelated to profit (depth first search)
- qt 简单布局 盒子模型 加弹簧
- 项目经理如何凭借NPDP证书逆袭?看这里
- 【opencv】图像形态学操作-opencv标记不同连通域的位置
- 做自媒体,有哪些免费下载视频剪辑素材的网站?
- Longest non descent subsequence (LIS) (dynamic programming)
- 最全常用高数公式
- Let f (x) = Σ x^n/n^2, prove that f (x) + F (1-x) + lnxln (1-x) = Σ 1/n^2
- Disk monitoring related commands
猜你喜欢
Weebly mobile website editor mobile browsing New Era
Y58. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (Sany)
Batch normalization (Standardization) processing
Analysis -- MySQL statement execution process & MySQL architecture
动态生成表格
HarmonyOS第四次培训
JHOK-ZBL1漏电继电器
Safe landing practice of software supply chain under salesforce containerized ISV scenario
Mysql database (basic)
人体传感器好不好用?怎么用?Aqara绿米、小米之间到底买哪个
随机推荐
使用知云阅读器翻译统计遗传学书籍
Let f (x) = Σ x^n/n^2, prove that f (x) + F (1-x) + lnxln (1-x) = Σ 1/n^2
ThinkPHP Association preload with
最长回文子串(动态规划)
Analysis -- MySQL statement execution process & MySQL architecture
LabVIEW在打开一个新的引用,提示内存已满
Clickhouse (03) how to install and deploy Clickhouse
记录一次压测经验总结
Timer创建定时器
Autowired注解用于List时的现象解析
《五》表格
vector和类拷贝构造函数
sublime使用技巧
[PHP SPL notes]
2. Overview of securities investment funds
How can professional people find background music materials when doing we media video clips?
数字化创新驱动指南
Array initialization of local variables
利用OPNET进行网络指定源组播(SSM)仿真的设计、配置及注意点
10 distributed databases that take you to the galaxy