欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

纯css实现tab导航栏切换

程序员文章站 2022-05-31 13:17:56
...

本人js水平有点差。。。能用css解决不咋会使用js

最终效果
(这个效果之前是在刷知乎的时候看见的,现在也找不到了。。。有看到原作者的望告知)
纯css实现tab导航栏切换
主要通过label for与表单元素绑定,加上:checked伪类实现效果

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			html, body {
				  background-color: #212121;
				  width: 100%;
				  height: 100%;
				  font-family: 'Rajdhani', sans-serif;
				  font-size: 18px;
				}
				.scifiUI {
				  list-style: none;
				  position: relative;
				  width: 600px;
				  margin: 50px auto;
				  padding: 0;
				  color: #00bebe;
				}
				li {
				  display: inline-block;
				}
				/*让radio组件显示透明*/
				li input[type="radio"] {
				  opacity: 0;
				  position: absolute;
				  outline: none;
				}
				/*设置选中后的label属性*/
				li input[type="radio"]:checked ~ label {
				  background: #00bebe;
				  color: #212121;
				  height: 50px;
				  font-size: 1.2em;
				  cursor: default;
				  box-shadow: 0 0 10px #00dcdc;
				}
				/*设置选中后的section属性*/
				li input[type="radio"]:checked ~ .section {
				  opacity: 1;
				  width: 500px;
				  padding: 50px;
				  border: 1px solid #00bebe;
				  color: #00dcdc;
				  text-indent: 1em;
				}
				li label {
				  display: block;
				  width: 120px;
				  height: 40px;
				  border: 1px solid rgba(0, 190, 190, 0.7);
				  border-bottom: 0;
				  border-radius: 5px 5px 0 0;
				  color: rgba(0, 190, 190, 0.7);
				  line-height: 50px;
				  text-align: center;   /*这两行意思是设置文字居正中间*/
				  cursor: pointer;
				}
				li label:hover {
				  height: 50px;
				  border-color: #00bebe;
				  color: #00bebe;
				}
				li .section {
				  opacity: 0;
				  position: absolute;
				  top: 51px;
				  left: 0;
				  border: 1px solid #00bebe;
				  background: rgba(0, 190, 190, 0.1);
				  transition: all 200ms ease-in-out;
				}
		
		</style>
	</head>
	<body>
		<ul class="scifiUI">
  <li>
    <input type="radio" name="tab" id="tab1" checked>
    <label for="tab1">Tab1</label>
    <div class="section">
     The user interface, in the industrial design field of human–machine interaction, is the space where interactions between humans and machines occur. The goal of this interaction is effective operation and control of the machine on the user's end, and feedback from the machine, which aids the operator in making operational decisions. Examples of this broad concept of user interfaces include the interactive aspects of computer operating systems, hand tools, heavy machinery operator controls, and process controls. The design considerations applicable when creating user interfaces are related to or involve such disciplines as ergonomics and psychology.
    </div>
  </li>
  <li>
    <input type="radio" name="tab" id="tab2" />
    <label for="tab2">Tab2</label>
    <div class="section">
    User Experience (UX) involves a person's behaviors, attitudes, and emotions about using a particular product, system or service. User Experience includes the practical, experiential, affective, meaningful and valuable aspects of human-computer interaction and product ownership. Additionally, it includes a person’s perceptions of system aspects such as utility, ease of use and efficiency. User Experience may be considered subjective in nature to the degree that it is about individual perception and thought with respect to the system. User Experience is dynamic as it is constantly modified over time due to changing usage circumstances and changes to individual systems as well as the wider usage context in which they can be found.
    </div>
  </li>
  <li>
    <input type="radio" name="tab" id="tab3" />
    <label for="tab3">Tab3</label>
    <div class="section">
      Interactive Design is defined as a user-oriented field of study that focuses on meaningful communication of media through cyclical and collaborative processes between people and technology. Successful interactive designs have simple, clearly defined goals, a strong purpose and intuitive screen interface.
    </div>
  </li>
  <li>
    <input type="radio" name="tab" id="tab4" />
    <label for="tab4">Tab4</label>
    <div class="section">
      In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.
    </div>
  </li>
</ul>
	</body>
	
</html>

主要知识点

  1. label for元素与input的id绑定,达到点击label等同于点击radio的效果
  2. :checkedradio组件被选中后的效果

中途出现的一个bug就是当你给定section里面内容过多,但是我们之前又没有给这个盒子设置具体的宽度,所以他达不到li标签横排显示效果,我的方法是先用少数字符代替,等达到效果在进行更换,不知道有没有好的办法