实用效果
Last updated
Last updated
::: code-group
<div class="loading">
<div></div>
<div></div>
<div></div>
</div>
@keyframes loading{
/*to 表示100%的效果*/
to {
opacity: .1;
transform: translate(0, -1rem)
}
}
.loading {
display: flex;
justify-content: center;
}
.loading > div {
width: 1rem;
height: 1rem;
margin: 3rem 0.2rem;
background: #8385aa;
border-radius: 50%;
animation: loading 0.6s infinite alternate;
}
.loading > div:nth-child(2) {
animation-delay: .2s;
}
.loading > div:nth-child(3) {
animation-delay: .4s;
}
:::
::: code-group
<div class="button-border">
<button class="button">提交</button>
</div>
.button {
background: #c47135;
border:none;
color:#fff;
outline: none;
padding: 12px 40px 10px;
position: relative;
}
.button::before,
.button:after{
border: solid 0 transparent;
transition: all 0.25s;
content: '';
height: 24px;
position: absolute;
width: 24px;
}
.button:before{
border-top: solid 2px #c47135;
left: 0px;
top: -5px;
}
.button:after{
border-bottom: solid 2px #c47135;
bottom: -5px;
right:0px;
}
.button:hover:before,
.button:hover:after{
width: 100%;
}
:::
::: code-group
<div class="loading"></div>
@keyframes loading {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.loading{
display: inline-block;
border: solid 4px rgba(0,0,0, .1);
border-left-color: #7983ff;
border-radius: 50%;
width: 30px;
height: 30px;
animation: loading 1.2s linear infinite;
}
:::
::: warning 提示 更多 loaders
效果可以参考 css-loaders :::
::: code-group
<div class="rotate_01">Hover</div>
.rotate_01{
display: inline-block;
width:75px;
height:75px;
padding: 10px;
color: white;
line-height: 50px;
text-align: center;
background: #333;
transition: transform 1s ease;
}
.rotate_01:hover{
transform: rotate(45deg);
}
:::
::: code-group
<div class="wrapper">
hover展开过渡效果
<div class="el">看见我了么</div>
</div>
.el {
transition: max-height 0.5s ease;
overflow: hidden;
max-height: 0;
}
.wrapper:hover > .el{
max-height: var(--max-height);
}
var ele = document.querySelector('.el')
var height = ele.scrollHeight
ele.style.setProperty('--max-height', height + 'px')
:::
::: code-group
<p class="hover-animation">快活呀!</p>
.hover-animation{
display: inline-block;
vertical-align: middle;
transition-duration: .3s;
transition-property: box-shadow, transform;
}
.hover-animation:hover,
.hover-animation:focus,
.hover-animation:active{
box-shadow: 1px 10px 10px -10px rgba(0, 0, 24, .5);
transform: scale(1.2)
}
:::
::: code-group
<p class="hover-animation">文字下方出现效果</p>
.hover-animation{
display: inline-block;
position: relative;
color: #0087ca;
}
.hover-animation::after{
content: '';
position: absolute;
width: 100%;
transform: scale(0);
height: 2px;
bottom: 0;
left: 0;
background: #0087ca;
/* 元素在x和y轴的位置 */
transform-origin: bottom right;
transition: transform 0.25s ease-out;
}
.hover-animation:hover::after{
transform: scaleX(1);
transform-origin: bottom left;
}
:::
::: code-group
<p>正常能选择</p>
<p class="no-select">不能被选择</p>
.no-select{user-select: none;}
:::
::: code-group
<div class="parent" tabindex="0">
<div class="child">子菜单</div>
</div>
.parent{
position: relative;
background: red;
width: 100px;
height: 100px;
}
.child{
position: absolute;
visibility: hidden;
left: 100%;
background: #333;
color: white;
padding: 15px;
}
/* focus-within: 一个元素获得焦点,该元素后代元素获得焦点 */
.parent:hover > .child,
.parent:focus > .child,
.parent:focus-within > .child{
visibility: visible;
}
:::
::: code-group
<div class="sibling">
<span>苹果</span>
<span>香蕉</span>
<span>西瓜</span>
<span>橘子</span>
<span>红枣</span>
</div>
.sibling span{
padding: 0 1rem;
transition: opacity .2s;
}
.sibling:hover > span:not(:hover) {
opacity: .5;
}
:::
::: code-group
<div class="clearfix">
<div class="floated">float 一 </div>
<div class="floated">float 二 </div>
<div class="floated">float 三 </div>
</div>
.clearfix::after{
content: '';
display: block;
clear: both;
}
.floated{float: left;}
:::
::: code-group
<div class="container">
<div class="center">
<span>垂直居中</span>
</div>
</div>
.container{
margin: 0 auto;
border: solid 1px #333;
height: 100px;
width: 100px;
}
.container > .center{
display: table;
height: 100%;
width: 100%;
}
.container > .center > span {
display: table-cell;
text-align: center;
vertical-align: middle;
}
:::
::: code-group
<div class="equal-wrapper">
<p>贵金属</p>
<p>能源</p>
<p>有色</p>
</div>
.equal-wrapper{
margin: 0 auto;
width: 300px;
display: flex;
justify-content: space-between;
}
:::
::: code-group
<div class="wrapper">
<img class="image image-contain" src="https://www.baidu.com/img/bd_logo1.png?where=super" />
<img class="image image-cover" src="https://www.baidu.com/img/bd_logo1.png?where=super" />
</div>
.image{
background: #333;
border: solid 1px #333;
width: 200px;
height: 200px;
}
/* object-fit: 自适应 */
/* object-position: 对齐方式 */
.image-contain{
object-fit: contain;
object-position: center;
}
.image-cover{
object-position: cover;
object-position: left top;
}
:::
::: code-group
<div class="flexbox">
<div class="child">这是内容</div>
</div>
.flexbox {
margin: 0 auto;
border: solid 1px #333;
width: 200px;
display: flex;
justify-content: center;
align-items: center;
height: 100px;
}
:::
::: code-group
<div class="parent">
<div class="child">
<p>子元素在父元素垂直居中</p>
</div>
</div>
.parent > .child{
height: 300px;
background: cyan;
}
.parent > .child:before{
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.parent > .child > p{
display: inline-block;
vertical-align: middle;
}
:::
::: code-group
<div class="parent">
<div class="child">
子元素在父元素水平垂直居中
</div>
</div>
.parent {
margin: 0 auto;
width: 300px;
display: grid;
justify-content: center;
align-items: center;
height: 100px;
background: cyan;
}
:::
::: code-group
<div class="parent">
<div class="child">
绝对定位水平垂直居中
</div>
</div>
.parent {
margin: 0 auto;
width: 300px;
border: solid 1px #333;
height: 250px;
position: relative;
width: 250px;
}
.parent > .child{
left: 50%;
position: absolute;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
}
:::
::: code-group
<div class="container">
这是一条神奇的天路诶,这是一条神奇的天路诶,这是一条神奇的天路诶,
这是一条神奇的天路诶,这是一条神奇的天路诶,这是一条神奇的天路诶,
这是一条神奇的天路诶,这是一条神奇的天路诶,这是一条神奇的天路诶,
</div>
.container{
overflow: hidden;
display: block;
height: 109.2px;
margin: 0 auto;
font-size: 26px;
line-height: 1.4;
width: 400px;
position: relative;
}
/* linear-gradient: 方向; color1; color2; ... */
.container:after{
content: '';
position: absolute;
bottom: 0;
right: 0;
width: 150px;
height: 36.4px;
background: linear-gradient(to right, rgba(0, 0, 0, 0), #f5f6f9 50%);
}
:::
::: code-group
<div class="container">
这是一条神奇的天路诶,这是一条神奇的天路诶,这是一条神奇的天路诶,
</div>
.container{
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 200px;
}
:::
::: code-group
<div class="cal"></div>
/* calc运算符前后都需要保留一个空格 */
.cal{
width: 400px;
height: 200px;
background: #333 url('https://daily.zhihu.com/img/new_home_v3/top_logo.png') no-repeat;
background-position: 0 calc(100% - 20px) ;
}
:::
::: code-group
<div class="circle"></div>
.circle{
margin: 0 auto;
width: 2rem;
height: 2rem;
background: #333;
border-radius: 50%;
}
:::
::: code-group
<ul class="wrapper">
<li class="parent">化工</li>
<li class="parent">
能源
<ul>
<li class="child">原油</li>
<li class="child">燃油</li>
</ul>
</li>
<li class="parent">贵金属</li>
</ul>
ul.wrapper {counter-reset: section;}
li {list-style: none;}
li.parent{counter-reset: subsection;}
/* counters: */
.parent:before{
counter-increment: section;
content: counter(section);
}
.child:before{
counter-increment: subsection;
content: counter(section) '.' counter(subsection);
}
:::
::: code-group
<div class="scrollbar-style">
<p>
独在异乡为异客,每逢佳节倍思亲,遥知兄弟登高处,遍插茱萸少一人!
独在异乡为异客,每逢佳节倍思亲,遥知兄弟登高处,遍插茱萸少一人!
独在异乡为异客,每逢佳节倍思亲,遥知兄弟登高处,遍插茱萸少一人!
独在异乡为异客,每逢佳节倍思亲,遥知兄弟登高处,遍插茱萸少一人!
</p>
</div>
.scrollbar-style{
margin: 0 auto;
width: 400px;
height:70px;
overflow-y: scroll;
}
.scrollbar-style::-webkit-scrollbar{
width: 8px;
}
/* 滚动条小方块 */
/* box-shadow: 水平, 垂直, 发散 */
.scrollbar-style::-webkit-scrollbar-track{
box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
border-radius: 10px;
}
/* 滚动条轨道 */
.scrollbar-style::-webkit-scrollbar-thumb{
border-radius: 10px;
box-shadow: inset 0 0 6px rgba(0, 0, 0, .5);
}
:::
::: code-group
<div class="text-selection">
测试复制文本
</div>
::selection{
background: aquamarine;
color:black;
}
.text-selection::selection{
background: deeppink;
color:white;
}
:::
::: code-group
<div class="form-focus">
<form>
<label for="family_name">姓: </label><input id="family_name" type="text" /><br>
<label for="given_name">名: </label><input id="given_name" type="text" />
</form>
</div>
.form-focus{
width: 500px;
margin: 0 auto;
}
form {
border: solid 3px #333;
color: #000;
padding: 5px;
}
form:focus-within {
background: aquamarine;
color: #000;
}
:::
::: code-group
<div class="container">
<p>点击按钮切换到全屏模式</p>
<div class="ele" id="ele"><p>改变全屏背景色</p></div>
<br>
<button onclick="changeToFullscreen()">全屏</button>
</div>
.container {
margin: 40px auto;
max-width: 700px;
}
.ele {
padding: 20px;
height: 300px;
width: 100%;
background-color: skyblue;
box-sizing: border-box;
}
.ele p {
text-align: center;
color: white;
font-size: 3em;
}
.ele:-ms-fullscreen p {
visibility: visible;
}
.ele:fullscreen {
background: red;
width:100vw;
height: 100vh;
}
function changeToFullscreen () {
let el = document.getElementById('ele')
el.requestFullscreen()
}
:::
::: code-group
<button class="gradient-track">
<span>悬停展示效果</span>
</button>
.gradient-track {
margin: 0 auto;
}
.gradient-track {
position: relative;
background: #7983ff;
padding: .5rem 1rem;
font-size: 1.2rem;
border: none;
color: white;
cursor: pointer;
outline: none;
overflow: hidden;
}
.gradient-track span {
position: relative;
}
.gradient-track::before {
--size: 0;
content: '';
position: absolute;
left: var(--x);
top: var(--y);
width: var(--size);
height: var(--size);
background: radial-gradient(circle closest-side, pink, transparent);
transform: translate(-50%, -50%);
transition: width 0.2s ease, height 0.2s ease;
}
.gradient-track:hover::before {
--size: 200px;
}
// e是onmousemove对象, rect是按钮对象
let btn = document.querySelector('.gradient-track')
btn.onmousemove = function (e) {
let rect = e.target.getBoundingClientRect()
let x = e.clientX - rect.left
let y = e.clientY - rect.top
btn.style.setProperty('--x', x + 'px')
btn.style.setProperty('--y', y + 'px')
}
:::
::: code-group
<ul>
<li>苹果</li>
<li>香蕉</li>
<li>菠萝</li>
<li>荔枝</li>
<li>桂圆</li>
</ul>
li {width: 200px;margin: 0 auto;}
li:nth-child(odd) {
background: #eee;
}
:::
::: code-group
<div class="triangle"></div>
.triangle {
width: 0;
height: 0;
border-top: solid 20px #333;
border-left: solid 20px transparent;
border-right: solid 20px transparent;
}
:::
::: code-group
<div class="separator"></div>
.separator {
position: relative;
height: 48px;
background: #333;
}
.separator::after {
content: '';
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 12'%3E%3Cpath d='m12 0l12 12h-24z' fill='%23fff'/%3E%3C/svg%3E");
position: absolute;
width: 100%;
left: 0;
height: 12px;
bottom: 0;
}
:::
::: code-group
<div class="parent">
<div class="child">
独在异乡为异客,每逢佳节倍思亲。遥知兄弟登高处,遍插茱萸少一人。
独在异乡为异客,每逢佳节倍思亲。遥知兄弟登高处,遍插茱萸少一人。
独在异乡为异客,每逢佳节倍思亲。遥知兄弟登高处,遍插茱萸少一人。
独在异乡为异客,每逢佳节倍思亲。遥知兄弟登高处,遍插茱萸少一人。
</div>
</div>
.parent {
position: relative;
}
.parent::after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 250px;
height: 25px;
background: linear-gradient(
rgba(255, 255, 255, 0.001),
white
);
pointer-events: none;
}
.parent > .child {
overflow-y: scroll;
background: white;
width: 200px;
height: 100px;
padding: 15px;
line-height: 1.2;
}
:::
::: code-group
<input type="checkbox" id="toggle" class="offscreen" />
<label for="toggle" class="switch"></label>
.switch {
position: relative;
display: inline-block;
width: 40px;
height: 20px;
background-color: rgba(0, 0, 0, .25);
border-radius: 20px;
transition: all 0.3s;
}
.switch::after {
content: '';
position: absolute;
width: 18px;
height: 18px;
border-radius: 18px;
background-color: white;
top: 1px;
left: 1px;
transition: all 0.3s;
}
input[type='checkbox']:checked + .switch::after {
transform: translateX(20px);
}
input[type='checkbox']:checked + .switch {
background-color: skyblue;
}
.offscreen {
position: absolute;
left: -9999px;
}
:::