This article mainly introduces simpler implementation examples of expand, collapse, and other effects in projects such as vue and react. It has certain reference value. Interested friends can refer to it
Preface
Although the title of this article contains vue and react, it is not related to vue and react, but some basic knowledge of html5 and css3. The reason why I write vue is because I A similar effect has been used in recent projects. I used vue related knowledge to achieve it which is not elegant, but using html5 and css3 to achieve it is even more perfect.
Project case
The project has the following effects:
There are many expand and collapse, for the implementation of this , I initially used some relatively frustrating dom operations in vue, which is a class name of the parent element toggleClass to display and hide child elements.
Since this method is a universal method, it is used in many places in the project. The code is roughly as follows:
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"); } }
Written like this, it is unfriendly and difficult to maintain later. When I refactored the project recently, I refactored all these places and used the method introduced today! For more refactoring points, please click on the article Refactoring Technical Points of Vue Project.
html5 and css3 implement expand and collapse
The code is as follows:
<details class="haorooms" open> <summary>图表参数</summary> <content>这里是包含的p等其他展示元素</content> </details>
css code
.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; }
Code explanation
The detail and summary of html5 itself have an expansion and collapse effect. If you don't understand, you can check .
Hide the default triangle as follows:
.haorooms ::-webkit-details-marker { display: none; } .haorooms ::-moz-list-bullet { font-size: 0; }
UI optimization of details and summary
Zhang Xinxu has an article that introduces details and summary in great detail
Corresponding to the optimization of its UI, the main aspects are as follows:
1. Optimization of small triangles, including color, hiding, position, and replacement.
2. Outline removal
Modify the color of the small triangle
.haorooms ::-webkit-details-marker { color: gray; } .haorooms ::-moz-list-bullet { color: gray; }
Modify the position of the small triangle-display on the right
.haorooms summary { width: -moz-fit-content; width: fit-content; direction: rtl; } .haorooms ::-webkit-details-marker { direction: ltr; } .haorooms ::-moz-list-bullet { direction: ltr; }
Outline removal
I used
-webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; outline: 0;
above. This is very unfriendly to accessibility. For optimization solutions, you can look at Zhang Xinxu’s approach.
details and summary other applications
1. More effects
<details> <summary> <p>测试内容测试内容</p> <p class="more"> <p>haorooms测试内容测试内容...</p> </p> <a>更多</a> </summary> </details>
css code
::-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. Suspended menu effect
CSS code:
/* 隐藏默认三角 */ ::-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 code:
<p class="bar"> <details> <summary>我的消息</summary> <p class="box"> <a href>我的回答<sup>12</sup></a> <a href>我的私信</a> <a href>未评价订单<sup>2</sup></a> <a href>我的关注</a> </p> </details> </p> <p>这里放一段文字表明上面的是悬浮效果。</p>
3. Tree menu effect
CSS code:
/* 隐藏默认三角 */ ::-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 code:
<details> <summary>我的视频</summary> <details> <summary>爆肝工程师的异世界狂想曲</summary> <p>tv1-720p.mp4</p> <p>tv2-720p.mp4</p> ... <p>tv10-720p.mp4</p> </details> <details> <summary>七大罪</summary> <p>七大罪B站00合集.mp4</p> </details> <p>珍藏动漫网盘地址.txt</p> <p>我们的小美好.mp4</p> </details>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to deal with display issues before vue rendering (detailed tutorial)
By using ueditor in the vue project (Detailed tutorial)
What are the steps to introduce noVNC remote desktop into the vue project
The above is the detailed content of Use vue and react to achieve effects such as expansion and collapse. For more information, please follow other related articles on the PHP Chinese website!

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
