前言
本文题目中虽然写有vue和react,但是并非vue和react相关知识,而是最基本的html5和css3的一些知识,之所以写vue,是因为我最近项目中用到了类似效果,我用vue相关知识实现并不雅观,用html5和css3实现,则更加完美。
项目案例
项目中有如下效果:
好多展开收起,对于这个的实现,我一开始用了vue一些比较挫的dom操作,就是父元素toggleClass一个类名,进行子元素的显示和隐藏。
由于这个方法是通用方法,项目中好多地方使用,代码大概如下:
toggleShow() {
let target = window.event.srcElement;
if (target.nodeName == "SPAN") {
target.parentNode.parentNode.classList.toggle("toggleclass");
target.classList.toggle("el-icon-arrow-right");
} else {
target.parentNode.classList.toggle("toggleclass");
target.children[0].classList.toggle("el-icon-arrow-right");
}
}
这样写,既不友好,后期又难以维护。最近重构项目的时候,把这些地方都重构了,用了今天介绍的方法!更多重构要点,请点击vue项目重构技术要点 这篇文章。
html5和css3实现展开收起
代码如下:
<details class="haorooms" open> <summary>图表参数</summary> <content>这里是包含的div等其他展示元素</content> </details>
css代码
.haorooms{position:relative}
.haorooms summary{
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
outline: 0;
}
/* 自定义的三角 */
.haorooms summary::after {
content: '';
position: absolute;
left:0;
top:0;
width: 15px; height: 15px;
background: url(./haorooms.png) no-repeat; /* 自定义的三角图片 */
background-size: 100% 100%;
transition: transform .2s;
}
.haorooms:not([open]) summary::after {
transform: rotate(90deg);
}
/* 隐藏默认三角 */
.haorooms ::-webkit-details-marker {
display: none;
}
.haorooms ::-moz-list-bullet {
font-size: 0;
}
代码解释
html5的detail和summary本身就是一个展开收起的效果。假如不了解, 可以查看 。
隐藏默认三角如下:
.haorooms ::-webkit-details-marker {
display: none;
}
.haorooms ::-moz-list-bullet {
font-size: 0;
}
details和summary的ui优化
张鑫旭有篇文章,对details和summary介绍的很详细
对应其UI的优化,主要有如下几个方面:
1、小三角的优化,包括颜色、隐藏、位置、替换。
2、outline轮廓的去除
小三角颜色修改
.haorooms ::-webkit-details-marker {
color: gray;
}
.haorooms ::-moz-list-bullet {
color: gray;
}
小三角位置修改-右侧显示
.haorooms summary {
width: -moz-fit-content;
width: fit-content;
direction: rtl;
}
.haorooms ::-webkit-details-marker {
direction: ltr;
}
.haorooms ::-moz-list-bullet {
direction: ltr;
}
outline轮廓的去除
我上面用的是
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
outline: 0;
这样对无障碍访问非常不友好,优化方案可以看张鑫旭大神的做法。
details和summary其他应用
1、更多效果
<details>
<summary>
<p>测试内容测试内容</p>
<div class="more">
<p>haorooms测试内容测试内容...</p>
</div>
<a>更多</a>
</summary>
</details>
css代码
::-webkit-details-marker {
display: none;
}
::-moz-list-bullet {
font-size: 0;
float: left;
}
summary {
user-select: none;
outline: 0;
}
.more {
display: none;
}
[open] .more {
display: block;
}
[open] summary a {
font-size: 0;
}
[open] summary a::before {
content: '收起';
font-size: 14px;
}
2、悬浮菜单效果
CSS代码:
/* 隐藏默认三角 */
::-webkit-details-marker {
display: none;
}
::-moz-list-bullet {
font-size: 0;
float: left;
}
summary {
display: inline-block;
padding: 5px 28px;
text-indent: -15px;
user-select: none;
position: relative;
z-index: 1;
}
summary::after {
content: '';
position: absolute;
width: 12px; height: 12px;
margin: 4px 0 0 .5ch;
background: url(./arrow-on.svg) no-repeat;
background-size: 100% 100%;
transition: transform .2s;
}
[open] summary,
summary:hover {
background-color: #fff;
box-shadow: inset 1px 0 #ddd, inset -1px 0 #ddd;
}
[open] summary::after {
transform: rotate(180deg);
}
.box {
position: absolute;
border: 1px solid #ddd;
background-color: #fff;
min-width: 100px;
padding: 5px 0;
margin-top: -1px;
}
.box a {
display: block;
padding: 5px 10px;
color: inherit;
}
.box a:hover {
background-color: #f0f0f0;
}
.box sup {
position: absolute;
color: #cd0000;
font-size: 12px;
margin-top: -.25em;
}
HTML代码:
<div class="bar">
<details>
<summary>我的消息</summary>
<div class="box">
<a href>我的回答<sup>12</sup></a>
<a href>我的私信</a>
<a href>未评价订单<sup>2</sup></a>
<a href>我的关注</a>
</div>
</details>
</div>
<p>这里放一段文字表明上面的是悬浮效果。</p>
3、树形菜单效果
CSS代码:
/* 隐藏默认三角 */
::-webkit-details-marker {
display: none;
}
::-moz-list-bullet {
font-size: 0;
float: left;
}
details {
padding-left: 20px;
}
summary::before {
content: '';
display: inline-block;
width: 12px; height: 12px;
border: 1px solid #999;
background: linear-gradient(to right, #999, #999) no-repeat center, linear-gradient(to top, #999, #999) no-repeat center;
background-size: 2px 10px, 10px 2px;
vertical-align: -2px;
margin-right: 6px;
margin-left: -20px;
}
[open] > summary::before {
background: linear-gradient(to right, #999, #999) no-repeat center;
background-size: 10px 2px;
}
HTML代码:
<details>
<summary>我的视频</summary>
<details>
<summary>爆肝工程师的异世界狂想曲</summary>
<div>tv1-720p.mp4</div>
<div>tv2-720p.mp4</div>
...
<div>tv10-720p.mp4</div>
</details>
<details>
<summary>七大罪</summary>
<div>七大罪B站00合集.mp4</div>
</details>
<div>珍藏动漫网盘地址.txt</div>
<div>我们的小美好.mp4</div>
</details>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
