关于css3动画:CSS-实例系列-02-2023-兔年祝福

Hello 小伙伴们早上、中午、下午、早晨和深夜好,这里是 jsliang~ 新年新气象,让咱们耍一个兔飞猛进的祝愿吧: 这个是一个残缺的线上小实例,小伙伴们能够填写数据,服务器会用 Node.js 定期读取数据: 填写数据:https://kdocs.cn/l/cbmawranzvNL成果查看:https://liangjunrong.github.io/例如你填的用户名称是:abab,那么你的链接就是:https://liangjunrong.github.io?username=abab本期将和小伙伴们探讨: [x] 如何通过 HTML + 海量 CSS + 简略 JS,实现这个兔年祝愿实例[x] 如何通过 Node.js,开启无头浏览器读取「金山文档」的数据,同步到 GitHub Page 上本实例的代码地址: Demo —— all for one码上掘金 - 02 - 2023 兔年祝愿一 前言本 CSS 系列文章: 主推学以致用。联合面试题和工作实例,让小伙伴们深刻体验 61 个工作常见的 CSS 属性和各种 CSS 常识。主推纯 CSS。尽可能应用 HTML + CSS 实现学习目标,但依然有 “一小部分” 性能须要用到 JavaScript 常识,适宜新人学习 + 大佬温习。如果文章在一些细节上没写分明或者误导读者,欢送评论/吐槽/批评,你的点赞、珍藏和关注是我更新的能源 ❤ 更多常识分享文章可见:jsliang 的文档库二 前端实现本实例的一些创意,参考自 Jamie Juviler 提供的 24 个 CSS 动画,从中失去启发创作了这封信,在此示意非常感谢: 24 Creative and Unique CSS Animation Examples to Inspire Your Own参考成果: ...

January 16, 2023 · 6 min · jiezi

关于css3动画:探究几种CSS视差动画实现方案及原理

滚动视差介绍视差滚动(Parallax Scrolling)是指多层背景以不同的速度挪动,造成平面的静止成果,带来十分杰出的视觉体验。 实现原理形式一:利用 background-attachment 属性,咱们能够把网页解刨成:背景层、内容层、悬浮层 background-attachment 的作用是设置背景图像随着页面滚动的时候固定,即便一个元素有滚动机制,背景也不会随着元素的内容而滚动,它的值有以下三个: Scroll:默认值,背景图像会随着页面其余部分的滚动而挪动。fixed:当页面的其余部分滚动时,背景图像不会挪动。inherit:继承父元素 background-attachment 属性的值。此实现形式可参考 形式二:配合应用 transform 和 perspective 利用3D视图空间实现物体静止的速度差从而来实现视觉差成果。 原理如下: transform: 能够对元素进行变换(2d/3d),包含平移 translate,旋转 rotate,缩放 scale,等等。 perspective : 当元素波及 3d 变换时,它能够定义咱们眼睛看到的 3d 平面成果,即空间感。 留神:当设置 translateZ 后,须要通过 scale 放大或者放大元素来让元素达到肉眼所见的物体没被被放大放大的感觉,计算 scale 的公式如下: scale = ((translateZ * (-1)) / perspective) + 1经典案例利用background-attachment经典案例这是一款名为Olija的游戏的网站,这是对于一个挥动着鱼叉的英雄摸索友好土地以找到回家的路的故事。游戏有两个外围方面,首先是它的视觉流动性,其像素艺术中的像素尺寸相当大,因而如果不看到它的动静,就很难传播这款游戏的美感;第二个外围方面是它的故事。感觉就像游戏是依据已有的书籍或电视剧改编的。该网站的指标是抓住这两个外围方面,并吸引用户与之互动;让他们感觉本人是故事的一部分,并让他们感觉能够管制口头。实战视差动画demo-01<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title></title> <style> section { height: 100vh; background: rgba(0, 0, 0, .7); color: #fff; display: flex; justify-content: center; align-items: center; font-size: 20vh; } .img1 { background-image: url(https://qnm.xxx.com/Fkx77YvvfwdODmMvNVv_IaQx42wT); background-attachment: fixed; background-size: cover; background-position: center center; } .img2 { background-image: url(https://qnm.xxx.com/Fjh7nb4J7JonMOgGfbxXQU0WNRS0); background-attachment: fixed; background-size: cover; background-position: center center; } .img3 { background-image: url(https://qnm.xxx.com/lhXcD2QvakkntJVZMzyI2xiwEgic); background-attachment: fixed; background-size: cover; background-position: center center; } </style> </head> <body> <section class="text">1</section> <section class="img1">2</section> <section class="text">3</section> <section class="img2">4</section> <section class="text">5</section> <section class="img3">6</section> <section class="text">7</section> </body></html> ...

October 23, 2022 · 2 min · jiezi

关于css3动画:css实现一个简单的-闪烁效果

纯css闪动成果demo具体速度和色彩本人能够本人调 <div class="father-box"> <div class="header-box"> </div> </div>@keyframes test { from { opacity: 1.0; } 50% { opacity: 0.4; } to { opacity: 1.0; }}@-webkit-keyframes test { from { opacity: 1.0; } 50% { opacity: 0.4; } to { opacity: 1.0; }}.father-box { position: relative;}.header-box { color: #fff; padding: 10px; font-size: 15px; height: 300px; background-color: rgba(146, 139, 139, .8); animation: test 1000ms infinite; -webkit-animation: test 1000ms infinite;}成果

July 17, 2022 · 1 min · jiezi

关于css3动画:10个css实现特效

打字成果阐明: 通过 steps() 属性来实现宰割文本的成果。首先,你必须指定 step() 中传入的数量,在这个例子中就是文本的长度。 接着,第二步,咱们应用 @keyframes 去申明什么时候开始执行动画 <div class="typing"> <div class="typing-effect">Typing effect for text</div></div>.typing { height: 80vh; display: flex; align-items: center; justify-content: center;}.typing-effect { width: 22ch; white-space: nowrap; overflow: hidden; border-right: 3px solid; font-family: monospace; font-size: 2em; animation: typing 2s steps(22), effect .5s step-end infinite alternate;}@keyframes typing { from { width: 0; }}@keyframes effect { 50% { border-color: transparent; }}通明图片暗影成果 阐明: 应用:drop-shadow drop-shadow 的工作形式是,其遵循给给定图片的 Alpha 通道。因而暗影是基于图片的外部形态,而不是显示在图片里面。 <div class="transparent-shadow"> <div class="margin-right"> <div class="margin-bottom align-center"> box-shadow </div> <img class="box-shadow" src="https://stackdiary.com/wp-content/uploads/2022/02/logo.png" alt="box-shadow example (transparent)"> </div> <div> <div class="margin-bottom align-center"> drop-shadow </div> <img class="drop-shadow" src="https://stackdiary.com/wp-content/uploads/2022/02/logo.png" alt="drop-shadow example (transparent)"> </div></div>.transparent-shadow { height: 80vh; display: flex; align-items: center; justify-content: center;}.margin-right { margin-right: 2em;}.margin-bottom { margin-bottom: 1em;}.align-center { text-align: center;}.box-shadow { box-shadow: 2px 4px 8px #3723a1;}.drop-shadow { filter: drop-shadow(2px 4px 8px #3723a1);}自定义 Cursor ...

June 23, 2022 · 4 min · jiezi

关于css3动画:纯CSS实现lineargradient的渐变动画效果

话不多说,先上效果图 受制于网站的容量,最多只能上传4MB的动图,所以我调快了动画的速度,缩短了动图的工夫,实际上动画是很弛缓的。 说到动画,家喻户晓,突变是不可能应用通过keyframes实现动画过渡成果的,只会忽然的扭转色彩。 例如,如果关键帧代码如下 @keyframes test { 0%{ background: linear-gradient(60deg, rgba(84,58,183,1) 0%, rgba(0,172,193,1) 100%); } 100%{ background: linear-gradient(60deg, rgb(173, 162, 218) 0%, rgb(201, 14, 189) 100%); }} 能够看到背景色并不会平滑的过渡,只会十分生硬忽然的色彩变动,显然不是咱们想要的成果。 换个想法,没必要局限于在keyframes中应用linear-gradient来实现突变动画成果,linear-gradient不可能平滑过渡,然而position能够呀,所以咱们先实现这样的成果 html代码如下 <div class="header"> <div class="inner-header flex"> <svg version="1.1" class="logo" baseProfile="tiny" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 500 500" xml:space="preserve"> <path fill="#FFFFFF" stroke="#000000" stroke-width="10" stroke-miterlimit="10" d="M57,283" /> <g> <path fill="#fff" d="M250.4,0.8C112.7,0.8,1,112.4,1,250.2c0,137.7,111.7,249.4,249.4,249.4c137.7,0,249.4-111.7,249.4-249.4 C499.8,112.4,388.1,0.8,250.4,0.8z M383.8,326.3c-62,0-101.4-14.1-117.6-46.3c-17.1-34.1-2.3-75.4,13.2-104.1 c-22.4,3-38.4,9.2-47.8,18.3c-11.2,10.9-13.6,26.7-16.3,45c-3.1,20.8-6.6,44.4-25.3,62.4c-19.8,19.1-51.6,26.9-100.2,24.6l1.8-39.7 c35.9,1.6,59.7-2.9,70.8-13.6c8.9-8.6,11.1-22.9,13.5-39.6c6.3-42,14.8-99.4,141.4-99.4h41L333,166c-12.6,16-45.4,68.2-31.2,96.2 c9.2,18.3,41.5,25.6,91.2,24.2l1.1,39.8C390.5,326.2,387.1,326.3,383.8,326.3z" /> </g> </svg> </div> <div> <svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"> <defs> <path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z" /> </defs> <g class="parallax"> <use xlink:href="#gentle-wave" x="48" y="0" fill="rgba(255,255,255,0.7" /> <use xlink:href="#gentle-wave" x="48" y="3" fill="rgba(255,255,255,0.5)" /> <use xlink:href="#gentle-wave" x="48" y="5" fill="rgba(255,255,255,0.3)" /> <use xlink:href="#gentle-wave" x="48" y="7" fill="#fff" /> </g> </svg> </div></div>css代码如下 ...

June 9, 2022 · 2 min · jiezi

关于css3动画:CSS3CSS3动画我离前端的炫酷又近了一步

【CSS3】CSS3动画——我离前端的炫酷又近了一步博客阐明文章所波及的局部材料来自互联网整顿,当然还有本人集体的总结和认识,分享的目标在于共建社区和坚固本人。援用的材料如有侵权,请分割自己删除!幸好我在,感激你来!阐明CSS3 可实现 HTML 元素的动画成果,而不应用 JavaScript 或 Flash。 为了学习前端的动效,我几乎是不折伎俩。目前就抓着CSS的动画来搞一波。 什么是动画⭕老规矩,问句结尾。 在须要变动的工夫节点上指定一些关键帧,关键帧就是此刻所定义的款式。例如我在1s左移5m,3s左一15m,这时就有两个关键帧,因为这两个关键帧,画面就产生了位移,那么动画也就产生了。 上面这句话总结到位:动画使元素逐步从一种款式变为另一种款式。 CSS3的动画次要依赖@keyframes和animation来实现。 @keyframes和animation的浏览器反对 @keyframes 规定@keyframes 规定是创立动画。 @keyframes 规定内指定一个 CSS 款式和动画将逐渐从目前的款式更改为新的款式。 @keyframes myfirst{ from {background: red;} to {background: yellow;}}以上的就是一个简略的动画,关键词 "from" 和 "to",等同于 0% 和 100%。0% 是动画的开始,100% 是动画的实现。也就是从红色变成黄色。 为了失去最佳的浏览器反对,应用 0% 和 100% 选择器是最好的抉择。animationanimation既然动画的关键帧都有了,那么就须要把这个关键帧落实并绑定到某个DOM上。 语法能够一起写,也能够离开写属性。 animation: name duration timing-function delay iteration-count direction fill-mode play-state;animation-name指定要绑定到选择器的关键帧的名称 语法 animation-name: keyframename|none;// keyframename 指定要绑定到选择器的关键帧的名称animation-duration动画指定须要多少秒或毫秒实现 语法 animation-duration: time;// time 指定动画播放实现破费的工夫。animation-timing-function指定动画将如何实现一个周期 语法 animation-timing-function: value;动画函数 linear 动画从头到尾的速度是雷同的。ease 默认。动画以低速开始,而后放慢,在完结前变慢。ease-in 动画以低速开始ease-out 动画以低速完结。ease-in-out 动画以低速开始和完结。steps(int,start|end)指定了工夫函数中的距离数量(步长)。 ...

December 26, 2021 · 1 min · jiezi

关于css3动画:CSS进阶

CSS_进阶1. 动画1) 实现步骤1. 定义动画帧 @keyframes 动画名{ from { // 开始帧 } to { // 完结帧 } } @keyframes 动画名{ 0% { // 开始帧 } 20% { } ... 100% { // 完结帧 } }2. 设定动画(贺卡) animation-name: move; 动画名 animation-duration: 2s; 持续时间 animation-timing-function: linear; 工夫曲线函数(自由落体,贝塞尔曲线) animation-fill-mode:forwards; 填充模式,动画完结后保留哪一帧规定 animation-iteration-count: 2; 动画迭代次数 infinite animation-direction: alternate-reverse; 动画执行的方向 from->to , to->from animation-play-state: paused; 动画状态 animation-delay: 1s; 延迟时间 animation: move 2s 1s 2 linear; 动画的速写模式2) 案例(呼吸灯)<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>hi</title> <style> *{ -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } .container{ width:226px; height: 330px; background: #343434; margin:0 auto ; } .circles{ padding: 36px;/*动画*/ height: 226px; animation-name:wobble; animation-duration: 4.4s; /*9s*/ animation-timing-function: linear; animation-delay: 0s; animation-iteration-count: infinite; } .circles > .outer{ height: 100%; border:5px solid #9b9b9b; padding: 10px;/*动画*/ border-radius:50%; border-radius:50%; animation-name:outer_wobble; animation-duration: 4.4s; /*9s*/ animation-timing-function: linear; animation-delay: 0s; animation-iteration-count: infinite; } .circles > .outer > .inner{ height: 100%; border:15px solid #ffffff; border-radius:50%; border-radius:50%; } @keyframes outer_wobble { 20% { padding: 20px; } 40% { padding:10px; } 65%{ padding:22px; } 100%{ padding: 10px; } } @keyframes wobble { 20% { padding: 26px; } 40% { padding:36px; } 100%{ padding:36px; } } .text{ font-size: 22px; color: #ffffff; text-align: center; } </style></head><body> <div class="container"> <div class="circles"> <!--内部圆--> <div class="outer"> <!--外部--> <div class="inner"> </div> </div> </div> <div class="text"> HI! </div> </div></body></html>3) 案例(梦幻西游)<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>梦幻西游</title> <link rel="stylesheet" href="../common.css"> <style> html { color: #333; font:normal 12px '微软雅黑','Microsoft YaHei'; } body,ul,ol,p,h1,h2,h3 { margin: 0; padding: 0; } ul,ol { list-style: none; } a { text-decoration: none; color: #333; } .content { width: 950px; position: absolute; left: 50%; margin-left: -475px; bottom: 300px; } .content > ul::after { display: block; content: ""; clear: both; } .content > ul > li { float: left; width: 200px; height: 180px; margin-right: 50px; overflow: hidden; } .content > ul > li:last-child { margin-right: 0; } .content > ul > li > div { width: 1600px; height: 180px; animation-name: dong; animation-duration: 1s; animation-timing-function: steps(8); animation-iteration-count: infinite; /*animation-direction: reverse;*/ } .content > ul > li > div img { width: 100%; } /*定义动画*/ @keyframes dong { from { margin-left: 0; } to { margin-left: -1600px; } } html,body,.main { height: 100%; } .main { width: 100%; overflow-x: hidden; } .main > .bg { width: 3920px; height: 100%; margin-left: -1920px; background-image: url('./images/bg.jpg'); background-repeat: repeat-x; animation-name: bg; animation-duration: 30s; animation-timing-function: linear; animation-iteration-count: infinite; } @keyframes bg { from { margin-left: -1920px } to { margin-left: 0; } } </style></head><body> <div class="main"> <div class="bg"></div> </div> <!-- 屏幕两头 宽 200 * 4 + 50*3 = 950 --> <div class="content"> <ul> <li> <div><img src="./images/wk.png" alt=""></div> </li> <li> <div><img src="./images/bj.png" alt=""></div> </li> <li> <div><img src="./images/ts.png" alt=""></div> </li> <li> <div><img src="./images/ss.png" alt=""></div> </li> </ul> </div></body></html>2. 动画库 animate.css动画帧、动画设定规定都有第三方实现,咱们间接应用class即可 ...

August 18, 2021 · 3 min · jiezi

Loading图总有一款适合你

总有一款适合你的Loadinggithub地址,喜欢的可以star下哦 插件预览图 使用教程1.插件代码拷贝 下载后把components目录下countUp.vue文件拷贝到自己项目目录下2.插件全局配置 在项目里main.js中配置如下代码import loading from './components/loading/loading.vue'Vue.component('loading',loading)3.插件使用 vue页面使用<template> <view> <loading ></loading> </view></template><script></script><style></style>还收集了一些其他的Loding Loaders.css css-loaders SVG-Loaders 暂时收集了这些,我相信能满足大部分人的需求了 兼容性uni-app项目中使用都兼容

July 5, 2019 · 1 min · jiezi

译Vue-svg面向想要自由的驾驭-css-动画的人详细解说附代码

大家好,这里是 UX(交互体验设计师)& 前端开发,并且还喜欢画画的 yuki!@yuneco。 在这篇文章里,为了能够更加轻松地去使用 Vue 和 css animation,基础的部分,将会跟大家一步步的详细解释。目标如下图↓所示,用 JavaScript 自由的控制动画。 源码送上 https://github.com/yuneco/css-anime-tutorial 目录那么先从最简单的 svg 标签开始。用 Vue 自由地配置、使其变形。然后利用 css 的 transition 来做动画,最后把动画抽象封装,运用到更加复杂的场景上。 制作 svg创建 Vue 项目显示 svg能够自由配置能够更自由更大角度的变化赋予动画能够连续进行动画抽象封装动画注意点这篇文章介绍的方法并不是使用动画时通用的方法。想要制作更复杂的动画,请使用 anime.js 或者 pixi.js。这篇文章并没有使用专门的动画库,而是自己封装的动画,目标是为了更加深入地理解 Vue、javascript、css 动画。虽然还有很多理由,但能点亮自己的【自己组建能够理解的动画】这方面的技能树,无疑也是很高兴的。文章稍稍有点长,如果你能看完的话,那我也很高兴。 制作 svg第一步,显示这篇教程要使用到的 svg,用 Illustrator 制作自己喜欢的角色去,依次从菜单上选择 [ファイル] -> [書き出し] -> [スクリーン用に書き出し],格式选择 svg,从右边齿轮一样的图标,显示设定。 设定看起来有点复杂的????,这里在 Vue 也没有那么麻烦的去使用 svg,所以这里的设定不需要太在意,右下角的 Responsive(レスポンシブ) 选择记得要取消掉。 设定好了之后,「設定を保存」->「アートボードを書き出し」导出 svg 文件,没有 Illustrator 的同学用其它的文件也 ok。怕麻烦的同学,我姑且在 github 上也放了一份... 用浏览器打开,大概就是这种感觉,名字叫 tama桑,现在刚决定的。为了方便理解,我特意加了 1 像素的边框。 创建 Vue 项目不管怎么说,不创建的 Vue 项目的话,就没法开始。运行,vue create 项目的名字 创建项目,就像下面一样,当然,你也可以根据自己的喜好来。 ...

July 5, 2019 · 7 min · jiezi

CSS-动画分类

CSS 动画主要分为CSS 动画分类和CSS 常用动画库简介与JS 动画;然后CSS 动画分类又分为过渡动画transition和关键帧动画keyframes,keyframes主要是从keyframes规则和keyframes属性来介绍。CSS 常用动画简介库简介与JS 动画主要是Animate.css 的引入、Animate.css 使用举例和JS 动画。 首先,CSS动画分类的基本概念:1 过渡动画(transition) 从初始状态过渡到结束状态所产生的动画,它只能定义初始和借宿两个状态,不能定义中间状态,它是一种很简单的最基础的css动画,这个过渡动画只能被动触发,不能主动触发,而且也不能重复发生。被动触发一次,它发生一次,不能重复。 2 关键帧动画(Animation) 使用关键帧@keyframes,在关键帧里面的写法如下: 在每一个阶段我们可以定义不同的元素状态,也就是css,这样就大大增强了css的动画能力。 总结起来就是:可以定义多个状态,可以实现更复杂的效果,相对于transition它可以主动的触发,也可以重复发生。 @keyframes 动画名称 { 时间点 { 元素状态 } 时间点 { 元素状态 } 时间点 { 元素状态 } 时间点 { 元素状态 } … }其次 浏览器支持完全支持该属性的第一个浏览器版本图片描述 基本语法 .main { animation: animationname(动画名字) duration(持续时间) timing-function(时间运动曲线) delay(延迟) iteration-count(总共重复的次数) direction(方向,正向播放还是反向播放) fill-mode(定义开始和结束的状态) play-state(是否停止或运行这个动画); } //animationname是和keyframes绑定的,在animationname下面定义动画的状态,一种是from...to从开始到结束,这个是和transition一样的,一种是百分比,从0%到100%,可以定义任意的中间状态。 @keyframes animationname { from { css-code } to { css-code } 0% - 100% { css-code } }1 animationname - 关键帧名称 ...

July 3, 2019 · 2 min · jiezi

VueCSS3实现转盘抽奖

最近有个转盘抽奖的需求,搜了一下现有的轮子,有的是用jQuery的动画函数实现的,有的是用canvas绘图然后再用高频率的setTimeout调用旋转方法,前者太老了没法简单移植到vue项目,后者感觉性能表现可能不会太好。也有一些用CSS动画的方案,设计了加速-匀速-减速三个动画,再计算偏转角度让三个动画尽可能无缝衔接,但我感觉绕了大远路,应该有更简单轻量的实现方案。个人更倾向于用transition来实现,不过网上的例子感觉还不够好,有的倾斜文字都没有对齐,最后还是自己手写了一个。无需jQuery,不用js脚本实现动画细节,动画效果完全连续无需衔接,支持动态设置2个以上任意数量的奖项。核心思路是用transition以及rotate实现旋转动画,使用transition-origin和rotate绘制出定位较为精确的轮盘奖项。 代码及实例展示 <script src="//unpkg.com/vue/dist/vue.js"></script><span id="app"> <span>Prize number: {{ prizeNumber }}</span> <button type="button" @click="!rolling && prizeNumber < 8 && (prizeNumber++)" :disabled="rolling || prizeNumber === 8">Add</button> <button type="button" @click="!rolling && prizeNumber > 2 && (prizeNumber--)" :disabled="rolling || prizeNumber === 2">Remove</button> <div class="wheel-wrapper"> <div class="wheel-pointer" @click="onClickRotate" > Start </div> <div class="wheel-bg" :class="{freeze: freeze}" :style="`transform: rotate(${wheelDeg}deg)`" > <div class="prize-list"> <div class="prize-item-wrapper" v-for="(item,index) in prizeList" :key="index" > <div class="prize-item" :style="`transform: rotate(${(360/ prizeList.length) * index}deg)`" > <div class="prize-name"> {{ item.name }} </div> <div class="prize-icon"> <img :src="item.icon"> </div> </div> </div> </div> </div> </div></div>html { background: #DD7C7D;}.wheel-wrapper { width: 300px; height: 300px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .wheel-pointer { width: 60px; height: 60px; border-radius: 1000px; background: yellow; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); text-align: center; line-height: 60px; z-index: 10; } .wheel-bg { width: 100%; height: 100%; border-radius: 1000px; overflow: hidden; transition: transform 4s ease-in-out; background: #7EEF97; &.freeze { transition: none; background: red; } } .prize-list { width: 100%; height: 100%; position: relative; text-align: center; } .prize-item-wrapper { position: absolute; top: 0; left: 50%; transform: translateX(-50%); width: 150px; height: 150px; } .prize-item { width: 100%; height: 100%; transform-origin: bottom; .prize-name { padding: 16px 0; } .prize-icon {} }var Main = { data() { return { freeze: false, rolling: false, wheelDeg: 0, prizeNumber: 8, prizeListOrigin: [ { icon: "https://picsum.photos/40?random=1", name: "$10000" }, { icon: "https://picsum.photos/40?random=6", name: "Thank you!" }, { icon: "https://picsum.photos/40?random=2", name: "$500" }, { icon: "https://picsum.photos/40?random=3", name: "$100" }, { icon: "https://picsum.photos/40?random=6", name: "Thank you!" }, { icon: "https://picsum.photos/40?random=4", name: "$50" }, { icon: "https://picsum.photos/40?random=5", name: "$10" }, { icon: "https://picsum.photos/40?random=6", name: "Thank you!" } ] }; }, computed: { prizeList () { return this.prizeListOrigin.slice(0, this.prizeNumber) } }, methods: { onClickRotate() { if (this.rolling) { return; } this.rolling = true; const { wheelDeg, prizeList } = this; const random = Math.floor(Math.random() * (prizeList.length )); console.log(random); this.wheelDeg = wheelDeg - wheelDeg % 360 + 6 * 360 + (360 - 360 / prizeList.length * random); setTimeout(() => { this.rolling = false; alert("Result:" + prizeList[random].name); }, 4500); } }, watch: { prizeNumber () { this.freeze = true this.wheelDeg = 0 setTimeout(() => { this.freeze = false }, 0) } }};var App = Vue.extend(Main);new App().$mount("#app");

June 25, 2019 · 2 min · jiezi

CSS3新特性之animation纯CSS实现轮播图

HTML:<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="./css/index.css"> </head> <body > <div class="swiper-wrap"> <ul class="swiper-img"> 这里的<!---->是为了消除每个li换行所带来的间距问题 <li><img src="./images/1.jpg" alt="" /></li><!-- --><li><img src="./images/2.jpg" alt="" /></li><!-- --><li><img src="./images/3.jpg" alt="" /></li><!-- --><li><img src="./images/4.jpg" alt="" /></li><!-- --><li><img src="./images/5.jpg" alt="" /></li><!-- --><li><img src="./images/6.jpg" alt="" /></li> </ul> </div> <script src="./js/index.js"></script> </body></html>CSS:body{ padding: 0; margin: 0;}ul{ margin: 0; padding: 0;}.swiper-wrap{ width: 100%; position: absolute; top: 0; bottom: 0; overflow: hidden;}.swiper-wrap li{ display: inline-block;}.swiper-wrap .swiper-img{ width: 600%; height: 100%; border: 1px solid darkcyan;}.swiper-wrap .swiper-img li{ width: calc(100%/6); height: 100%;}.swiper-wrap .swiper-img li img{ width: 100%; height: 100%;}/* 切换 */.swiper-img{ animation: swiper 12s linear infinite;}@keyframes swiper{ 0% { margin-left: 0%;} 9.1% { margin-left: 0%;} 18.2% { margin-left: -100%;} 27.3% { margin-left: -100%;} 36.4% { margin-left: -200%;} 45.5% { margin-left: -200%;} 54.6% { margin-left: -300%;} 63.7% { margin-left: -300%;} 72.8% { margin-left: -400%;} 81.9% { margin-left: -400%;} 91% { margin-left: -500%;} 100% { margin-left: -500%;}}

June 10, 2019 · 1 min · jiezi

CSS实现模拟float-center文字左右环绕图片的效果

什么是文字左右环绕图片?就是下图的效果: 效果的CSS代码可以点击这里查看 在CSS中,并没有float: center这种设置,但是我们可以通过一些小技巧来模拟出类似的效果。 经常会有小伙伴问:有float: left和right,为什么没有float: center?我的答案是: 1.text-align: center已经可以实现内联元素的居中效果 2.在一行中使文字环绕显示在图片的两侧,这种效果浏览器处理起来是非常困难的。除非将文字显示为两列,但这是另一个问题 3.当涉及到文本时,float实际上应该被称为环绕,float:left的意思是“将这个元素放置在容器的左侧,并将其右侧的所有内容都环绕其显示”,在这种情况下,我们讨论的float:center实际上是两侧环绕,这会带来一系列的问题,比如如何确定元素在容器中的“深度”? 为了模拟实现float: center的效果,我们通过创建两个div将文字划分为两列,其中居中的图片写在第一个div中: <div id="container"> <div id="leftcol"> <p><img src=".../01.jpg">京都位于本州岛的中心附近,在王位移到江户... </div> <div id="rightcol"> <p>因此,京都许多保存下来的地方都是联合国教科文组织世界遗产,包括... </div></div>首先,我们设置两个div即.leftcol和.rightcol元素为display: table-cell,同时将容器.container元素设置为display: table: div#container { display: table; width: 80%; max-width: 900px; /* 容器最大宽度900px */ margin: 0 auto; line-height: 1.5;}div#leftcol, div#rightcol { display: table-cell; padding: 1em;}div#container img { width: 55%; /* 图片的宽度是第一列div宽度的55% */ height: auto;}这样,文字就显示成为表格的两列了: 图片会按照其该有的样式显示在第一列的顶部左侧,并且底部与文字的第一行的基线对齐。 接下来,如果给图片设置一个float:right,它就会在第一个div中右侧浮动,并且文字会环绕在图片的左侧显示: 到这里,我们实现了一个最传统的布局:两列文字,其中一列包含一张浮动的图片。 下面,就要使用一些技巧来实现左右环绕的文字效果了。 第一步,通过设置margin-right属性负值,将图片右移一定的距离: div#container img { float: right; width: 55%; height: auto; margin-right: -20%; /* 图片向右移动第一个div宽度20%的距离 */ margin-left: 20px; margin-bottom: 20px;}margin-left和margin-bottom属性设置图片与左侧和下方的文字都间距20px。 ...

April 30, 2019 · 1 min · jiezi

CSS 实现三角形,非 Hack

写过 HTML upvote arrow(向上箭头),speech bubble(对话气泡)或其他类似的尖角元素的人都知道,为了创建一个纯 CSS 实现的三角形,必须使用某些 Hack。最流行的两种方式是通过 边框实现,或 Unicode 字符。不得不说,这些 CSS Hack 都非常聪明,但它们却算不上好的解决方案,代码不优雅且不够灵活。例如,我们无法在三角形上使用背景图片,因为边框和字符只能使用颜色。译注: speech bubble(对话气泡)如下图:使用 Clip-pathClip-path 是 CSS 规范中新属性中的一个,它能让我们只显示元素的一部分并隐藏其余部分。其工作原理如下:假设我们有一个普通的矩形 div 元素。你可以在下面的编辑器中单击 Run 运行并查看渲染后的 HTML。(译注:原文内有在线代码编辑器,此处仅贴出代码,可自行 copy 测试。)div { width: 200px; height: 200px; background: url(https://goo.gl/BeSyyD);}<div></div>为了实现三角形,我们需要使用 polygon() 函数。其参数为以逗号分隔的平面坐标点,这些坐标点定义了我们的剪切遮罩的形状。三角形 = 3个点。可以试着更改值并查看形状是如何变化的。div { width: 200px; height: 200px; background: url(https://goo.gl/BeSyyD); /* 三个点分别为:中上的点,左下的点,右下的点 */ clip-path: polygon(50% 0, 0 100%, 100% 100%);}<div></div>创建的路径中的所有内容都会保留,而路径外内容会被隐藏。通过这种方式,我们不仅可以制作三角形,还可以制作出各种不规则的形状,且这些形状可像普通的 CSS 块一样。(译注:即可以正常运用 CSS 属性在这些形状上)这种方法唯一的缺点就是是我们需要自行计算点的坐标来得到一个好看的三角形。不过,它比使用边框或▲(译注:正三角的 Unicode 字符)更好。浏览器支持如果你查看 clip-path 的 caniuse 页面,一开始你发现貌似兼容性非常不好,但事实上,该属性在 Chrome 中能正常工作,且不需要前缀,在 Safari 中需要加 -webkit- 前缀。Firefox 53 版本以上可用。IE / Edge 一贯的不支持,未来也许会支持。关于 clip-path 属性有很多小技巧,包括 SVG 的“奇幻”用法。了解更多,请查看下面的链接。MDN 上的 clip-path - 链接Codrops 上的深入 clip-path 教程 - 链接Clippy, 一个 clip-path 生成器 - 链接 ...

April 18, 2019 · 1 min · jiezi

5个惊艳的CSS3、js动画库

Animates动画库magic.css动画库Bounce.js动画库Effect.css动画库hover.css动画库

April 10, 2019 · 1 min · jiezi

Canvas绘制出一个时钟

参考视频资料:Canvas 绘制时钟最近复习到Canvas,先准备来段有趣的代码,用Canvas绘制出一个动态的时钟。然后后续再对Canvas进行进一步学习。以下代码均来自以上链接所属的视频教程。 【侵删】完整代码:<!DOCTYPE html><html lang=“en”><head> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1.0”> <meta http-equiv=“X-UA-Compatible” content=“ie=edge”> <title>Document</title> <style type=“text/css”> div { text-align: center; margin-top: 250px; } </style></head><body> <div> <canvas id=“clock” height=“200px” width=“200px”>你的浏览器不支持canvas</canvas> </div> <script> var dom = document.getElementById(‘clock’); var ctx = dom.getContext(‘2d’); var width = ctx.canvas.width; var height = ctx.canvas.height; var r = width / 2; //绘制表盘 function drawBackground() { ctx.save(); ctx.translate(r, r); ctx.beginPath(); ctx.lineWidth = 10; ctx.arc(0, 0, r - 5, 0, 2 * Math.PI, false); ctx.stroke(); var hourNumbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]; ctx.font = ‘18px Arial’; ctx.textAlign = ‘center’; ctx.textBaseline = ‘middle’; //小时数字 hourNumbers.forEach(function (number, i) { var rad = 2 * Math.PI / 12 * i; var x = Math.cos(rad) * (r - 30); var y = Math.sin(rad) * (r - 30); ctx.fillText(number, x, y); // console.log(x) }) //绘制分刻度 for (var i = 0; i < 60; i++) { var rad = 2 * Math.PI / 60 * i; var x = Math.cos(rad) * (r - 18); var y = Math.sin(rad) * (r - 18); ctx.beginPath(); if (i % 5 == 0) { ctx.fillStyle = ‘#000’; ctx.arc(x, y, 2, 0, 2 * Math.PI, false); } else { ctx.fillStyle = ‘#ccc’; ctx.arc(x, y, 2, 0, 2 * Math.PI, false); } ctx.fill(); } } //绘制时针 function drawHour(hour, minute) { ctx.save(); ctx.beginPath(); var rad = 2 * Math.PI / 12 * hour; var mrad = 2 * Math.PI / 12 / 60 * minute; ctx.rotate(rad + mrad); ctx.lineWidth = 6; ctx.lineCap = ‘round’; ctx.moveTo(0, 10); ctx.lineTo(0, -r / 2); ctx.stroke(); ctx.restore(); } //绘制分针 function drawMinute(minute) { ctx.save(); ctx.beginPath(); var rad = 2 * Math.PI / 60 * minute; ctx.rotate(rad); ctx.lineWidth = 3; ctx.lineCap = ‘round’; ctx.moveTo(0, 10); ctx.lineTo(0, -r + 30); ctx.stroke(); ctx.restore(); } //绘制秒针 function drawSecond(second) { ctx.save(); ctx.beginPath(); ctx.fillStyle = ‘red’ var rad = 2 * Math.PI / 60 * second; ctx.rotate(rad); ctx.moveTo(-2, 20); ctx.lineTo(2, 20); ctx.lineTo(1, -r + 18); ctx.lineTo(-1, -r + 18); ctx.fill(); ctx.restore(); } //绘制指针的端点 function drawDot() { ctx.beginPath(); ctx.fillStyle = ‘white’; ctx.arc(0, 0, 3, 0, 2 * Math.PI, false); ctx.fill(); } //动起来 function draw() { //清除之前所绘制的 ctx.clearRect(0, 0, width, height); var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); drawBackground(); drawHour(hour, minute); drawMinute(minute); drawSecond(second) drawDot(); ctx.restore(); } //draw(); setInterval(draw, 1000); </script></body></html> ...

March 11, 2019 · 2 min · jiezi

【前端优化】动画几种实现方式总结和性能分析

备注:没整理格式,抱歉动画实现的几种方式:性能排序js < requestAnimationFrame <css3< Canvasjs实现方式:1.setTimeout 自身调用 eg12.setInterval 调用 eg2setTimeout的定时器值推荐最小使用16.7ms的原因(16.7 = 1000 / 60, 即每秒60帧)为什么倒计时动画一定要用setTimeout而避免使用setInterval——-两者区别及setTimeout引发的js线程讨论1.js线程讨论1.1 为什么:单线程是JavaScript的一大特性。JavaScript是浏览器用来与用户进行交互、进行DOM操作的,这也使得了它必须是单线程这一特性。比如你去修改一个元素的DOM,同时又去删除这个元素,那么浏览器应该听谁的?1.2 js单线程工作机制是:当线程中没有执行任何同步代码的前提下才会执行异步代码var t = true;window.setTimeout(function (){ t = false;},1000);while (t){}alert(’end’)JavaScript引擎是单线程运行的,浏览器只有一个线程在运行JavaScript程序1.3 浏览器工作基本原理一、浏览器的内核是多线程的,内核制控下保持同步,至少实现三个常驻线程:javascript引擎线程,GUI渲染线程,浏览器事件触发线程(http请求线程等)javascript引擎是基于事件驱动单线程执行的,JS引擎一直等待着任务队列中任务的到来,然后加以处理,浏览器无论什么时候都只有一个JS线程在运行JS程序。GUI渲染线程负责渲染浏览器界面,当界面需要重绘(Repaint)或由于某种操作引发回流(reflow)时,该线程就会执行。但需要注意 GUI渲染线程与JS引擎是互斥的,当JS引擎执行时GUI线程会被挂起,GUI更新会被保存在一个队列中等到JS引擎空闲时立即被执行。事件触发线程,当一个事件被触发时该线程会把事件添加到待处理队列的队尾,等待JS引擎的处理。异步事件:如setTimeOut、浏览器内核的其他线程如鼠标点击、AJAX异步请求等,(当线程中没有执行任何同步代码的前提下才会执行异步代码)也就是说即使setTimeout为0,他也是等js引擎的代码执行完之后才会插入到js引擎线程的最后执行。1.4 JavaScript中任务,一种是同步任务,一种是异步任务。同步任务:各个任务按照文档定义的顺序一一推入"执行栈"中,当前一个任务执行完毕,才会开始执行下一个任务。异步任务:各个任务推入"任务队列"中,只有在当前的所有同步任务执行完毕,才会将队列中的任务"出队"执行。(注:这里的异步任务并不一定是按照文档定义的顺序推入队列中)//只有用户触发点击事件才会被推入队列中(如果点击时间小于定时器指定的时间,则先于定时器推入,否则反之)1.5 “任务队列是什么?异步任务通常包括哪些?“任务队列(event loop):你可理解为用于存放事件的队列,当执行一个异步任务时,就相当于执行任务的回调函数。通常io(ajax获取服务器数据)、用户/浏览器自执行事件(onclick、onload、onkeyup等等)以及定时器(setTimeout、setInterval)都可以算作异步操作。先来看一段代码来理解一下console.log(“1”);setTimeout(function(){console.log(“2”);},1000);console.log(“3”);setTimeout(function(){console.log(“4”);},0);输出结果: 1->3->4->2.那么在来看你这段代码。var t = true;window.setTimeout(function (){t = false},1000);while (t){}alert(’end’);1.6 setTimeOut的讨论参数描述code必需。要调用的函数后要执行的 JavaScript 代码串。millisec必需。在执行代码前需等待的毫秒数。提示:setTimeout() 只执行 code 一次。如果要多次调用,请使用 setInterval() 或者让 code 自身再次原理:setTimeout调用的时候,JavaScript引擎会启动定时器timer,当定时器时间到,就把该事件放到主事件队列等待处理。注意:浏览器JavaScript线程空闲的时候才会真正执行 ep3millisec参数有什么用?那么问题来了。setTimeout(handler,0)和setTimeout(handler,100)在单独使用时,好像并没有区别。(中间执行的代码处理时间超过100ms时)millisec一般在多个setTimeout一起使用的时,需要区分哪个先加入到队列的时候才有用,否则都可以设置成setTimeout(handler,0)1.7 SetTimeout 与 setInterval的区别setTimeout(function(){/* 代码块… */setTimeout(arguments.callee, 10);}, 10);setInterval(function(){/*代码块… */}, 10);setTimeout递归执行的代码必须是上一次执行完了并间格一定时间才再次执行比仿说: setTimeout延迟时间为1秒执行, 要执行的代码需要2秒来执行,那这段代码上一次与下一次的执行时间为3秒. 而不是我们想象的每1秒执行一次.setInterval是排队执行的比仿说: setInterval每次执行时间为1秒,而执行的代码需要2秒执行, 那它还是每次去执行这段代码, 上次还没执行完的代码会排队, 上一次执行完下一次的就立即执行, 这样实际执行的间隔时间为2秒这样的话在我看来, 如果setInterval执行的代码时间长度比每次执行的间隔段的话,就没有意义,并且队伍越来越长,内存就被吃光了.如果某一次执行被卡住了,那程序就会被堵死巨坑无比的setInterval定时器的代码可能在代码还没有执行完成再次被添加到队列,结果导致循环内的判断条件不准确,代码多执行几次,之间没有停顿。JavaScript已经解决这个问题,当使用setInterval()时,仅当没有该定时器的其他代码实例时才将定时器代码插入队列。这样确保了定时器代码加入到队列的最小时间间隔为指定间隔某些间隔会被跳过2.多个定时器的代码执行之间的间隔可能比预期要小大前端团队 > 前端动画实现 > image2017-11-28 14:24:25.png5处,创建一个定时器205处,添加一个定时器,但是onclick代码没执行完成,等待300处,onclick代码执行完毕,执行第一个定时器405处,添加第二个定时器,但前一个定时器没有执行完成,等待605处,本来是要添加第三个定时器,但是此时发现,队列中有了一个定时器,被跳过等到第一个定时器代码执行完毕,马上执行第二个定时器,所以间隔会比预期的小。二 CSS3动画1.tansitiontransition-property 要运动的样式 (all || [attr] || none)transition-duration 运动时间transition-delay 延迟时间transition-timing-function 运动形式ease:(逐渐变慢)默认值linear:(匀速)ease-in:(加速)ease-out:(减速)ease-in-out:(先加速后减速)cubic-bezier 贝塞尔曲线( x1, y1, x2, y2 ) http://matthewlein.com/ceaser/transition的完整写法如下img { transition: 1s 1s height ease;}单独定义成各个属性。img{ transition-property: height; transition-duration: 1s; transition-delay: 1s; transition-timing-function: ease;}/可以多个动画同时运动/用逗号隔开transition:1s width,2s height,3s background;/可以在动画完成时间之后添加动画延迟执行的时间/transition:1s width,2s 1s height,3s 3s background;过渡完成事件// Webkit内核: obj.addEventListener(‘webkitTransitionEnd’,function(){},false);// firefox: obj.addEventListener(’transitionend’,function(){},false);/tansition动画发生在样式改变的时候/function addEnd(obj,fn) —封装适应与各个浏览器的动画结束{ //动画执行完执行该函数 obj.addEventListener(‘WebkitTransitionEnd’,fn,false); obj.addEventListener(’transitionend’,fn,false); //标准}addEnd(oBox,function(){ alert(“end”); });// 面临两个bug:1.tansition中有多个动画时,每个执行完,都会有一个结束弹出 // 2.发生重复调用的情况–需要移除//移除动画执行完的操作function removeEnd(obj,fn)} obj.removeEventListener(’transitionend’,fn,false); obj.removeEventListener(‘WebkitTransitionEnd’,fn,false);{使用注意(1)不是所有的CSS属性都支持transitionhttp://oli.jp/2010/css-animatable-properties/http://leaverou.github.io/animatable/(2)transition需要明确知道,开始状态和结束状态的具体数值,才能计算出中间状态transition的局限transition的优点在于简单易用,但是它有几个很大的局限。(1)transition需要事件触发,所以没法在网页加载时自动发生。(2)transition是一次性的,不能重复发生,除非一再触发。(3)transition只能定义开始状态和结束状态,不能定义中间状态,也就是说只有两个状态。(4)一条transition规则,只能定义一个属性的变化,不能涉及多个属性。CSS Animation就是为了解决这些问题而提出的。2.transformrotate() 旋转函数 取值度数 deg 度数 -origin 旋转的基点skew() 倾斜函数 取值度数skewX()skewY()scale() 缩放函数 取值 正数、负数和小数scaleX()scaleY()translate() 位移函数translateX()translateY()Transform 执行顺序问题 — 后写先执行-webkit-transform:rotate(360deg);旋转原点可以是关键字+像素位置:相对于左上角作为零点:正为下,右-webkit-transform-origin:right bottom;-webkit-transform-origin:200px 200px;一个transform可以有多个值: -webkit-transform:rotate(360deg) scale(0.2);-webkit-transform:skewX(45deg);-webkit-transform:skewY(45deg); -webkit-transform:skew(15deg,30deg);3.Animation 关键帧——keyFrames只需指明两个状态,之间的过程由计算机自动计算关键帧的时间单位数字:0%、25%、100%等字符:from(0%)、to(100%)格式@keyframes 动画名称{动画状态}@keyframes miaov_test{from { background:red; }to { background:green; }}可以只有to必要属性animation-name 动画名称(关键帧名称)animation-duration 动画持续时间属性:animation-play-state 播放状态( running 播放 和paused 暂停 )animation-timing-function 动画运动形式linear 匀速。ease 缓冲。ease-in 由慢到快。ease-out 由快到慢。ease-in-out 由慢到快再到慢。cubic-bezier(number, number, number, number): 特定的贝塞尔曲线类型,4个数值需在[0, 1]区间内animation-delay 动画延迟只是第一次animation-iteration-count 重复次数/infinite为无限次animation-direction 播放前重置/动画是否重置后再开始播放alternate 动画直接从上一次停止的位置开始执行normal 动画第二次直接跳到0%的状态开始执行reversealternate-reverseanimation-fill-modeforwards 让动画保持在结束状态none:默认值,回到动画没开始时的状态。backwards:让动画回到第一帧的状态。both: 根据animation-direction(见后)轮流应用forwards和backwards规则。animation-play-statepausedrunning动画播放过程中,会突然停止。这时,默认行为是跳回到动画的开始状态,想让动画保持突然终止时的状态,就要使用animation-play-state属性大前端团队 > 前端动画实现 > image2017-11-28 14:29:8.pnganimation也是一个简写形式div:hover { animation: 1s 1s rainbow linear 3 forwards normal;}分解成各个单独的属性div:hover { animation-name: rainbow; animation-duration: 1s; animation-timing-function: linear; animation-delay: 1s; animation-fill-mode:forwards; animation-direction: normal; animation-iteration-count: 3;}Animation与Js的结合通过class,在class里加入animation的各种属性直接给元素加-webkit-animation-xxx样式animation的问题写起来麻烦没法动态改变目标点位置animation的函数:obj.addEventListener(‘webkitAnimationEnd’, function (){}, false);实例1:无缝滚动animation的stepeg: http://dabblet.com/gist/1745856animation-timing-function: steps(30, end)1.什么时候使用:animation默认以ease方式过渡,它会在每个关键帧之间插入补间动画,所以动画效果是连贯性的,除了ease,linear、cubic-bezier之类的过渡函数都会为其插入补间。但有些效果不需要补间,只需要关键帧之间的跳跃,这时应该使用steps过渡方式大前端团队 > 前端动画实现 > image2017-11-28 14:29:45.png线性动画: http://sandbox.runjs.cn/show/…帧动画:http://sandbox.runjs.cn/show/…2.step使用:语法:steps(number[, end | start])参数说明:number参数指定了时间函数中的间隔数量(必须是正整数)第二个参数是可选的,可设值:start和end,表示在每个间隔的起点或是终点发生阶跃变化,如果忽略,默认是end。大前端团队 > 前端动画实现 > image2017-11-28 14:30:37.png横轴表示时间,纵轴表示动画完成度(也就是0%~100%)。第一个图,steps(1, start)将动画分为1段,跳跃点为start,也就是说动画在每个周期的起点发生阶跃(即图中的空心圆 → 实心圆)。由于只有一段,后续就不再发生动画了。第二个图,steps(1, end)同样是将动画分为1段,但跳跃点是end,也就是动画在每个周期的终点发生阶跃,也是图中的空心圆 → 实心圆,但注意时间,是在终点才发生动画。第三个图,steps(3, start)将动画分为三段,跳跃点为start,动画在每个周期的起点发生阶跃(即图中的空心圆 → 实心圆)。在这里,由于动画的第一次阶跃是在第一阶段的起点处(0s),所以我们看到的动画的初始状态其实已经是 1/3 的状态,因此我们看到的动画的过程为 1/3 → 2/3 → 1 。第四个图,steps(3, end)也是将动画分为三段,但跳跃点为end,动画在每个周期的终点发生阶跃(即图中的空心圆 → 实心圆)。虽然动画的状态最终会到达100%,但是动画已经结束,所以100%的状态是看不到的,因此我们最终看到的动画的过程是0 → 1/3 → 2/3。https://idiotwu.me/study/timi…steps第一个参数的错误的理解:第一个参数 number 为指定的间隔数,即把动画分为 n 步阶段性展示,估计大多数人理解就是keyframes写的变化次数@-webkit-keyframes circle { 0% {background-position-x: 0;} 100%{background-position-x: -400px;} }@-webkit-keyframes circle { 0% {} 25%{} 50%{} 75%{} 100%{} }如果有多个帧动画@-webkit-keyframes circle { 0% {background-position-x: 0;} 50% {background-position-x: -200px;} 100%{background-position-x: -400px;} } 0-25 之间变化5次, 25-50之间 变化5次 ,50-75 之间变化5次,以此类推应用:Sprite 精灵动画 2D游戏https://idiotwu.me/css3-runni…4.3D转换父容器:transform-style(preserve-3d) 建立3D空间Perspective 景深Perspective- origin 景深基点子元素:Transform 新增函数rotateX()rotateY()rotateZ()translateZ()scaleZ()实例1:3D盒子http://beiyuu.com/css3-animation使用实例:requestAnimationFrame是什么js的一个API该方法通过在系统准备好绘制动画帧时调用该帧,从而为创建动画网页提供了一种更平滑更高效的方法使用var handle = setTimeout(renderLoop, PERIOD);var handle = window.requestAnimationFrame(renderLoop);window.cancelAnimationFrame(handle);为什么出现css:统一的向下兼容策略 IE8, IE9之流CSS3动画不能应用所有属性 scrollTop值。如果我们希望返回顶部是个平滑滚动效果CSS3支持的动画效果有限 CSS3动画的贝塞尔曲线是一个标准3次方曲线缓动(Tween)知识:Linear:无缓动效果Quadratic:二次方的缓动(t^2)Cubic:三次方的缓动(t^3)Quartic:四次方的缓动(t^4)Quintic:五次方的缓动(t^5)Sinusoidal:正弦曲线的缓动(sin(t))Exponential:指数曲线的缓动(2^t)Circular:圆形曲线的缓动(sqrt(1-t^2))Elastic:指数衰减的正弦曲线缓动超过范围的三次方缓动((s+1)t^3 – st^2)指数衰减的反弹缓动js:1.延迟时间固定导致了动画过度绘制,浪费 CPU 周期以及消耗额外的电能等问题2.即使看不到网站,特别是当网站使用背景选项卡中的页面或浏览器已最小化时,动画都会频繁出现大前端团队 > 前端动画实现 > image2017-11-28 14:31:6.png相当一部分的浏览器的显示频率是16.7ms搞个10ms setTimeout,就会是下面一行的模样——每第三个图形都无法绘制显示器16.7ms刷新间隔之前发生了其他绘制请求(setTimeout),导致所有第三帧丢失,继而导致动画断续显示(堵车的感觉),这就是过度绘制带来的问题requestAnimationFrame 与setTimeout相似,都是延迟执行,不过更智能,跟着浏览器的绘制走,如果浏览设备绘制间隔是16.7ms,那我就这个间隔绘制;如果浏览设备绘制间隔是10ms, 我就10ms绘制,浏览器(如页面)每次要重绘,就会通知(requestAnimationFrame)页面最小化了,或者被Tab切换当前页面不可见。页面不会发生重绘兼容性Android设备不支持,其他设备基本上跟CSS3动画的支持一模一样https://developer.mozilla.org… ...

March 1, 2019 · 2 min · jiezi

css3动画

css3动画主要包含两个知识点:1是css过渡(transition) 2是css动画(animation)1.transition,css过渡是元素从一种样式逐渐改变为另一种的效果。必须规定两项内容:1,指定要添加效果的css属性,2.指定效果的持续时间。<!DOCTYPE html><html><head><meta charset=“utf-8”> <title>test</title><style> div{width:100px;height:100px;background:red;transition:background 2s;-webkit-transition:background 2s; /* Safari /}div:hover{background:green;}</style></head><body><p><b>注意:</b>该实例无法在 Internet Explorer 9 及更早 IE 版本上工作。</p><div></div><p>鼠标移动到 div 元素上,查看过渡效果。</p></body></html>注意:transition后面跟的属性就是要变化的属性。 2.animation。要创建css3,必须先了解@keyframes规则。@keyframes作用就是创建动画,可以指定一个css样式和动画逐步从目前的样式更改为新的样式。@keyfragmes里没有时间秒的概念,只有百分比的概念,0%就是动画的开始,100%就是动画的结束,动画可以设置播放次数,默认是1次。即播放完一次动画就停止。写完@keyframes接下来就是用animation去调用keyframes了。使用例子:<!DOCTYPE html><html><head><meta charset=“utf-8”> <title>test</title><style> div{width:100px;height:100px;background:red;position:relative;animation:myfirst 5s linear 2s infinite alternate;/ Firefox: /-moz-animation:myfirst 5s linear 2s infinite alternate;/ Safari and Chrome: /-webkit-animation:myfirst 5s linear 2s infinite alternate;/ Opera: /-o-animation:myfirst 5s linear 2s infinite alternate;}@keyframes myfirst{0% {background:red; left:0px; top:0px;}25% {background:yellow; left:200px; top:0px;}50% {background:blue; left:200px; top:200px;}75% {background:green; left:0px; top:200px;}100% {background:red; left:0px; top:0px;}}@-moz-keyframes myfirst / Firefox /{0% {background:red; left:0px; top:0px;}25% {background:yellow; left:200px; top:0px;}50% {background:blue; left:200px; top:200px;}75% {background:green; left:0px; top:200px;}100% {background:red; left:0px; top:0px;}}@-webkit-keyframes myfirst / Safari and Chrome /{0% {background:red; left:0px; top:0px;}25% {background:yellow; left:200px; top:0px;}50% {background:blue; left:200px; top:200px;}75% {background:green; left:0px; top:200px;}100% {background:red; left:0px; top:0px;}}@-o-keyframes myfirst / Opera */{0% {background:red; left:0px; top:0px;}25% {background:yellow; left:200px; top:0px;}50% {background:blue; left:200px; top:200px;}75% {background:green; left:0px; top:200px;}100% {background:red; left:0px; top:0px;}}</style></head><body><p><b>注意:</b> 该实例在 Internet Explorer 9 及更早 IE 版本是无效的。</p><div></div></body></html> ...

February 18, 2019 · 1 min · jiezi

LockMe 又双叒叕一款密码管理工具

简介LockMe 是什么呢?一款免费开源的密码管理小程序。源码地址 https://github.com/pushmetop/lockme 。市面上有许许多多的密码管理工具,为何要特意造轮子呢?自己造的轮子放心。需要写一个小程序练手。想体验 免费 小程序云所谓的 Serverless。为什么不使用 wepy 这类小程序开发框架呢?学习和使用一个知识的时候,要知其然,知其所以然。wepy 等框架其实对小程序进行了很多特殊的封装,如果对小程序本身不熟悉,出现错误的时候有可能会一头雾水。LockMe 怎么保证安全性?采用了 国密算法 SM2 的公私钥算法。私钥由用户个人进行保存。私钥不进行触网。密码数据都由公钥进行加密存储。使用与配置初始化项目创建 project.config.json复制 miniprogram/config-example.js 为 miniprogram/utils/Config.js 环境变量配置 miniprogram/utils/Config.js 环境变量上传 cloudfunctions/ 云函数根据 cloudfunction.collection 创建云数据库环境变量说明cloud.env 小程序云环境 IDcloud.collection 小程序云的对应数据库集合cloud.collection.safes 密码集合cloud.collection.settings 设置集合其他配置请参考小程序官方文档 传送门演示初始化密码中心设置设计理念基于大家日常生活中最常使用的网站 百度 的交互进行设计,更方便和直觉的让用户使用 LockMe。先回想一下平时使用百度时的三个基本状态:不进行任何搜索操作时:进行关键词搜索且有结果时:无结果时需要点击 百度一下:因而在 LockMe 中的搜索密码、创建密码的交互逻辑也与此类似。不进行任何搜索操作时:进行关键词搜索且有结果时:无结果时需要点击 创建 便可创建新的密码记录:嘻嘻,所以 LockMe 是一款有大厂交互的工具,小二吹了这么久,小哥哥小姐姐你还不试试嘛?还不赶紧给小二点个 Star?打赏&联系如果您感觉有收获,欢迎给我打赏,以激励我输出更多的优质内容。本文原稿来自 PushMeTop

February 11, 2019 · 1 min · jiezi

三分钟掌握 React render props

上午review 代码的时候, 发现一些问题, 关于逻辑复用的一些小技巧。 然后就花点时间整理了下, 做了个例子, 聊一下 render props.what is it ?简单点讲, render props 就一种在组件间共享逻辑的技巧。 把一些渲染逻辑以prop 的形式传递给 Component, 把注意力集中在渲染逻辑上。What do render props do?处理组件的渲染逻辑。When to use ?当你发现组件中有重复的逻辑或者模式的时候。比如:重复的UI结构共享某个数据源共享某个全局事件(比如scroll, resize, …)A live demo光说不练假把式, 一起看个例子。想了想, 写个表吧, 会动的那种(年会毛都没中,给个手环也好啊..)。一番操作之后, 我们花了一个表:现在我们又想换个表带, 改怎么做? 重写一个吗? 显然不是。这时候就轮到 render props 登场了。我们可以把一个个部分独立出来, 把有差异的部分当作prop 传入就可以了。上代码:class Watch extends Component { state = { date: moment(), } static propTypes = { face: PropTypes.func, } static defaultProps = { face: date => <DefaultFace date={date} />, } componentDidMount = () => (this.TICK = setInterval(this.update, 1000)) componentWillUnmount = () => clearInterval(this.TICK) update = () => this.setState({ date: moment() }) render = () => ( <Strap> <Bezel> <Screen> <Face>{this.props.face(this.state.date)}</Face> </Screen> </Bezel> </Strap> )}不用关注 Strap, Bezel, Screen 这些, 我们只看关键点: Face.如果我们啥也不传, 得到的将是一个空空如也的表盘:这时候可以给他加个 DefaultFace, 显示 HH:mm static defaultProps = { face: date => <DefaultFace date={date} />, }很赞。现在给他换个样子, 骚黄色:const SecondsFace = ({ date }) => { const hours = date.format(‘HH’) const minutes = date.format(‘mm’) const seconds = date.format(‘ss’) return ( <> <Value>{hours}</Value> <Value>{minutes}</Value> <Value>{seconds}</Value> </> )}SecondsFace.propTypes = watchfacePropTypes;心满意足。这时候我们的render 是这样的:class App extends Component { render() { return ( <Watch /> <Watch face={date => <SecondsFace date={date} />} /> </div> ); }}如此这般, 可以把其他款式的表都写了:舒服~年会又又又又啥也没中的心情放佛得到了安抚。完整代码在这里: 代码 ,喜欢的可以给个星星。Live Demo : codeOpenTipsDos ????当有组件可以共享或者部分渲染逻辑重复的时候Dont’s ????宁可不用也不要滥用避免在使用PureComponents 的时候用render Props. 除非你的prop 是静态定义的。Notes ⚠️Render Prop 只是一个技巧,名字啥都可以, 比如 children。大部分可以用Render Prop的场景, 也可以用HOC 实现, 反之亦然。That’s it.:)如有纰漏, 欢迎指正,谢谢。更多参考:https://reactjs.org/docs/rend… ...

January 24, 2019 · 1 min · jiezi

Animate.css 超强CSS3动画库,三行代码搞定H5页面动画特效!

一、基本用法引入CSS依赖<link rel=“stylesheet” href=“https://cdn.bootcss.com/animate.css/3.7.0/animate.min.css">在元素的Class中加以下内容animated (必选)infinite (可选) 无限重复bounce (必选) 动画样式 参考下方表格delay-2s (可选) 延迟出现 <div class=“animated infinite bounce delay-2s”><h1>Example</h1></div>Class Name bounceflashpulserubberBandshakeheadShakeswingtadawobblejellobounceInbounceInDownbounceInLeftbounceInRightbounceInUpbounceOutbounceOutDownbounceOutLeftbounceOutRightbounceOutUpfadeInfadeInDownfadeInDownBigfadeInLeftfadeInLeftBigfadeInRightfadeInRightBigfadeInUpfadeInUpBigfadeOutfadeOutDownfadeOutDownBigfadeOutLeftfadeOutLeftBigfadeOutRightfadeOutRightBigfadeOutUpfadeOutUpBigflipInXflipInYflipOutXflipOutYlightSpeedInlightSpeedOutrotateInrotateInDownLeftrotateInDownRightrotateInUpLeftrotateInUpRightrotateOutrotateOutDownLeftrotateOutDownRightrotateOutUpLeftrotateOutUpRighthingejackInTheBoxrollInrollOutzoomInzoomInDownzoomInLeftzoomInRightzoomInUpzoomOutzoomOutDownzoomOutLeftzoomOutRightzoomOutUpslideInDownslideInLeftslideInRightslideInUpslideOutDownslideOutLeftslideOutRightslideOutUpheartBeat大功告成,刷新页面即可查看动画效果。基本用法就是这些官方还给出了一些进阶用法如下二、进阶用法动态调用动画的Javascript例子function animateCss(element, animationName, callback) { const node = document.querySelector(element) node.classList.add(‘animated’, animationName) function handleAnimationEnd() { node.classList.remove(‘animated’, animationName) node.removeEventListener(‘animationend’, handleAnimationEnd) if (typeof callback === ‘function’) callback() } node.addEventListener(‘animationend’, handleAnimationEnd)}三、在官方例子基础上,稍加修改以后由于官方例子用的是querySelector,故只会选中第一个符合要求的元素。并且持续时间只有slow(2s)、slower(3s)、fast(800ms)、faster(500ms)故我稍加修改,依然用的原生JS语法(部分ES6)其中选择器element改为选中所有符合要求的元素新增times参数,可以是2000ms或者2s/** * element: 选择器 例如 #id | .class | div * animationName: 动画名称 参考animate.css官网 例如fadeIn * times: 持续时间 例如 200ms | 2s * callback: 回调函数 */function animateCss(element, animationName,times, callback) { const nodes = document.querySelectorAll(element) nodes.forEach((node => { if(times) node.style.setProperty(‘animation-duration’, times, ‘important’); node.classList.add(‘animated’, animationName) function handleAnimationEnd() { node.classList.remove(‘animated’, animationName) node.removeEventListener(‘animationend’, handleAnimationEnd) if (typeof callback === ‘function’) callback() } node.addEventListener(‘animationend’, handleAnimationEnd) }))}例子animateCss(’.post’, ‘pulse’);animateCss(’.post’, ‘pulse’,‘200ms’);animateCss(’.post’, ‘pulse’,‘200ms’,function(){//do something});Animate.css官网https://daneden.github.io/animate.css/https://github.com/daneden/animate.css另外本篇文章也发表在了我的个人主页,欢迎来查看https://zzzmh.cn/single?id=59 ...

January 23, 2019 · 1 min · jiezi

css动画实现loading效果

css animation 动画实现loading状态HTML<div class=“root”></div> CSS.root { width: 25px; height: 25px; border-radius: 50px; border: 2px dashed #ddeeff; animation: loading 1s infinite linear;}@keyframes loading { to { transform: rotate(180deg) }}JavaScriptsetTimeout(() => { $(’.root’).css({ ‘animation-play-state’: ‘paused’ }) }, 5000)

January 19, 2019 · 1 min · jiezi

JavaScript是如何工作的: CSS 和 JS 动画底层原理及如何优化它们的性能

JavaScript 是如何工作的:深入网络层 + 如何优化性能和安全这是专门探索 JavaScript 及其所构建的组件的系列文章的第 13 篇。如果你错过了前面的章节,可以在这里找到它们:JavaScript 是如何工作的:引擎,运行时和调用堆栈的概述!JavaScript 是如何工作的:深入V8引擎&编写优化代码的5个技巧!JavaScript 是如何工作的:内存管理+如何处理4个常见的内存泄漏 !JavaScript 是如何工作的:事件循环和异步编程的崛起+ 5种使用 async/await 更好地编码方式!JavaScript 是如何工作的:深入探索 websocket 和HTTP/2与SSE +如何选择正确的路径!JavaScript 是如何工作的:与 WebAssembly比较 及其使用场景 !JavaScript 是如何工作的:Web Workers的构建块+ 5个使用他们的场景!JavaScript 是如何工作的:Service Worker 的生命周期及使用场景!JavaScript 是如何工作的:Web 推送通知的机制!JavaScript是如何工作的:使用 MutationObserver 跟踪 DOM 的变化!JavaScript是如何工作的:渲染引擎和优化其性能的技巧!JavaScript是如何工作的:深入网络层 + 如何优化性能和安全!概述你肯定知道,动画在创建引人注目的 Web 应用程序中扮演着重要的角色。随着用户越来越多地将注意力转移到用户体验上,商户开始意识到完美、愉快的用户体验的重要性,结果 Web 应用程序变得越来越重,并具有更动态交互的 UI。这一切都需要更复杂的动画,以便用户在整个过程中更平稳地进行状态转换。今天,这甚至不被认为是什么特别的事情。用户正变得越来越挑剔,默认情况下,他们期望的是具有高响应性和交互性的用户界面。然而,界面的动画化并不一定是简单的。什么是动画,什么时候该用动画,动画应该有什么样的视频效果,这些都是棘手的问题。JavaScript 和 CSS 动画比较创建 Web 动画的两种主要方法是使用JavaScript和 CSS。选择哪种没有对或错,这完全取决于你想要达到的效果。CSS 动画用CSS制作动画是让元素在屏幕上移动的最简单方法。这里将从如何让元素在 X 和 Y 轴上移动 50px 简单示例开始,通过持续 1 秒的 CSS 过渡来移动元素。.box { -webkit-transform: translate(0, 0); -webkit-transition: -webkit-transform 1000ms; transform: translate(0, 0); transition: transform 1000ms;}.box.move { -webkit-transform: translate(50px, 50px); transform: translate(50px, 50px);}当元素加上 move 类时,改变 transform 的值然后开发发生过渡效果。除了转换持续时间外,还有 easing 属性,这实际上就是动画的运动速度方式,该参数会在之后详细介绍。如果像上面的代码片段一样,创建单独的 CSS 类来实现动画,当然也可以使用 JavaScript 来切换每个动画。如下元素:div class=“box”> Sample content.</div>然后,使用 JavaScript 来切换每个动画。var boxElements = document.getElementsByClassName(‘box’), boxElementsLength = boxElements.length, i;for (i = 0; i < boxElementsLength; i++) { boxElements[i].classList.add(‘move’);}上面的代码片段是为所有包含 box 类的元素为其添加 move 类以触发动画。这样做可以为你的应用提供良好的平衡。 你可以专注于使用 JavaScript 管理状态,只需在目标元素上设置适当的类,让浏览器处理动画。 如果沿着这条路线前进,你可以在元素上监听 transitionend 事件,但前提是放弃旧版 Internet Explorer 的支持:监听 transitionend 触发的事件如下所示:var boxElement = document.querySelector(’.box’);boxElement.addEventListener(’transitionend’, onTransitionEnd, false);function onTransitionEnd() { // Handle the transition finishing.}除了使用 CSS 过渡之外,你还可以使用 CSS 动画,CSS 动画可以让你更好地控制单独的动画关键帧,持续时间以及循环次数。关键帧用于指示浏览器 CSS 属性在给定时间点上应有的 CSS 属性,然后填充空白。来个简单的例子:.box { /* 动画的名字 / animation-name: movingBox; / 动画的持续时间 / animation-duration: 2300ms; / 动画的运行次数 / animation-iteration-count: infinite; / 设置对象动画在循环中是否反向运动的方法 */ animation-direction: alternate;}@keyframes movingBox { 0% { transform: translate(0, 0); opacity: 0.4; } 25% { opacity: 0.9; } 50% { transform: translate(150px, 200px); opacity: 0.2; } 100% { transform: translate(40px, 30px); opacity: 0.8; }}效果示例: https://sessionstack.github.i…使用CSS动画,你可以独立于目标元素定义动画本身,并使用 animation-name 属性来选择所需的动画。CSS 动画在某种程度仍然需要加浏览器前缀的,在 Safari、Safari Mobile 和 Android 中都使用了 -webkit。 Chrome、 Opera、Internet Explorer 和 Firefox 都不需要添加前缀。许多工具可以帮助你创建所需 CSS 的前缀,这样就不需要在源文件中带样式前缀。JavaScript 动画和 CSS 过渡或者 CSS 动画相比,使用 JavaScript 创建动画更加复杂,但它通常为开发人员提供了更强大的功能。JavaScript 动画是作为代码的一部分内联编写的。你还可以将它们封装在其他对象中。以下为用 JavaScript 来实现最开始的 CSS 过渡的代码:var boxElement = document.querySelector(’.box’);var animation = boxElement.animate([ {transform: ’translate(0)’}, {transform: ’translate(150px, 200px)’}])animation.addEventListener(‘finish’, function() { boxElement.style.transform = ’translate(150px, 200px)’;})默认情况下,Web 动画仅修改元素的展示效果。 如果要将对象停留在移动后的位置,则应在动画完成时修改其基础样式。 这就是为什么在上面的例子中监听 finish 事件,并将 box.style.transform 属性设置为 translate(150px, 200px),该属性值和 CSS 动画执行的第二个样式转换是一样的。使用 JavaScript 动画,你可以在每一步完全控制元素的样式。 这意味着你可以放慢动画速度,暂停动画,停止它们,翻转它们,并根据需要操纵元素。 如果你正在构建复杂的面向对象的应用程序,这尤其有用,因为你可以正确地封装你想要的动画行为。Easing 定义自然过渡效果会让你的用户对你的 Web 应用程序感觉更舒服,从而带来更好的用户体验。当然,没有任何东西从一个点到另一个点线性移动。 实际上,当事物在我们周围的物理世界中移动时,事物往往会加速或减速,因为我们不是在真空中,并且有不同的因素会影响这一点。 人类的大脑会期望感受这样的移动,所以当为网络应用制作动画的时候,利用此类知识会对自己会有好处。以下是一些术语需要了解一下:ease in — 相对于匀速,开始的时候慢,之后快ease out — 相对于匀速,开始时快,结束时候间慢ease-in-out — 相对于匀速,开始和结束都慢)两头慢Easing 关键字CSS 过渡和动画允许你选择要使用的 easing 类型。 不同的关键字会影响动画的 easing,你也可以完全自定义 easing 方法。以下为可以选择用来控制 easing 的 CSS 关键字:linearease-inease-outease-in-out让我们深入来了解一下这几个兄弟,并看它们各自展示的效果是怎么样。Linear 动画easing 方法的的默认为 linear,以下为 linear 过渡效果的图示:随着时间增加,值等比增加,使用 linear 动效,会让动画不自然,一般来说,避免使用 linear 动效。以下是如何实现简单的线性动画:transition: transform 500ms linear;Ease-out 动画如前所述,与线性动画相比,easing out 动画开始时快,结束时候间慢,过渡效果的图示如下:一般来说,easing out过渡效果是最适合做界面体验的,因为快速地启动会给人以快速响应的动画的感觉,而结束时让人感觉很平滑这得归功于不一致的移动速度。有很多方法可以实现 ease-out 效果,但最简单的是 CSS 中的 ease-out 关键字:transition: transform 500ms ease-out;Ease-in 动画和 ease-out 动画相反-开始时快,结束时候间慢,过渡效果图如下:与 ease-out 动画相比, ease-in 可能会让人感到不寻常,由于启动缓慢给人以反应卡顿的感觉,因此会产生一种无反应的感觉。 动画结束很快也会产生一种奇怪的感觉,因为整个动画正在加速,而现实世界中的物体在突然停止时往往会减速。和 ease-out 和 linear 动画类似,使用 CSS 关键字来实现 ease-in 动画:transition: transform 500ms ease-in;Ease-in-out 动画该动画为 ease-in 和 ease-out 的合集,过渡效果图如下:不要使用太长的动画持续时间,因为它们会让你的 UI 感觉没有响应。用 ease-in-out CSS 关键字来实现 ease-in-out 动画:transition: transform 500ms ease-in-out;自定义 easing你也可以定义自己的 easing 曲线,这可以更好地创建自己想要的动画效果。实际上, ease-in,linear 及 ease 关键字映射到预定义 贝塞尔曲线 ,可以在 CSS transitions specification 和 Web Animations specification 中查找更多关于贝塞尔曲线的内容。贝塞尔曲线 (Bézier curves)Bézier curve(贝塞尔曲线)是应用于二维图形应用程序的数学曲线。 曲线定义:起始点、终止点(也称锚点)、控制点。通过调整控制点,贝塞尔曲线的形状会发生变化。 1962年,法国数学家Pierre Bézier第一个研究了这种矢量绘制曲线的方法,并给出了详细的计算公式,因此按照这样的公式绘制出来的曲线就用他的姓氏来命名,称为贝塞尔曲线。CSS3 transition-timing-function 属性,其语法如下:transition-timing-function: linear|ease|ease-in|ease-out|ease-in-out|cubic-bezier(n,n,n,n);总而言之可以用cubic-bezier(n,n,n,n)的形式来表示全部的属性值,这里就涉及到贝塞尔曲线(Bézier curve)。让我们看看贝塞尔曲线的工作原理。 贝塞尔曲线需要四个值,或者更准确地说它需要两对数字。 每对描述立方贝塞尔曲线控制点的 X 和 Y 坐标。贝塞尔曲线的起点有一个坐标 (0, 0) ,结束坐标是 (1, 1)。 你可以设置两个对号,两个控制点的 X 值必须在 [0,1] 范围内,并且每个控制点的 Y 值可以超过 [0,1] 限制,尽管规定不清楚多少。即使每个控制点的 X 和 Y 值稍有变化,也会得到完全不同的曲线。让我们看两张贝塞尔曲线的图,两张图相近但坐标的控制结点却不同。和如您所见,两张图有很大的不同, 第一个控制点矢量差为 (0.045,0.183) 矢量差,而第二控制点矢量差为 (-0.427, -0.054) 。第二条曲线的样式为:transition: transform 500ms cubic-bezier(0.465, 0.183, 0.153, 0.946);前两个数字是第一个控制点的 X 和 Y 坐标,后两个数字是第二个控制点的 X 和 Y 坐标。性能优化当你在使用动画的时候,你应该维持 60 帧每秒,否则会影响用户体验。和世界上的其他事物一样,动画也会有性能的开销。一些属性的动画性能开销相比其它属性要小。例如,为元素的 width 和 height 做动画会更改其几何结构并且可能会造成页面上的其它元素移动或者大小的改变,这个过程称为布局。我们在之前的一篇文章 中更详细地讨论了布局和渲染。通常,你应该避免动画触发布局或重绘的属性。 对于大多数现代浏览器,这意味着把动画局限于 opacity 和 transform 属性。Will-change你可以使用 will-change 知浏览器你打算更改元素的属性,这允许浏览器在进行更改之前进行最适当的优化。但是,不要过度使用 will-change,因为这样做会导致浏览器浪费资源,从而导致更多的性能问题。will-change 用法如下:.box { will-change: transform, opacity;}该属性在 Chrome, Firefox,Opera 得到很好的兼容。JavaScript 动画和 CSS 动画该如果抉择根据 Google Developer,渲染线程分为 主线程 (main thread) 和 合成线程 (compositor thread)。如果 CSS 动画只是改变 transforms 和 opacity,这时整个 CSS 动画得以在 合成线程 完成(而JS动画则会在 主线程 执行,然后触发合成线程进行下一步操作),在 JS 执行一些昂贵的任务时,主线程繁忙,CSS 动画由于使用了合成线程可以保持流畅在许多情况下,也可以由合成线程来处理 transforms 和 opacity 属性值的更改。对于帧速表现不好的低版本浏览器,CSS3可以做到自然降级,而JS则需要撰写额外代码。CSS动画有天然事件支持(TransitionEnd、AnimationEnd,但是它们都需要针对浏览器加前缀),JS则需要自己写事件。如果有任何动画触发绘画,布局或两者,则需要 “主线程” 才能完成工作。 这对于基于 CSS 和 JavaScript 的动画都是如此,布局或绘制的开销可能会使与 CSS 或 JavaScript 执行相关的任何工作相形见绌,这使得问题没有实际意义。CSS3有兼容性问题,而JS大多时候没有兼容性问题。总结如果动画只是简单的状态切换,不需要中间过程控制,在这种情况下,css 动画是优选方案。它可以让你将动画逻辑放在样式文件里面,而不会让你的页面充斥 Javascript 库。然而如果你在设计很复杂的富客户端界面或者在开发一个有着复杂 UI 状态的 APP。那么你应该使用 js 动画,这样你的动画可以保持高效,并且你的工作流也更可控。所以,在实现一些小的交互动效的时候,就多考虑考虑 CSS 动画。对于一些复杂控制的动画,使用 javascript 比较可靠。原文:https://blog.sessionstack.com…代码部署后可能存在的BUG没法实时知道,事后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给大家推荐一个好用的BUG监控工具 Fundebug。你的点赞是我持续分享好东西的动力,欢迎点赞!一个笨笨的码农,我的世界只能终身学习!更多内容请关注公众号《大迁世界》! ...

January 17, 2019 · 3 min · jiezi

css实现边框动画

1、css实现边框动画效果如图:<style> body,div{ margin: 0; padding: 0; box-sizing: border-box; } .box{ width: 300px; height: 200px; padding: 20px; margin: 50px auto; color: #fff; background-color: #f60; } .border-box{ position: relative; width: 100%; height: 100%; padding: 20px; } .border-box::before, .border-box::after{ position: absolute; content: ’ ‘; width: 0; height: 0; border: 1px solid transparent; box-sizing: border-box; } .border-box::before{ top: 0; left: 0; /* 鼠标离开后的回退效果,如果不设置回退效果,则鼠标离开后就直接很生硬的隐藏了 */ transition: border-color 0s ease-in 0.8s,width 0.2s ease-in 0.6s,height 0.2s ease-in 0.4s; } .border-box::after{ bottom: 0; right: 0; transition: border-color 0s ease-in 0.4s,width 0.2s ease-in 0.2s,height 0.2s ease-in; } .border-box:hover:before, .border-box:hover:after{ width: 100%; height: 100%; } .border-box:hover::after{ border-bottom-color: #fff; border-left-color: #fff; transition: border-color 0s ease-out 0.4s,width 0.2s ease-out 0.4s,height 0.2s ease-out 0.6s; } .border-box:hover::before{ border-top-color: #fff; border-right-color: #fff; transition: width 0.2s ease-out,height 0.2s ease-out 0.2s; }</style><body> <div class=“box”> <div class=“border-box”>hover查看效果</div> </div></body> ...

January 8, 2019 · 1 min · jiezi

3D文本悬停改变效果

html<h1 class=“hover-text-3d” data-text=“w3cbest.com”>W3CBEST.COM</h1>css.hover-text-3d { font-size: 7em;}.hover-text-3d { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); -o-transform: translate(-50%, -50%); transform: translate(-50%, -50%); -webkit-text-fill-color: #fff; text-fill-color: #fff;}.hover-text-3d:before { position: absolute; overflow: hidden; width: 0; content: attr(data-text); -webkit-transition: 2s; -o-transition: 2s; transition: 2s; -webkit-text-fill-color: #aaf0d1; text-fill-color: #aaf0d1; -webkit-text-stroke-width: 2px; text-stroke-width: 2px; -webkit-text-stroke-color: #aaf0d1; text-stroke-color: #aaf0d1; -webkit-filter: drop-shadow(5px 5px 5px rgba(0, 0, 0, .8)); -o-filter: drop-shadow(5px 5px 5px rgba(0, 0, 0, .8)); filter: drop-shadow(5px 5px 5px rgba(0, 0, 0, .8));}.hover-text-3d:hover:before { width: 100%;}查看效果 ...

January 3, 2019 · 1 min · jiezi

CSS动画指南(第1部分)

开始这篇文章之前,假设你从来没有创建过 CSS 动画。但即使你有,也可能有一些内容你之前没有注意到。接着,我们将探索创建的第一个动画,例如链接动画。CSS 动画看上去可能是一个快速就能用起来,但一旦真正深入了解,它是一个很大主题。因此,本文分为多个部分。第1部分:介绍CSS动画,CSS 动画在性能上如何,通过创建一个基本的动画,讲解 @keyframes 组成。第2部分:掌握了基础知识之后,我们将深入研究 animation 属性可以做的不同事情。这包括一些技巧,如使用 fill-mode 和链接动画。第3部分:我们总结了一些比较热门的主题,比如在 JavaScript 使用CSS变量的一些钩子 。还会讨论了是否应该使用 CSS 动画。没错,有些时候使用 CSS 动画 并不总是最好的选择。但是理解这些基础和一些替代方案是有好处的。开始之前所有代码都可 这里 获取并且调试,还可以通过 Github 上的获取到源代码。对于所有动画,我们使用一个div元素,除非另有说明。基本标记包括以下内容本指南的目标是让你轻松从头开始创建自己的CSS动画。那么,为什么需要动画?在我们开始创建动画之前,需要知道我们可以用哪些动画属性。我们不能让每个属性都动起来。下面的 MDN 的一篇文章,其中列出了动画化有哪些的属性:Animatable CSS propertiesLea Verou也有一个很好的动画属性演示页面:Animatable: One property, two values, endless possibilities性能在动画的属性中,可以根据性能选择一些动画的属性。例如,使用 transform 可以更好地处理动画元素位置,这个属性都会开启 GPU 硬件加速模式,从而让浏览器在渲染动画时从 CPU 转向 GPU。 相反,动画其它一些属性会降低性能。下面的文章对理解动画性能很有帮助:High Performance Animations第一个动画我们第一个动画是使一个元素旋转360度,效果如下:首先,我们使用 @keyframes 规则创建动画,@keyframes 采用以下结构:animation-name 是我们给动画起的名字,你可以有一个或多个关键帧选择器。首先取一个动画名为 spin ,然后 @keyframes 来创建动画,from 里面放的是动画开始(0%)的样式,to 里面放的是动画结束(100%)的样式,然后把动画绑定到选择器。我们可以更进一步的优化。from 下的样式对我们的起始的样式没有影响。因此,from 是冗余的,这里可以移除它:现在,我们需要将动画应用到元素上。我们使用动画属性 animation-name 和 animation-duration如上是告诉 div 使用动画 spin,持续时间为2秒。持续时间可以设置为毫秒(ms)或秒(s)。Animations inspector我们已经创建了第一个动画。可以在谷歌 Chrome 中引入 Animations inspector 。打开谷歌的开发工具接着通过 More Tools 选项选择 Animations 面板。如果动画面板显示 “Listening for animations…”,则刷新页面。刷新后,应该会在动画面板中看到一些东西,那就是我们的动画!点击动画,我们可以检查它。由于我们现在的动画不是特别复杂,没有太多可检查。但是有了Animations inspector ,我们可以做很多事情。我们可以试验持续时间和延迟以及改变回放速度。最重要的是,我们可以回放动画,而不必刷新整个页面当我们有很多动画的时候,这变得特别有用。无论是对不同的元素还是对一个元素。可以在下面这篇文章中阅读更多关于 Animations inspector 的内容。https://developers.google.com…这系列文章中,作者建议在演示时动画时使用 Animations inspector。这允许你回放动画和调整他们而无需重新加载页面。@keyframes在上列中动画 spin 下,我们使用第一个 @keyframes 规则。上例中 @keyframes 并没有太多内容。在指定动画名称之后,我们在关键帧选择器中指定动画。关键帧选择器指定动画持续时间的百分比。或者,如前所述,我们可以使用 from 和 to 关键字,它们分别等于 0% 和 100%。每个选择器都定义了在动画的哪个时间点应该应用的哪个样式。如果我们有指定相同C SS 样式的选择器,我们可以将它们组合在一起。让我们从一个简单的例子开始。考虑元素在正方形路径上移动的效果。我们将把动画命名为 squarePath,这非常有创意 ????对于本例,元素有四个位置。对于正方形的每一边,我们使用四分之一的动画。因为我们的开始和结束位置是一样的,所以我们可以对这些关键帧选择器进行分组给元素加上动画及时间这个就是有一个元素沿着正方形的路径移动。小结:我们已经了解了创建和应用动画元素的基本知识。我们还可以检查我们的动画并在浏览器中调整它们。以下是先列出常用的属性,后续文章会介绍:animation-name:规定 @keyframes 动画的名称animation-duration:规定动画完成一个周期所花费的秒或毫秒。默认是 0animation-timing-function:规定动画的速度曲线。默认是 “ease"animation-delay:规定动画何时开始。默认是 0animation-iteration-count:规定动画被播放的次数。默认是 1animation-direction:规定动画是否在下一周期逆向地播放。默认是 “normal"animation-play-state:规定动画是否正在运行或暂停。默认是 “running"animation-fill-mode:规定对象动画时间之外的状态虽然这将足以让你启动和运行 CSS 动画,但还远的不够,在下一篇中,将深入研究动画的应用以及相关的技巧和技巧。原文:https://codeburst.io/a-guide-…你的点赞是我持续分享好东西的动力,欢迎点赞!一个笨笨的码农,我的世界只能终身学习!更多内容请关注公众号《大迁世界》! ...

December 28, 2018 · 1 min · jiezi

10分钟入门 CSS3 Animation

简介Animation可以让你不用依赖javascript或jquery,用纯CSS在网页中轻松实现各种动画效果。兼容性animation在绝大部分主流浏览器都得到了很好的支持!还在兼容IE9的同学要谨慎使用。CSS 坐标系在了解animtion之前,我们有必要先了解css的坐标系,因为很多的animation效果都和元素的坐标密切相关。在css3中网页不再是一个二维平面,而是一个三维空间,水平方向、竖直方向和垂直屏幕方向分别对应三维坐标系的x,y,z轴,如下图所示。箭头方向为正向,反之为负向(注意y轴方向与常规笛卡尔坐标系相反)。Animations1. Transformstransform中文译为“转换”,但我更倾向于称呼它“变形”(大名鼎鼎的变形金刚叫transformer)。我们可以使用transform function使html元素产生各种各样的变形,比如平移、缩放、旋转、扭曲等,而且不会影响正常的文档流(document flow)。(1) TranslateTranslate一般译为“翻译”,但在css里面一般用作“平移”,因为translate用于改变html元素的在3d坐标系位置。translate支持在坐标系内任意方向移动(通过任意组合x、y、z方向的向量),单位可以是长度单位和百分比(百分比是相对于被平移的元素自身尺寸,x轴是相对于width,y轴是相对于height,而在z轴方向由于元素是没有‘厚度’的,所以对于z方向不能用百分比表示),例如:transform: translateX(100px) translateY(50%) translateZ(-100px);// 或者简写transform: translate3d(100px, 50%, 2em);注意:translate是xy平面内的2维平移,translate3d是xyz空间内的三维平移;translate也可以单独书写,如 translate: 50% 105px 5rem;,但是该特性尚在实验阶段,很多浏览器不支持,所以现阶段还是配合transform使用。详情参考 MDN translate。(2) ScaleScale意为“缩放”,顾名思义,是用于改变元素的大小。例如:transform: scaleX(2) scaleY(0.5) scaleZ(1);// 或者简写transform: scale3d(2, 0.5, 1);scale方法接收任意数字(正负整数、小数、0)作为参数,该参数为缩放系数,系数>1 效果为放大,0<系数<1 效果为缩小,系数=0 元素尺寸变为无限小而不可见,系数<0 效果为 >0 时的镜像(具体效果可自己实验)。与translate类似,scale也有2维 scale() 和三维 scale3d()之分,也可以单独书写,此处不赘述。(3) RotateRotate意为“旋转”,支持将元素以x、y、z为轴旋转,旋转正方向为面朝坐标轴正向逆时针方向,可参考上面坐标系示意图。rotate方法接收一个角度作为参数,角度>0 正向旋转,角度<0 负向旋转,例如:// 默认绕z轴旋转transform: rotate(90deg);transform: rotateX(30deg) rotateY(60deg) rotateZ(-90deg);与translate和scale不同,rotate不能简写为transform: rotate3d(30deg, 60deg, 90deg)的形式,rotate3d的用法为:前三个参数为数字,代表x、y、z方向的向量,相加得到向量v,第四个参数为角度,表示以向量v为轴逆时针旋转的角度,语法如下:transform: rotate3d(1, 2, 3, 90deg);与translate和scale类似,rotate也可以作为单独的css属性,但还在实验阶段。出于篇幅考虑,我只列出三种最常用的tranform function,剩下的transform function请参考 MDN transform function。(4) 组合我们可以将不同的transform方法组合起来使用,如:transform: translateY(200px) rotateZ(90deg) scale(3);组合方法的执行顺序是从右往左,即先执行scale,然后rotate,最后translate,产生的效果是逐次累加的。方法书写的顺序对最后效果有很大的影响,看下面例子,沿y轴平移和放大,顺序不同,产生的结果有明显差别:如果先translate再scale,平移的距离也将被等比例缩放,而先scale再translate则不会。所以在使用transform需按照 translate -> rotate -> scale 的顺序书写,让scale先执行,以免放大translate的效果,而rotate先translate执行以防止带着平移的距离一起转动。我觉得这是transform目前不方便的地方,因为方法之间相互干扰并不容易理解,书写顺序也不容易记住。在未来有望通过使用独立的css transform属性解决这一问题,因为独立的transform属性对书写顺序没有依赖,方法之间彼此不会干扰。TransitionTransition翻译为“过渡”,强调的是过程。在css中指在一段时间内,元素从一个状态(对应一个css属性)过渡到另一个状态的动态过程。我们可以决定以何种方式过渡过渡和花费多少时间。例如,我们把鼠标悬浮到云上面的时候使其变大一些可以这么写:.cloud{ width: 240px; transition: 1s;}.cloud:hover{ width: 320px;}效果:transition可以和transform结合使用,比如我们可以让云变大的同时转一圈:.cloud:hover{ width: 320px; transform: rotate(360deg);}效果:我们可以给不同的效果设置不同的过渡时间:.cloud{ width: 240px; transition: width 1s, transform 0.5s;}我们也可以给效果设置延时时间,比如我们等宽度增大之后再旋转:.cloud{ width: 240px; transition: width 1s, transform 0.5s 1s;}效果:我们还可以给每个效果设置不同的timing function,用于控制加速效果。比如我们可以让旋转的速度逐渐加快:.cloud{ width: 240px; transition: transform 2s ease-in;}.cloud:hover{ transform: rotate(1080deg);}效果:更多的timing function请后面会进一步讨论,也可以参考 MDN transition-timing-functionsKeyframes(1) 基本用法Keyframe中文译为“关键帧”,是animation中很强大的功能,通俗说就是我们可以通过定义一段动画中的关键点、关键状态来创建动画。Keyframes相比transition对动画过程和细节有更强的控制。我们先看一个例子(部分代码省略)html:<div class=“sky”></div><div class=“grass”></div><div class=“road”> <div class=“lines”></div> <img src=“http://lc-jOYHMCEn.cn-n1.lcfile.com/a3f630c957a4b32d0221.png" class=“mario animated”></div>css:.mario{ position: absolute; left: 0px; width: 100px;}.animated{ animation-name: drive; animation-duration: 1s; animation-timing-function: linear;}@keyframes drive { from{ transform: translateX(0) } to{ transform: translateX(700px) }}效果:其中 drive 是该keyframes的名称,from, to 是keyframes播放过程的时间起点和终点,时间点也可以用百分比表示,如50%,from, to 等价于 0%, 100%。每个时间点对应一个css状态,代表一个关键帧(keyframe)。keyframes定义完成后使用方法如下:animation也有简写形式,如:animation: slidein 3s ease-in 1s infinite reverse both running;但这种对书写顺序有一定要求(delay要写在duration后面,其他参数无顺序要求,css会通过传入的关键词识别)。(2) Animation Delay通过animation-delay,我们可以给动画延迟执行:animation-delay: 2s;(3) Animation Fill Modeforwards在上面的例子中可以看到马里奥运动到右边之后又回到了起点,如果我们想让他运动完成后就停留在右边呢?很简单,我们设置annimation fill mode就可以了:animation-fill-mode: forwardsforwards 表示动画完成后,元素将保持最后一帧的状态。backwards与之相对的还有 backwards,backwards表示并不是动画完成后元素变回第一帧的状态,而是表示当设置了animation-delay时,在动画开始前的等待过程中,立刻给元素应用第一帧的状态。我们将上面的例子稍作修改看一下效果:.animated{ animation-name: drive; animation-duration: 1s; animation-fill-mode: backwords; animation-delay: 1s; animation-timing-function: linear;}@keyframes drive { from{ transform: translateX(350px) } to{ transform: translateX(700px) scale(2) }}效果:可以看到,动画开始之前小人立马移动到350px处,1s之后才开始动画。both显而易见,both会同时应用forwards和backwards两种规则,即在delay时先应用第一帧的状态,结束时保持最后一帧的状态。(3) Animation Repeat我们可以通过 animation-iteration-count 设置动画循环播放的次数,比如:animation-iteration-count: 3;// 无限循环animation-iteration-count:infinite;就像这样:(4) Animation Directionnormal正常方向,也是默认方向,即先from,再to。reverse与正常方向相反,即先to,再from。例如:.animated{ … animation-direction: reverse;}@keyframes drive { from{ transform: translateX(-100px) rotateY(180deg) } to{ transform: translateX(862px) rotateY(180deg)}}效果:alternatealternate意为“交替”,即normal和reverse交替之行,先normal再reverse。reverse alternate反向交替,先reverse再normal。(4) Animation Timing function和transition一样,keyframes也可以设置timing function,常用的timing function归纳如下:ease:默认方法,初速度较慢,然后加速再减速ease-in:初速度最慢,然后一直加速ease-out:初速度最快,然后一直减速ease-in-out:初速度较慢,然后加速再减速,与ease的区别在于加速减速过程是对称的linear:恒速运动直观表现如下(codepen):除了上面现成的方法,我们可以通过贝塞尔曲线自定义速度曲线。我们可以在 http://cubic-bezier.com 可视化的创建我们自己的贝塞尔曲线。比如创建一个刚开始极慢,突然变得极快的曲线:css:animation-timing-function: cubic-bezier(1,.03,1,-0.03);效果:是不是挺神奇!(5) Chain Animation我们可以将多个animation串联使用,比如我们想让小人在行驶的过程中跳跃,可以这么写:css:.mario { … animation: drive 3s both infinite linear, jump 0.5s 1.2s ease-in-out infinite;}@keyframes jump { 0% { top: -40px; } 50% { top: -120px; } 100% { top: -40px; }}效果:实践本文目的在于普及css3 animation的基础,并未完全覆盖animation的知识,未涉及和讲解的知识请大家见谅 。 掌握上述知识后,我们就已经可以用animation做出丰富的动画效果了,下面列出一些codepen上的小例子:full mario demoanimated popupfly items to shopping cartflipping cards ...

December 25, 2018 · 2 min · jiezi

用css3实现ps蒙版效果+动画

说实话,css3越来越强大,使用css也可以做越来越多意想不到的效果。今天,见到有人用css3实现了ps的蒙版效果,如下所示:实现原理这个动画,其实也并不复杂。它使用background-clip实现了文字蒙版的效果,然后结合了背景图片的animation实现了如上图所示的文字蒙版动画。用css3做蒙版效果常见的有两种,一种是图形的,另一种是文字的。图形蒙版这里要使用的到时clip-path,它的作用是根据你指定的图形的轮廓来保留剩余的区域,如果你制定的图形是圆形,那么剩余的就是个圆形。它有个特点就是,你可以把你的盒子模型定义为不规则的图形。我们都知道原来的css只允许我们把元素定义成矩形和正方形,又可以在矩形区域内嵌套其他的矩形或者正方形元素。而现在我们可以定义一个不规则的图形,然后在这个不规则的图形内定义其他的元素。这样说可能还是有点儿含糊。假如你在一个元素内填充满文字,以前只能沿着矩形或者正方形的边缘填充,而现在使用clip-path可以使文字沿着不规则的图形的边缘填充。具体可参考文字环绕接着说图形蒙版,有两种实现:一种是保留剪切图形轮廓内的内容,可以参考shpape-masking。另一种使保留剪切图内之外的内容,可参考Reverse clip path。这个动画效果是由背景的gif和视频结合的,当然也可以使用css3的animation。文字蒙版文字蒙版使用的使css3中的backgorund-clip,这个属性支持border-box,padding-box,content-box和text等属性,具体使用可参考mdn background-clip。它和clip的效果类似,都是剪切后剩余部分的内容,text 这个属性值比较特殊,针对的是元素内的文字,其他的针对的是元素内内容的显示区域。背景的动画很简单就是一个animation动画。参考示例:Merry Christmas

December 19, 2018 · 1 min · jiezi

CSS实例:翻转图片、滚动图片栏、打开大门

CSS 翻转图片主要用到的技术除了3D翻转和定位 ,还用到了一个属性 backface-visibility:visable|hidden;该属性主要是用来设定元素背面是否可见。效果图如下:具体的步骤如下:1、写出页面主体,<div> <img src=“Images/b.jpg” alt=""> <img src=“Images/c.jpg” alt=""> </div>2、通过定位使两张图片叠加在一起div img { width: 250px; height: 170px; position: absolute; top: 0; left: 0; transition: all 1s; }3、设置第一张图片背面不可见div img:first-child { z-index: 1; backface-visibility: hidden; }4、添加旋转180度div:hover img { transform: rotateY(180deg); }完整代码:<!DOCTYPE html><html lang=“en”><head> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1.0”> <meta http-equiv=“X-UA-Compatible” content=“ie=edge”> <title>Document</title> <style> /* backface-visibility */ div { width: 250px; height: 170px; margin: 100px auto; position: relative; } div img { width: 250px; height: 170px; position: absolute; top: 0; left: 0; transition: all 1s; } div img:first-child { z-index: 1; backface-visibility: hidden; } div:hover img { transform: rotateY(180deg); } </style></head><body> <div> <img src=“Images/b.jpg” alt=""> <img src=“Images/c.jpg” alt=""> </div></body></html>更多项目实战视频,免费分享,加入我的前端学习qun:784783012 点击:加入CSS 滚动的图片栏网站上可以经常看到有一些图片进行持续不断的滚动,这个效果可以通过css的动画效果来实现。主要原理是通过动画向左移动。首先给出两组一样的图片(同一行上),让整体图片向左移动一组图片的长度,这样在动画结束时会迅速还原到原来位置,而此时正好与第二组图片交替,看起来就像是一组图片在不断循环向左滚动。具体步骤如下:1、设置主体代码各处两组一样的图片<nav> <ul> <li><img src=“Images/1 (2).jpg” alt=""></li> <li><img src=“Images/2 (2).jpg” alt=""></li> <li><img src=“Images/3 (2).jpg” alt=""></li> <li><img src=“Images/1 (2).jpg” alt=""></li> <li><img src=“Images/2 (2).jpg” alt=""></li> <li><img src=“Images/3 (2).jpg” alt=""></li> </ul> </nav>2、设置nav的大小,宽度为一组图片相加的宽度,高度为图片的高度。 nav { width: 750px; height: 170px; border: 1px solid red; margin: 100px auto;}3、设置ul大小,宽度为nav的两倍,高度与nav相同,并指定动画相关属性ul { width: 200%; height: 100%; animation: picmove 5s linear infinite forwards; }4、定义动画,主要是向左移动一组图片的长度 @keyframes picmove { from { transform: translate(0); } to { transform: translate(-750px); } }5、增加鼠标悬停,动画暂停的效果ul:hover { animation-play-state: paused; }6、最后给nav 增加 overflow:hidden 使得超出的部分隐藏,这样整体一组滚动的图片栏就做好了完整代码如下:<!DOCTYPE html><html lang=“en”><head> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1.0”> <meta http-equiv=“X-UA-Compatible” content=“ie=edge”> <title>Document</title> <style> * { margin: 0; padding: 0; } ul { list-style: none; } nav { width: 750px; height: 170px; border: 1px solid red; margin: 100px auto; overflow: hidden; } ul { width: 200%; height: 100%; animation: picmove 5s linear infinite forwards; } @keyframes picmove { from { transform: translate(0); } to { transform: translate(-750px); } } img { width: 250px; height: 170px; float: left; } ul:hover { animation-play-state: paused; } </style></head><body> <nav> <ul> <li><img src=“Images/1 (2).jpg” alt=""></li> <li><img src=“Images/2 (2).jpg” alt=""></li> <li><img src=“Images/3 (2).jpg” alt=""></li> <li><img src=“Images/1 (2).jpg” alt=""></li> <li><img src=“Images/2 (2).jpg” alt=""></li> <li><img src=“Images/3 (2).jpg” alt=""></li> </ul> </nav></body></html>CSS 打开大门主要运用到了3D旋转和定位技术。效果如下:具体步骤如下:1、首先在页面主体加三个很简单的div标签: <div class=“door”> <div class=“door-l”></div> <div class=“door-r”></div> </div>2、给外层盒子(.door) 加上基本的属性、背景、视距以及相对定位(子盒子要用到绝对定位,所以父盒子最好 加上相对定位)。.door { width: 450px; height: 450px; border: 1px solid #000000; margin: 100px auto; background: url(Images/men.png) no-repeat; background-size: 100% 100%; position: relative; perspective: 1000px; }3、给左右的门设置相关属性,这里给出左盒子的 相关属性。右盒子只需将定位改为右边距离为0,以及将旋转轴改为右侧即可。.door-l { width: 50%; height: 100%; background-color: brown; position: absolute; top: 0; transition: all 0.5s; left: 0; border-right: 1px solid #000000; transform-origin: left; }4、添加门上的 小圆环,在这里是使用伪元素 before 进行添加的。 (1)、设置大小与边框 (2)、设置border-radius 为50% 让其变成圆形。 (3)、设置定位 垂直居中并靠里面有一定距离。.door-l::before { content: “”; border: 1px solid #000000; width: 20px; height: 20px; position: absolute; top: 50%; border-radius: 50%; transform: translateY(-50%); right: 5px; }5、最后设置旋转度数,我这里是设置了120度(注意度数的正负代表旋转方向).door:hover .door-l { transform: rotateY(-120deg); }完整代码如下:<!DOCTYPE html><html lang=“en”><head> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1.0”> <meta http-equiv=“X-UA-Compatible” content=“ie=edge”> <title>Document</title> <style> .door { width: 450px; height: 450px; border: 1px solid #000000; margin: 100px auto; background: url(Images/men.png) no-repeat; background-size: 100% 100%; position: relative; perspective: 1000px; } .door-l, .door-r { width: 50%; height: 100%; background-color: brown; position: absolute; top: 0; transition: all 0.5s; } .door-l { left: 0; border-right: 1px solid #000000; transform-origin: left; } .door-r { right: 0; border-left: 1px solid #000000; transform-origin: right; } .door-l::before, .door-r::before { content: “”; border: 1px solid #000000; width: 20px; height: 20px; position: absolute; top: 50%; border-radius: 50%; transform: translateY(-50%); } .door-l::before { right: 5px; } .door-r::before { left: 5px; } .door:hover .door-l { transform: rotateY(-120deg); } .door:hover .door-r { transform: rotateY(120deg); } </style></head><body> <div class=“door”> <div class=“door-l”></div> <div class=“door-r”></div> </div></body></html>更多项目实战视频,免费分享,加入我的前端学习qun:784783012 点击:加入 ...

December 2, 2018 · 3 min · jiezi

【译】只用 CSS 就能做到的像素画/像素动画

只用 CSS 就能做到的像素画/像素动画原文链接:box-shadowを使ってCSSだけでドット絵を描き、アニメーションさせる作者推特:bc_rikko作者的推特里面有不少例子,有能力的同学可以看一下这篇文章将会介绍只用 CSS 就能制作像素画·像素动画的方法。虽说纯 CSS 就能做到,但是为了更高的可维护性,也会顺便介绍使用 Sass 的制作方法。上面的马里奥和 Minecraft 方块都没有使用 JavaScript,单纯使用 CSS 动画制作。关于 box-shadow 属性绘制像素点可以借助 box-shadow 属性。原本 box-shadow 属性用于制作阴影效果,先介绍一下基本用法。该属性的写法有几种:box-shadow: offset-x offset-y colorbox-shadow: offset-x offset-y blur-radius colorbox-shadow: offset-x offset-y blur-radius spread-radius colorbox-shadow: inset offset-x offset-y coloroffset-x 和 offset-y 用于指定阴影偏移位置。以元素的左上角为原点,指定 XY 轴移动的位置。color 字面意思,指定阴影颜色。blur-radius 指定模糊效果的半径。跟 border-radius 差不多。spread-raduis 模糊范围的扩大与缩小。inset 关键字可以使阴影效果显示在元素内则。文字说明或许不够形象,我们可以直接看效果:https://jsfiddle.net/bc_rikko…实际效果如下,每个值会造成什么影响应该能很直观地看懂。基础:描绘一个像素点box-shadow 基础都明白了,就可以进入下一步:描绘一个像素点。对一个边长 100px 的正方形使用 box-shadow:<div class=“container”> <div class=“box”></div></div><style>* { /* 为了方便看到元素而添加的边框(不加也行) / box-sizing: border-box;}.container { / 长和宽包括 box-shadow / width: 200px; height: 200px;}.box { / 元素属性 / width: 100px; height: 100px; border: 2px solid #777; / 在元素右下角相同大小的方块 / box-shadow: 100px 100px rgba(7,7,7,.3);}</style>如图所示,使用 box-shadow 描绘了一个与元素相同大小的阴影。代码的意思是把一个 100px 的方形的影子放到 (100px, 100px) 的位置。进阶:用 box-shadow 属性绘制像素画完成预想图这两个都是 5✖️5 的像素画,我们先从左边开始:<div class=“container”> <div class=“pixel one”></div></div><style>.container { / 像素画的大小 / width: 100px; height: 100px;}.pixel { / 使伪元素的位置可调整 / position: relative;}.pixel::before { content: “”; / 一个点的大小(例:20px x 20px) / width: 20px; height: 20px; / box-shadow 着色,伪元素设为透明 / background-color: transparent; / 调整伪元素位置,让左上角成为(0,0) / position: absolute; top: -20px; left: -20px;}.pixel.one::before { box-shadow: / 列 行 色 / / 第1列 / 20px 20px #FB0600, 20px 40px #FC322F, 20px 60px #FC6663, 20px 80px #FD9999, 20px 100px #FECCCB, / 第2列 / 40px 20px #60169F, 40px 40px #7A23B0, 40px 60px #964DC2, 40px 80px #B681D9, 40px 100px #D8BEED, / 第3列 / 60px 20px #1388BC, 60px 40px #269DC9, 60px 60px #55B3D7, 60px 80px #88CAE2, 60px 100px #BFE3EF, / 第4列 / 80px 20px #ACD902, 80px 40px #BDE02D, 80px 60px #CDEA5E, 80px 80px #DBEF8E, 80px 100px #F4FBC8, / 第5列 / 100px 20px #FB8F02, 100px 40px #FDA533, 100px 60px #FDBB64, 100px 80px #FED39A, 100px 100px #FDE8C9;}</style>首先,box-shadow 生产的影子大小不包括本体元素的大小,container 类的大小设为像素画完成后的大小就行。接着,box-shadow 的影子大小由,pixel 类的大小决定,所以把 width 和 height设定为 20px。实际的点是 before 伪元素绘制的,pixel 的 20px 正方形会在左上角留下空位,为此可以使用 position: absolute 调整。最后使用 box-shadow 逐格绘制像素画。接着实现右边的像素画。.pixel.two::before { box-shadow: 20px 20px #704b16, 40px 20px #704b16, 60px 20px #704b16, 80px 20px #704b16, 100px 20px #704b16, 20px 40px #704b16, 40px 40px #fdb778, 60px 40px #fdb778, 80px 40px #fdb778, 100px 40px #704b16, 20px 60px #fdb778, 40px 60px #333333, 60px 60px #fdb778, 80px 60px #333333, 100px 60px #fdb778, 20px 80px #fdb778, 40px 80px #fdb778, 60px 80px #fdb778, 80px 80px #fdb778, 100px 80px #fdb778, 20px 100px #fdb778, 40px 100px #c70300, 60px 100px #c70300, 80px 100px #c70300, 100px 100px #fdb778;}应用:使用 Sass 编写可维护像素画上面写的几个例子,至少我是没什么信心去维护好他们。5x5 的像素画要写 25 次属性值,一般的 16x16 则是多达 256 个值。所以,我们可以使用 Sass 编写可维护像素画。Sass 环境搭建可以参考以下文章(日语)https://kuroeveryday.blogspot…Sass 使用 mixin(function 亦可)生成样式的方法:@mixin pixelize($matrix, $size, $colors) { $ret: “”; @for $i from 1 through length($matrix) { $row: nth($matrix, $i); @for $j from 1 through length($row) { $dot: nth($row, $j); @if $dot != 0 { @if $ret != "" { $ret: $ret + “,”; } $color: nth($colors, $dot); $ret: $ret + ($j * $size) + " " + ($i * $size) + " " + $color; } } } box-shadow: unquote($ret + “;”);}$heart-colors: (#333, #f11416, #831200);$heart: ( (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), (0,0,1,1,1,0,0,0,0,0,1,1,1,0,0,0), (0,1,2,2,2,1,0,0,0,1,2,2,3,1,0,0), (1,2,0,0,2,2,1,0,1,2,2,2,2,3,1,0), (1,2,0,2,2,2,2,1,2,2,2,2,2,3,1,0), (1,2,2,2,2,2,2,2,2,2,2,2,2,3,1,0), (1,2,2,2,2,2,2,2,2,2,2,2,2,3,1,0), (1,2,2,2,2,2,2,2,2,2,2,2,2,3,1,0), (0,1,2,2,2,2,2,2,2,2,2,2,3,1,0,0), (0,0,1,2,2,2,2,2,2,2,2,3,1,0,0,0), (0,0,0,1,2,2,2,2,2,2,3,1,0,0,0,0), (0,0,0,0,1,2,2,2,2,3,1,0,0,0,0,0), (0,0,0,0,0,1,2,2,3,1,0,0,0,0,0,0), (0,0,0,0,0,0,1,3,1,0,0,0,0,0,0,0), (0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0), (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0));.icon { width: 20px; height: 20px; @include pixelize($heart, 20px, $heart-colors);}定义名为 pixelize 的 mixin,把像素画的矩阵($heart)像素点的大小(20px)颜色列表($hearts-colors)传入其中,即可生成 box-shadow 属性。像素画的矩阵用数字 0~N 表示,0 为透明,1~n 为颜色列表对应颜色。如果有代码高亮的话,像素画的图案就一目了然啦。与原生 CSS 相比,这样简单多了吧?如果这样都觉得麻烦,可以使用 CSS 像素画生成器~CSSドット絵ジェネレータ番外篇:制作像素动画之前 icon 类直接使用 box-shadow 属性绘制像素画,在制作像素动画时,需要使用 CSS animation。.mario { width: 8px; height: 8px; animation: jump 1s infinite, sprite 1s infinite;}/ 跳跃动作(上下移動) /@keyframes jump { from, 25%, 75%, to { transform: translateY(0); } 50% { transform: translateY(calc(8px * -8)); }}/ 普通状态和跳跃状态的像素画 /@keyframes sprite { / 对比 animation-timing-function: steps(n) * 使用百分比可以更细致的调整动画时间 / from, 24%, 76%, to { box-shadow: / 普通状态的像素画 / } 25%, 75% { box-shadow: / 跳跃状态的像素画 */ }}使用 CSS 动画修改 box-shadow 和元素的位置,看起来就像是跳起来一样。详细代码可以在 github 仓库中了解https://github.com/BcRikko/cs… ...

November 29, 2018 · 3 min · jiezi

CSS3 入门详解(二)

动画动画是CSS3中具有颠覆性的特征之一,可通过设置多个节点来精确控制一个或一组动画,常用来实现复杂的动画效果。11.1 如何实现动画必要元素:a、通过@keyframes指定动画序列;b、通过百分比将动画序列分割成多个节点;c、在各节点中分别定义各属性d、通过animation将动画应用于相应元素;示例代码: 两种定义动画的方式<style> /* move 是定义的动画名 / @keyframes move { 0% { transform: translateX(0px); width: 200px; height: 200px; } 40% { width: 300px; height: 300px; } 80% { width: 200px; height: 200px; transform: translateX(100px); } 100% { transform: translateX(0px); width: 200px; height: 200px; } } / 定义动画集 / @keyframes move1 { from { / 动画的开始状态 / / 位置开始状态 / transform: translateX(0px); } to { / 动画的结束状态 / / 位置结束状态 / transform: translateX(300px); } } .box { width: 200px; height: 200px; background-color: pink; / 调用动画集 动画名 持续时间 执行速度 / animation: move 5s linear; }</style>11.2 动画关键属性关键属性a、animation-name设置动画序列名称b、animation-duration动画持续时间c、animation-delay动画延时时间d、animation-timing-function动画执行速度,linear、ease等e、animation-play-state动画播放状态,running、paused等f、animation-direction动画逆播,alternate等g、animation-fill-mode动画执行完毕后状态,forwards、backwards等h、animation-iteration-count动画执行次数,inifinate等i、steps(60)表示动画分成60步完成参数值的顺序:关于几个值,除了名字,动画时间,延时有严格顺序要求其它随意。示例代码:<style type=“text/css”> .box { width: 200px; height: 200px; background-color: pink; / 调用动画集 / animation: move 5s linear; } / 定义动画集 / @keyframes move { 0% { / 位置开始状态 / transform: translateX(0px); / 动画的开始状态 / width: 100px; height: 100px; } 40% { width: 150px; height: 150px; background: orange; } 80% { width: 200px; height: 200px; background: cyan; transform: translateX(100px); } 100% { / 位置结束状态 / transform: translateX(0px); / 动画的结束状态 / width: 100px; height: 100px; background: pink; } }</style><div class=“box”></div>效果图:11.3 动画案例1、全屏切换定义三个动画集,当点击a标签的时候,通过他的锚点触发对应的动画集。<style type=“text/css”> * { margin: 0; padding: 0; } html,body { width: 100%; height: 100%; } .box { width: 100%; height: 100%; position: relative; overflow: hidden; } .box>div{ width: 100%; height: 100%; position: absolute; background-size: cover; } .one { background: url(img/bg6.jpg) no-repeat; } .two { background: url(img/bg7.jpg) no-repeat; } .three { background: url(img/bg8.jpg) no-repeat; } .box .numbers { width: 150px; height: 40px; left: 50%; margin-left: -75px; bottom: 30px; z-index: 4; } .box .numbers a { width: 40px; height: 40px; display: block; line-height: 40px; text-align: center; text-decoration: none; color: #777; background-color: rgba(255,255,255,.8); border-radius: 50%; float: left; margin: 0 5px; } .one:target { animation: move 1s linear; z-index: 1; } .two:target { animation: rotate 1s linear; z-index: 1; } .three:target { animation: scale 1s linear; z-index: 1; } @keyframes move { from { transform: translateX(-100%); } to { transform: translateX(0px); } } @keyframes rotate { from { transform: scale(0) rotateZ(0deg); } to { transform: scale(1) rotateZ(360deg); } } @keyframes scale { from { transform: scale(0); } to { transform: scale(1); } }</style><div class=“box”> <div class=“one” id=“one”></div> <div class=“two” id=“two”></div> <div class=“three” id=“three”></div> <div class=“numbers”> <a href="#one">1</a> <a href="#two">2</a> <a href="#three">3</a> </div></div>效果图:2、大海波涛波浪其实是两张图片,执行的是同一组动画,只需要在第二张图片执行动画的时候,让它延迟1s执行,就可以出现“波动”的感觉。<style type=“text/css”> * { margin: 0; padding: 0; } html,body { width: 100%; height: 100%; } .box { width: 100%; height: 100%; background-color: #0EA9B1; position: relative; overflow: hidden; } .box img { position: absolute; width: 100%; bottom: 0; } .sun { width: 80px; height: 80px; background-color: #fff; border-radius: 50%; position: absolute; left: 100px; top: 50px; } .sun:after { content: “”; display: block; width: 100px; height: 100px; border-radius: 50%; background-color: rgba(255,255,255,.6); position: absolute; left: 50%; top: 50%; margin-left: -50px; margin-top: -50px; animation: scale 1s linear infinite alternate; } .sun:before { content: “”; display: block; width: 120px; height: 120px; border-radius: 50%; background-color: rgba(255,255,255,.4); position: absolute; left: 50%; top: 50%; margin-left: -60px; margin-top: -60px; animation: scale 2s 1s linear infinite alternate; } @keyframes scale { from { transform: scale(1); } to { transform: scale(1.2); } } @keyframes move{ from { bottom: 0px; } to { bottom: -60px; } } img:nth-child(2){ animation: move 1s linear infinite alternate; } img:nth-child(3) { animation: move 2s 1s linear infinite alternate; }</style> <div class=“box”> <div class=“sun”></div> <img src=“img/1.png” alt=""> <img src=“img/2.png” alt=""> </div>效果图:3、宇宙通过定位确定各“星球”的位置,再对每个星球设定单独的动画,形成联动的各大星球。<style type=“text/css”> * { margin: 0; padding: 0; } html,body { width: 100%; height: 100%; background: url(img/bg.jpg) no-repeat; background-size: cover; position: relative; } .box { width: 600px; height: 600px; border: 1px solid #ccc; border-radius: 50%; position: absolute; left: 50%; margin-left: -300px; top: 50%; margin-top: -300px; } .sun { position: absolute; width: 50px; height: 50px; background-color: orange; border-radius: 50%; margin-left: -25px; left: 50%; margin-top: -25px; top: 50%; box-shadow: 0px 0px 50px orange; } .lin1 { width: 100px; height: 100px; border: 1px solid #ccc; border-radius: 50%; margin-left: -50px; margin-top: -50px; animation: rotate 10s linear infinite; } .lin1:after { content: “”; width: 20px; height: 20px; display: block; border-radius: 50%; background-color: orange; position: absolute; left: -2px; top: 10px; } .public { position: absolute; left: 50%; top: 50%; } @keyframes rotate { from { transform: rotateZ(0deg); } to { transform: rotateZ(360deg); } } .lin2 { width: 150px; height: 150px; border: 1px solid #ccc; border-radius: 50%; margin-left: -75px; margin-top: -75px; animation: rotate 5s linear infinite; } .lin2:after { content: “”; width: 10px; height: 10px; background-color: blue; display: block; border-radius: 50%; position: absolute; left: 25px; top: 10px; } .lin3 { width: 230px; height: 230px; border: 1px solid #ccc; border-radius: 50%; margin-left: -115px; margin-top: -115px; animation: rotate 10s linear infinite; } .lin3 span { width: 20px; height: 20px; display: block; background-color: green; border-radius: 50%; position: absolute; left: 50px; animation: rotate 5s linear infinite; } .lin3 span b { width: 10px; height: 10px; background-color: pink; display: block; border-radius: 50%; position: absolute; left: 25px; } .lin4 { width: 332px; height: 332px; border: 1px solid #ccc; background: url(img/asteroids_meteorids.png) no-repeat; border-radius: 50%; margin-left: -161px; margin-top: -161px; animation: rotate 15s linear infinite; } .lin4:after { content: “”; display: block; width: 10px; height: 10px; background-color: red; border-radius: 50%; position: absolute; left: 120px; } .line5 { width: 450px; height: 450px; border: 1px solid #ccc; border-radius: 50%; margin-left: -220px; margin-top: -220px; animation: rotate 10s linear infinite; } .line5 span { width: 30px; height: 30px; background-color: blue; border-radius: 50%; display: block; position: absolute; left: 120px; } .line5 span b { display: block; width: 40px; height: 40px; border: 5px solid #ccc; border-radius: 50%; transform: skew(45deg); position: absolute; left: -6px; top: -10px; }</style> <div class=“box”> <div class=“sun”></div> <div class=“lin1 public”></div> <div class=“lin2 public”></div> <div class=“lin3 public”> <span> <b></b> </span> </div> <div class=“lin4 public”></div> <div class=“line5 public”> <span> <b></b> </span> </div></div>效果图:4、无缝滚动轮播结构上与普通的轮播图没有什么区别,定义一个盒子的宽度,宽度为八张图片的总宽度,定义一个动画集,10s内将ul从0的位置移动到盒子的宽度,此时动画集结束,应该是跳到0的位置,循环此动画就会形成无缝滚动。之所以ul下的li定义的是十四张图片,是因为,一开始的时候图片从零的位置向右移动时,如果只有七张图片的时候,右边是空白的,只有完全到达盒子宽的时候,整个图片才会显示完整,用户体验很差,所以需要用到障眼法,就是用14张图片。<style type=“text/css”> * { margin: 0; padding: 0; list-style: none; } .box { width: 882px; height: 84px; border: 1px solid #ccc; margin: 50px auto; overflow: hidden; } ul { width: 1764px; height: 84px; float: right; animation: move 10s linear infinite; } li { float: left; } ul:hover { animation-play-state: paused; } @keyframes move { from { transform: translateX(0px); } to { transform: translateX(882px); } }</style><div class=“box”> <ul> <li><img src=“img/1.jpg” alt=""></li> <li><img src=“img/2.jpg” alt=""></li> <li><img src=“img/3.jpg” alt=""></li> <li><img src=“img/4.jpg” alt=""></li> <li><img src=“img/5.jpg” alt=""></li> <li><img src=“img/6.jpg” alt=""></li> <li><img src=“img/7.jpg” alt=""></li> <li><img src=“img/1.jpg” alt=""></li> <li><img src=“img/2.jpg” alt=""></li> <li><img src=“img/3.jpg” alt=""></li> <li><img src=“img/4.jpg” alt=""></li> <li><img src=“img/5.jpg” alt=""></li> <li><img src=“img/6.jpg” alt=""></li> <li><img src=“img/7.jpg” alt=""></li> </ul></div>效果图:5、进度条前面已经接触了一个demo,只不过是一个静态的进度条,现在我们通过动画,让他动起来。<style type=“text/css”> * { margin: 0; padding: 0; } html,body { width: 100%; height: 100%; background-color: #ccc; } .line { width: 980px; height: 40px; margin: 50px auto; border-radius: 10px; background-image: linear-gradient( 135deg, #fff 25%, #000 25%, #000 50%, #fff 50%, #fff 75%, #000 75%, #000 ); background-size: 40px 40px; animation: move 2s linear infinite; } @keyframes move { from { background-position: 0px; } to { background-position: 40px; } }</style><div class=“line”></div>效果图:6、时钟案例本时钟案例,主要是依赖CSS3中动画以及旋转的概念实现的。<style type=“text/css”> * { margin: 0; padding: 0; } body{ background: #ccc; } .box { width: 300px; height: 300px; border: 1px solid #fff; margin: 100px auto; border-radius: 50%; position: relative; } .line { position: absolute; left: 50%; margin-left: -1px; height: 100%; width: 2px; background-color: #fff; } .line:nth-child(1){ } / 设置时刻位置的刻度线 / .line:nth-child(2) { transform: rotateZ(30deg); } .line:nth-child(3) { transform: rotateZ(60deg); } .line:nth-child(4) { transform: rotateZ(90deg); } .line:nth-child(5) { transform: rotateZ(120deg); } .line:nth-child(6) { transform: rotateZ(-30deg); } .cover { position: absolute; left: 50%; top: 50%; margin-left: -140px; margin-top: -140px; width: 280px; height: 280px; background-color: #fff; border-radius: 50%; } / 设置时针转完一圈所要用的时间为 43200s / .h{ width: 6px; height: 100px; background-color: #000; position: absolute; left: 50%; top: 40px; margin-left: -3px; animation: rotate 43200s linear infinite; } / 设置分针转完一圈所要用的时间为 3600s / .m { width: 4px; height: 120px; background-color: #000; position: absolute; left: 50%; top: 20px; margin-left: -2px; transform-origin: bottom; animation: rotate 3600s linear infinite; } / 设置秒针转完一圈所要用的时间为 60s / / steps(60) 表示该动画需要60步完成 / .s { width: 2px; height: 130px; background-color: orange; position: absolute; left: 50%; top: 10px; margin-left: -1px; transform-origin: bottom; animation: rotate 60s steps(60) infinite; } .dotted { width: 10px; height: 10px; background-color: #000; border-radius: 50%; position: absolute; left: 50%; top: 50%; margin-left: -5px; margin-top: -5px; } / 定义旋转角度动画,从0 度旋转到 360度 / @keyframes rotate { from { transform: rotateZ(0deg); } to { transform: rotateZ(360deg); } }</style><div class=“box”> <div class=“line”></div> <div class=“line”></div> <div class=“line”></div> <div class=“line”></div> <div class=“line”></div> <div class=“line”></div> <div class=“cover”> <div class=“h”></div> <div class=“m”></div> <div class=“s”></div> <div class=“dotted”></div> </div></div>效果图:7、魔方案例<style type=“text/css”> * { margin: 0; padding: 0; list-style: none; } .box { width: 400px; height: 400px; margin: 150px auto; / perspective: 1000px; / transform-style: preserve-3d; animation: rotate 5s linear infinite alternate; } .box>div { position: absolute; width: 100%; height: 100%; } .left { background-color: transparent; transform: rotateY(-90deg) translateZ(200px); } .right { background-color: transparent; transform: rotateY(90deg) translateZ(200px); } .top { background-color: transparent; transform: rotateX(90deg) translateZ(200px); } .bottom { background-color: transparent; transform: rotateX(-90deg) translateZ(200px); } .before { background-color: transparent; transform: translateZ(200px); } .back { transform: translateZ(-200px); background-color: transparent; } @keyframes rotate { 0% { transform: rotateY(0deg) rotateX(0deg) rotateZ(0deg); } 50% { transform: rotateY(30deg) rotateX(60deg) rotateZ(45deg); } 75% { transform: rotateY(80deg) rotateX(-60deg) rotateZ(-45deg); } 100% { transform: rotateY(-45deg) rotateX(50deg) rotateZ(-35deg); } } li { width: 120px; height: 120px; background-color: green; float: left; margin-left: 10px; margin-top: 10px; text-align: center; line-height: 120px; font-size: 50px; border-radius: 10px; } .left li { background-color: orange; } .right li { background-color: pink; } .top li { background-color: red; } .bottom li { background-color: yellow; } .back li { background-color: orangered; }</style><div class=“box”> <div class=“left”> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div> <div class=“right”> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div> <div class=“top”> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div> <div class=“bottom”> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div> <div class=“before”> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div> <div class=“back”> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div></div>效果图:12. 伸缩布局本章节摘自阮一峰老师的《Flex 布局教程》CSS3在布局方面做了非常大的改进,使得我们对块级元素的布局排列变得十分灵活,适应性非常强,其强大的伸缩性,在响应式开中可以发挥极大的作用。伸缩布局也叫弹性布局或者flex布局。12.1 什么是伸缩布局Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性。任何一个容器都可以指定为Flex布局。.box{ display: flex;}行内元素也可以使用Flex布局。.box{ display: inline-flex;}Webkit内核的浏览器,必须加上-webkit前缀。.box{ display: -webkit-flex; / Safari / display: flex;}注意,设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。12.2 基本概念浏览器兼容性:学习几个新概念:主轴:Flex容器的主轴主要用来配置Flex项目,默认是水平方向侧轴:与主轴垂直的轴称作侧轴,默认是垂直方向的方向:默认主轴从左向右,侧轴默认从上到下主轴和侧轴并不是固定不变的,通过flex-direction可以互换。12.3 flex-direction属性flex-direction属性决定主轴的方向(即项目的排列方向)。.box { flex-direction: row | row-reverse | column | column-reverse; }参数说明:row(默认值):主轴为水平方向,起点在左端。row-reverse:主轴为水平方向,起点在右端。column:主轴为垂直方向,起点在上沿。column-reverse:主轴为垂直方向,起点在下沿。示例代码:<style> .block{ width: 100%; border-radius: 15px; background: rgba(158,158,158,0.15); padding: 20px; box-sizing: border-box; margin-bottom: 20px; } .box{ background: #fff; display: flex; margin: 0 0 55px; } .box-item { width: 150px; height: 150px; line-height: 150px; vertical-align: middle; margin: 5px; background-color: #ffd200; font-size: 100px; color: white; text-align: center; } .box-1-1 { flex-direction: row; } .box-1-2 { flex-direction: row-reverse; } .box-1-3 { flex-direction: column; } .box-1-4 { flex-direction: column-reverse; }</style><div class=“block”> <h4>flex-direction: row 水平向右</h4> <div class=“box box-1-1”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div><div class=“block”> <h4>flex-direction: row-reverse 水平向左</h4> <div class=“box box-1-2”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div><div class=“block”> <h4>flex-direction: column 垂直向下</h4> <div class=“box box-1-3”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div><div class=“block”> <h4>flex-direction: column-reverse 垂直向上</h4> <div class=“box box-1-4”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div>效果图:12.4 flex-wrap 属性默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。.box{ flex-wrap: nowrap | wrap | wrap-reverse; }参数说明:nowrap(默认):不换行,宽度自动适应;wrap:换行,第一行在上方;wrap-reverse:换行,第一行在下方示例代码:<style> .block{ width: 100%; border-radius: 15px; background: rgba(158,158,158,0.15); padding: 20px; box-sizing: border-box; margin-bottom: 20px; } .box{ background: #fff; display: flex; margin: 0 0 55px; } .box-item { width: 150px; height: 150px; line-height: 150px; vertical-align: middle; margin: 5px; background-color: #ffd200; font-size: 100px; color: white; text-align: center; } .box-2-1 { flex-wrap: nowrap; } .box-2-2 { flex-wrap: wrap; } .box-2-3 { flex-wrap: wrap-reverse; } </style><div class=“block”> <h4>nowrap(默认):不换行。</h4> <div class=“box box-2-1”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> </div></div><div class=“block”> <h4>wrap:换行,第一行在上方。</h4> <div class=“box box-2-2”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> </div></div><div class=“block”> <h4>wrap-reverse:换行,第一行在下方。</h4> <div class=“box box-2-3”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> </div></div>效果图:12.5 flex-flow 属性flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。.box { flex-flow: <flex-direction> || <flex-wrap>;}示例代码:<style> .block{ width: 100%; border-radius: 15px; background: rgba(158,158,158,0.15); padding: 20px; box-sizing: border-box; margin-bottom: 20px; } .box{ background: #fff; display: flex; margin: 0 0 55px; } .box-item { width: 150px; height: 150px; line-height: 150px; vertical-align: middle; margin: 5px; background-color: #ffd200; font-size: 100px; color: white; text-align: center; } .box-3{ flex-flow: row wrap; }</style><div class=“block”> <h4>row nowrap(默认):不换行。</h4> <div class=“box box-3”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> </div></div>效果图:12.6 justify-content 属性justify-content属性定义了项目在主轴上的对齐方式。.box { justify-content: flex-start | flex-end | center | space-between | space-around;}参数说明:flex-start:(默认值):左对齐flex-end:右对齐center: 居中space-between:两端对齐,项目之间的间隔都相等。space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。示例代码:<style> .block{ width: 100%; border-radius: 15px; background: rgba(158,158,158,0.15); padding: 20px; box-sizing: border-box; margin-bottom: 20px; } .box{ background: #fff; display: flex; margin: 0 0 55px; } .box-item { width: 150px; height: 150px; line-height: 150px; vertical-align: middle; margin: 5px; background-color: #ffd200; font-size: 100px; color: white; text-align: center; } .box-4-1{ justify-content:flex-start; } .box-4-2{ justify-content:flex-end; } .box-4-3{ justify-content:center; } .box-4-4{ justify-content:space-between; } .box-4-5{ justify-content:space-around; }</style><div class=“block”> <h4>flex-start(默认值):左对齐</h4> <div class=“box box-4-1”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div><div class=“block”> <h4>flex-end:右对齐</h4> <div class=“box box-4-2”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div><div class=“block”> <h4>center: 居中</h4> <div class=“box box-4-3”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div><div class=“block”> <h4>space-between:两端对齐,项目之间的间隔都相等。</h4> <div class=“box box-4-4”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div><div class=“block”> <h4>space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。</h4> <div class=“box box-4-5”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> </div></div>效果图:12.7 align-items 属性align-items属性定义项目在交叉轴上如何对齐。.box { align-items: flex-start | flex-end | center | baseline | stretch;}参数说明:flex-start:交叉轴的起点对齐。flex-end:交叉轴的终点对齐。center:交叉轴的中点对齐。baseline: 项目的第一行文字的基线对齐。stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。示例代码:<style> .block{ width: 100%; border-radius: 15px; background: rgba(158,158,158,0.15); padding: 20px; box-sizing: border-box; margin-bottom: 20px; } .box{ background: #fff; display: flex; margin: 0 0 55px; } .box-item { width: 150px; height: 150px; line-height: 150px; vertical-align: middle; margin: 5px; background-color: #ffd200; font-size: 100px; color: white; text-align: center; } .item-tall { height: 400px; line-height: 400px; } .box-5-1 { align-items: flex-start; } .box-5-2 { align-items: flex-end; } .box-5-3 { align-items: center; } .box-5-4{ align-items: baseline; } .box-5-4 .box-item{ font-size: 88px; line-height: initial; text-decoration: underline; } .box-5-4 .item-tall{ font-size: 122px; line-height: initial; } .box-5-5 { align-items: stretch; } .box-5-5 .box-item { height: auto;}</style><div class=“block”> <h4>flex-start:交叉轴的起点对齐。</h4> <div class=“box box-5-1”> <div class=“box-item”>1</div> <div class=“box-item item-tall”>2</div> <div class=“box-item”>3</div> <div class=“box-item item-tall”>4</div> </div></div><div class=“block”> <h4>flex-end:交叉轴的终点对齐。</h4> <div class=“box box-5-2”> <div class=“box-item”>1</div> <div class=“box-item item-tall”>2</div> <div class=“box-item”>3</div> <div class=“box-item item-tall”>4</div> </div></div><div class=“block”> <h4>center:交叉轴的中点对齐。</h4> <div class=“box box-5-3”> <div class=“box-item”>1</div> <div class=“box-item item-tall”>2</div> <div class=“box-item”>3</div> <div class=“box-item item-tall”>4</div> </div></div><div class=“block”> <h4>baseline: 项目的第一行文字的基线对齐。</h4> <div class=“box box-5-4 line”> <div class=“box-item”>1</div> <div class=“box-item item-tall”>2</div> <div class=“box-item”>3</div> <div class=“box-item item-tall”>4</div> </div></div><div class=“block”> <h4>stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。</h4> <div class=“box box-5-5”> <div class=“box-item”>1</div> <div class=“box-item item-tall”>2</div> <div class=“box-item”>3</div> <div class=“box-item item-tall”>4</div> </div></div>效果图:12.8 align-content 属性align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch;}参数说明:flex-start:与交叉轴的起点对齐。flex-end:与交叉轴的终点对齐。center:与交叉轴的中点对齐。space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。stretch(默认值):轴线占满整个交叉轴。示例代码:<style> .box-tall { height: 800px; } .box-6-1 { flex-wrap: wrap; align-content: flex-start; } .box-6-2 { flex-wrap: wrap; align-content: flex-end; } .box-6-3 { flex-wrap: wrap; align-content: center; } .box-6-4 { flex-wrap: wrap; align-content: space-between } .box-6-5 { flex-wrap: wrap; align-content: space-around; } .box-6-6 { flex-wrap: wrap; align-content: stretch; } .box-6-6 .box-item { height: auto; }</style><div class=“block”> <h4>flex-start:交叉轴的起点对齐。</h4> <div class=“box box-tall box-6-1”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> <div class=“box-item”>8</div> <div class=“box-item”>9</div> <div class=“box-item”>10</div> <div class=“box-item”>11</div> <div class=“box-item”>12</div> </div></div><div class=“block”> <h4>flex-end:与交叉轴的终点对齐。</h4> <div class=“box box-tall box-6-2”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> <div class=“box-item”>8</div> <div class=“box-item”>9</div> <div class=“box-item”>10</div> <div class=“box-item”>11</div> <div class=“box-item”>12</div> </div></div><div class=“block”> <h4>center:与交叉轴的中点对齐。</h4> <div class=“box box-tall box-6-3”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> <div class=“box-item”>8</div> <div class=“box-item”>9</div> <div class=“box-item”>10</div> <div class=“box-item”>11</div> <div class=“box-item”>12</div> </div></div><div class=“block”> <h4>space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。</h4> <div class=“box box-tall box-6-4”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> <div class=“box-item”>8</div> <div class=“box-item”>9</div> <div class=“box-item”>10</div> <div class=“box-item”>11</div> <div class=“box-item”>12</div> </div></div><div class=“block”> <h4>space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。</h4> <div class=“box box-tall box-6-5”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> <div class=“box-item”>8</div> <div class=“box-item”>9</div> <div class=“box-item”>10</div> <div class=“box-item”>11</div> <div class=“box-item”>12</div> </div></div><div class=“block”> <h4>stretch(默认值):轴线占满整个交叉轴。</h4> <div class=“box box-tall box-6-6”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item”>4</div> <div class=“box-item”>5</div> <div class=“box-item”>6</div> <div class=“box-item”>7</div> <div class=“box-item”>8</div> <div class=“box-item”>9</div> <div class=“box-item”>10</div> <div class=“box-item”>11</div> <div class=“box-item”>12</div> </div></div>效果图:12.9 Order 属性order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。.item { order: <integer>; }参数说明:order属性值是一个数值,可以是正数,也可以是负数,在当前项目里,数值越小,排列越靠前。示例代码:<style> .box-7 .order { order: -1; } .box-7 .box-item div{ font-size: 14px; position: relative; bottom: 90px; color: #9236f4; }</style><div class=“box box-7”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> <div class=“box-item order”>4 <div>(order:-1)</div></div></div>效果图:12.10 flex-grow 属性flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。.item { flex-grow: <number>; / default 0 / }参数说明:如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。示例代码:<style> .box-8 .box-item div{ font-size: 14px; position: relative; bottom: 90px; color: #9236f4; } .box-8 .grow{ flex-grow: 1; width: auto; } .box-8 .grow-2 { flex-grow: 2; }</style><div class=“block”> <div class=“box box-8”> <div class=“box-item grow”>1 <div>flex-grow: 1</div></div> <div class=“box-item grow grow-2”>2 <div>flex-grow: 2</div></div> <div class=“box-item grow”>3 <div>flex-grow: 1</div></div> </div></div>效果图:12.11 flex-shrink 属性flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。.item { flex-shrink: <number>; / default 1 / }参数说明:如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。负值对该属性无效。示例代码:<style> .box-9 .box-item div{ font-size: 14px; position: relative; bottom: 90px; color: #9236f4; } .box-9 .box-item { width: 400px; } .box-9 .shrink{ flex-shrink: 0; }</style><div class=“block”> <div class=“box box-9”> <div class=“box-item shrink”>1 <div>flex-shrink: 0</div></div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> </div></div>效果图:12.12 flex-basis 属性flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main-size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。.item { flex-basis: <length>; | auto; / default auto / }示例代码:<style> .box-10 .box-item { flex-basis: 200px; width: 400px; / width 将失去作用*/ }</style><div class=“block”> <h4></h4> <div class=“box box-10”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item”>3</div> </div></div>效果图:12.13 flex 属性flex属性是flex-grow, flex-shrink和flex-basis的简写,默认值为0 1 auto。后两个属性可选。该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。.item { flex: none | [ <‘flex-grow’> <‘flex-shrink’>? || <‘flex-basis’> ] }12.14 align-self 属性align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。.item { align-self: auto | flex-start | flex-end | center | baseline | stretch;}参数说明:auto:表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。flex-start:交叉轴的起点对齐。flex-end:交叉轴的终点对齐。center:交叉轴的中点对齐。baseline: 项目的第一行文字的基线对齐。stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。示例代码:<style> .box-11 { height: 400px; } .box-11 .box-item { align-self: flex-start; } .box-11 .box-item div{ font-size: 14px; position: relative; bottom: 90px; color: #9236f4; } .box-11 .end { align-self: flex-end; }</style><div class=“block”> <h4>stretch(默认值):轴线占满整个交叉轴。</h4> <div class=“box box-11”> <div class=“box-item”>1</div> <div class=“box-item”>2</div> <div class=“box-item end”>3<div>flex-end</div></div> <div class=“box-item”>4</div> </div></div>效果图:

November 26, 2018 · 14 min · jiezi

CSS3 入门详解(一)

前言如同人类的的进化一样,CSS3是CSS2的“进化”版本,在CSS2基础上,增强或新增了许多特性,弥补了CSS2的众多不足之处,使得Web开发变得更为高效和便捷。1.选择器CSS3新增了许多灵活查找元素的方法,极大的提高了查找元素的效率和精准度。CSS3选择器与jQuery中所提供的绝大部分选择器兼容。1.1 属性选择器属性选择器就是通过属性来选择元素。选择器含义[attr]存在attr属性即可[attr=val]属性值完全等于val[attr*=val]属性值里包含val字符并且在“任意”位置[attr^=val]属性值里包含val字符并且在“开始”位置[attr$=val]属性值里包含val字符并且在“结束”位置1、[attr]<style> /* 所有拥有class属性的标签,添加color样式 / [class]{ color: #333; }</style>2、[attr=val]<style> / 所有拥有class属性全等于“one”的标签,添加color样式 / [class = “one”]{ color: #333; }</style>3、[attr=val]<style> /* class属性的值里面包含“one”的标签,添加color样式 / [attr=“one”]{ color: #333; }</style>4、[attr^=val]<style> /* class属性的值以“one”开始的标签,添加color样式 / [attr ^= “one”]{ color: #333; }</style>5、[attr$=val]<style> / class属性的值以“one”结束的标签,添加color样式 / [attr $= “one”]{ color: #333; }</style>1.2 伪类选择器除了以前介绍的的:link、:active、:visited、:hover,CSS3又新增了其它的伪类选择器。1、结构(位置)伪类选择器含义:first-child其父元素的第1个子元素:last-child其父元素的最后1个子元素:nth-child(n)其父元素的第n个子元素:nth-last-child(n)其父元素的第n个子元素(倒着数)2、空伪类:empty选中没有任何子节点的元素<style> div:empty { / 没有子元素的div元素 / width: 100px; height: 100px; background-color: pink; }</style><!– css 样式不起作用 –><div class=“one”>阿斯蒂芬</div> <!– css样式不起作用 –><div> <p></p></div><!– css样式生效 –><div></div>3、目标伪类:target结合锚点进行使用,处于当前锚点的元素会被选中;<style type=“text/css”> / 使用锚链接指向当前标签的时候 / .one:target { background-color: pink; font-size: 30px; }</style><a href="#hh">找笑脸去</a><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p id=“hh” class=“one”>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p>4、排除伪类:not(selector)除selector(任意选择器)外的元素会被选中;<style> / 除了类名是“.not”的div元素 / div:not(.one) { width: 100px; height: 100px; background-color: pink; }</style><!– css样式不生效 –><div class=“one”></div><!– css样式生效 –><p></p><!– css样式生效 –><div></div>1.3 伪元素选择器1、::first-letter文本的第一个单词或字(如中文、日文、韩文等)2、::first-line 文本第一行;3、::selection 可改变选中文本的样式;4、::before和::after示例代码:伪元素实现横竖分割线<style type=“text/css”> * { margin: 0; padding: 0; list-style: none; } .box { width: 300px; height: 200px; background-color: pink; margin: 50px auto; } .box li { width: 100px; height: 100px; float: left; background-color: #555; position: relative; overflow: hidden; } li:before { content: “”; display: block; width: 90px; height: 1px; background-color: #ccc; position: absolute; top: 97px; left: 5px; } li:after { content: “”; display: block; width: 1px; height: 90px; background-color: #ccc; position: absolute; left: 0px; top: 4px; } li:nth-child(1):after,li:nth-child(4):after { display: none; } li:nth-last-child(-n+3):before { display: none; }</style><div class=“box”> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul></div>效果图::after、:before在旧版本里是伪元素,CSS3的规范里“:”用来表示伪类,“::”用来表示伪元素,但是在高版本浏览器下:after、:before会被自动识别为::after、::before,这样做的目的是用来做兼容处理。2. 颜色新增了RGBA、HSLA模式,其中的A表示透明度,即可以设置颜色值的透明度,相较opacity,它们不具有继承性,即不会影响子元素的透明度。2.1 RGBARed、Green、Blue、Alpha即RGBA,R、G、B取值范围0255。<style> #box{ width:100px; height:100px; background: rgba(200,200,200,.5); }</style><div id=“box”></div>2.2 HSLAH 色调 取值范围0360,0/360表示红色、120表示绿色、240表示蓝色S 饱和度 取值范围0%100%L 亮度 取值范围0%100%A 透明度 取值范围01<style> #box{ width:100px; height:100px; background: hsla(200,50%,50%,.5); }</style><div id=“box”></div>2.3 关于 CSS 的透明度Alpha和opacity的区别主要就是,opacity具有继承性,父盒子设置该属性,下面所有的子元素都会继承该属性。transparent不可调节透明度,始终完全透明。3. 文本阴影text-shadow,可分别设置偏移量、模糊度、颜色(可设透明度)。如:text-shadow: 2px 2px 2px #CCC;1、水平偏移量 正值向右 负值向左;2、垂直偏移量 正值向下 负值向上;3、模糊度是不能为负值;4、阴影颜色。示例代码:文字浮雕<style> html,body { margin: 0; padding: 0; width: 100%; height: 100%; background-color: #999; font-size: 50px; text-align: center; line-height: 260px; color: #999; } .one { text-shadow: -1px -1px 1px #fff,1px 1px 1px #000; } .two { text-shadow: -1px -1px 1px #000,1px 1px 1px #fff; }</style><div class=“one”>我是凸起文字</div><div class=“two”>我是凹下去的文字</div>效果图:4. 盒模型CSS3中可以通过box-sizing来指定盒模型,即可指定为content-box、border-box,这样我们计算盒子大小的方式就发生了改变。可以分成两种情况:1、box-sizing: border-box盒子大小为width2、box-sizing: content-box盒子大小为width + padding + border注:上面的标注的width指的是CSS属性里设置的width: length,content的值是会自动调整的。示例代码:<style type=“text/css”> .box { width: 316px; height: 170px; float: left; margin-left: 20px; box-sizing: border-box; } .box img { width: 100%; height: 100%; } .box:hover { border: 10px solid #00eeff; }</style><div class=“box”> <img src=“1.jpg” alt=""></div><div class=“box”> <img src=“1.jpg” alt=""></div>效果图:可以看出通过设置盒子模型后,虽然.box设置了边框,但是整个box的盒子大小没有改变。5. 边框边框中的边框圆角、边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特征。5.1 边框圆角通过border-radius属性,设置边框圆角,圆角处理时,脑中要形成圆、圆心、横轴、纵轴的概念,正圆是椭圆的一种特殊情况。为了方便表述,我们将四个角标记成1、2、3、4,如2代表右上角,CSS里提供了border-radius来设置这些角横纵轴半径值。分别设置横纵轴半径,以“/”进行分隔,遵循“1,2,3,4”规则,“/”前面的14个用来设置横轴半径(分别对应横轴1、2、3、4位置),“/”后面1~4个参数用来设置纵轴半径(分别对应纵轴1、2、3、4位置 )。<style type=“text/css”> .box { margin: 50px auto; width: 300px; height: 500px; border: 1px solid #ccc; border-radius: 10px 20px 50px 70px / 10px 20px 50px 70px; }</style><div class=“box”></div>效果图:一般情况下,我们用不到这么复杂的,除非特殊需求的时候。支持简写模式,具体如下:1、border-radius: 10px;表示四个角的横纵轴半径都为10px;2、border-radius: 10px 5px;表示1和3角横纵轴半径都为10px,2和4角横纵轴半径为5px;3、border-radius: 10px 5px 8px;表示1角模纵轴半径都为10px,2和4角横纵轴半径都为8px,3角的横纵轴半径都为8px;4、border-radius: 10px 8px 6px 4px;表示1角横纵轴半径都为10px,表示2角横纵轴半径都为8px,表示3角横纵轴半径都为6px,表示4角横纵轴半径都为6px;椭圆的画法:<style type=“text/css”> .box { margin: 50px auto; width: 300px; height: 500px; border: 1px solid #ccc; / 当盒子长宽不一致时,圆角属性 分别设置宽度的一半,以及长度的一半,即是椭圆 / / 或者直接 border-radius:50%; / border-radius: 150px 250px; }</style><div class=“box”></div>如果不想计算,直接设百分比:“50%”。正圆的画法:<style type=“text/css”> .box { margin: 50px auto; width: 200px; height: 200px; border: 1px solid #ccc; / 当盒子长宽相等时,圆角属性分别设置宽度的一半,以及长度的一半,即是正圆 / / 或者直接 border-radius:50%; / border-radius: 100px; }</style><div class=“box”></div>示例代码:边框圆角合集<style> * { margin: 0; padding: 0; list-style: none; background-color: wheat; overflow: hidden; } .box { width: 980px; height: 400px; background-color: #fff; margin: 50px auto; } .box li { float: left; width: 193px; height: 193px; background-color: #fff; margin:5px; box-shadow: 2px 3px 5px #aaa; } li:first-child:after { content: “”; height: 130px; width: 130px; margin: 30px auto; display: block; border: 1px solid orangered; border-radius: 50%; } li:nth-child(2):after { content: “”; display: block; height: 130px; width: 130px; border: 1px solid orangered; margin: 30px auto; border-radius: 65px 65px 0px 0px; } li:nth-child(3):after { content: “”; display: block; width: 130px; height: 65px; border: 1px solid orangered; margin: 50px auto; border-radius: 65px 65px 0px 0px; } li:nth-child(4):after { content: “”; display: block; width: 130px; height: 130px; border: 1px solid orangered; margin: 20px auto; border-radius: 65px 0px 0px 0px; } li:nth-child(5):after { content: “”; width: 130px; height: 65px; display: block; border: 1px solid orangered; border-radius: 50%; margin: 50px auto; } li:nth-child(6):after{ content: “”; height: 130px; width: 65px; display: block; border: 1px solid orangered; border-radius: 50%; margin: 20px auto; } li:nth-child(7):after { content: “”; height: 130px; width: 130px; display: block; border: 1px solid orangered; margin: 20px auto; border-radius: 135px 0px 0px 0px; } li:nth-child(8):after { content: “”; width: 135px; height: 30px; display: block; margin: 30px auto; border: 1px solid orangered; border-radius: 65px 65px 0px 0px / 30px 30px 0px 0px; }</style><div class=“box”> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul></div>效果图:5.2 边框阴影box-shadow,与文字阴影类似,可分别设置盒子阴影偏移量、模糊度、颜色(可设透明度)。如:box-shadow: 5px 5px 5px #CCC1、水平偏移量 正值向右 负值向左;2、垂直偏移量 正值向下 负值向上;3、模糊度是不能为负值;4、inset可以设置内阴影;注:设置边框阴影不会改变盒子的大小,即不会影响其兄弟元素的布局。可以设置多重边框阴影,实现更好的效果,增强立体感,符合渐进增强,实际开发中可以大胆使用。示例代码:<style> .box { width: 200px; height: 200px; margin: 50px auto; border: 1px dashed #000; box-shadow: 2px 3px 4px rgba(0, 247, 255, 0.452),inset 5px 6px 7px rgba(255, 0, 140, 0.562); }</style><div class=“box”></div>效果图:我们通过上图可以看到,虚线是盒子的位置,粉色阴影是inset属性设置的,所以是内阴影,浅蓝色是直接设置的外阴影,效果一目了然。6. 背景背景在CSS3中也得到很大程度的增强,比如背景图片尺寸、背景裁切区域、背景定位参照点、多重背景等。6.1 background-size通过background-size设置背景图片的尺寸,就像我们设置img的尺寸一样,在移动Web开发中做屏幕适配应用非常广泛。其参数设置如下:可以设置长度单位(px)或百分比(设置百分比时,参照盒子的宽高)设置为cover时,会自动调整缩放比例,保证图片始终填充满背景区域,如有溢出部分则会被隐藏。设置为contain会自动调整缩放比例,保证图片始终完整显示在背景区域。6.2 background-origin通过background-origin可以设置背景图片定位(background-position)的参照原点。其参数设置如下:border-box以边框做为参考原点;padding-box以内边距做为参考原点;content-box以内容区做为参考点;示例代码:<style type=“text/css”> .box1,.box2,.box3 { width: 200px; height: 200px; display: inline-block; margin: 50px 30px; border: 10px dashed aquamarine; padding: 10px; background-image: url(bg.jpg); background-repeat: no-repeat; } .box1{ background-origin: padding-box; } .box2{ background-origin: content-box; } .box3{ background-origin: border-box; }</style><div class=“box1”></div><div class=“box2”></div><div class=“box3”></div>效果图:6.3 background-clip通过background-clip,可以设置对背景区域进行裁切,即改变背景区域的大小。其参数设置如下:border-box裁切边框以内为背景区域;padding-box裁切内边距以内为背景区域;content-box裁切内容区做为背景区域;6.4 多背景以逗号分隔可以设置多背景,可用于自适应布局。在一个盒子里可以设置多个背景图片,通过背景定位的功能将两张图片组装起来。示例代码:<style type=“text/css”> .box { width: 320px; height: 410px; margin: 50px auto; background: url(head.jpg) no-repeat left top, url(foot.jpg) no-repeat left bottom; background-size: contain; background-color: #ccc; }</style><div class=“box”></div>效果图:从效果图中我们可以看到,在盒子里面设置了两张背景图,分别是上面一部分,下面一部分。这里故意给盒子高度拉长了一点,并且设置了一个灰色的背景,为的就是大家能够清楚的看到上下两部分的背景图。7. 渐变渐变是CSS3当中比较丰富多彩的一个特性,通过渐变我们可以实现许多炫丽的效果,有效的减少图片的使用数量,并且具有很强的适应性和可扩展性。7.1 线性渐变linear-gradient线性渐变指沿着某条直线朝一个方向产生渐变效果。1、必要的元素:借助Photoshop总结得出线性渐变的必要元素a、方向b、起始色c、终止色d、渐变距离2、关于方向通过具体的方位词指定to leftto rightto topto bottom通过角度改变渐变的方向0°,从下往上渐变90°,从左向右渐变示例代码:<style type=“text/css”> .box { width: 400px; height: 150px; margin: 100px auto; / 线性渐变 / background-image: linear-gradient( /渐变的方向/ 45deg, /渐变开始的颜色/ #88f5ea, /渐变结束的颜色/ #d36be7 ); }</style><div class=“box”></div>效果图:3、渐变范围如果不设置范围,默认渐变的范围是父盒子的宽度,如果通过background-size设置宽度的话,渐变范围即为设置的宽度。<style> .box { width: 500px; height: 100px; margin: 100px auto; background-image: linear-gradient( 135deg, yellow 20%, black 20%, black 40%, yellow 40%, yellow 60%, black 60%, black 80%, yellow 80%, yellow ); background-size: 66px 100px; }</style><div class=“box”></div>效果图:7.2 径向渐变radial-gradient径向渐变指从一个中心点开始沿着四周产生渐变效果。1、必要的元素:a、辐射范围即圆半径b、中心点 即圆的中心c、渐变起始色d、渐变终止色e、渐变范围2、关于中心点中心位置参照的是盒子的左上角,例如:<style> #div{ width:200px; height:200px; background: radial-gradient( / 100px是渐变辐射的范围 0 0 指的是圆心在盒子的左上角 / 100px at 0 0, /渐变起始色/ orange, /渐变终止色/ #ff4500 ) }</style><div id=“box”></div>示例代码:镜像渐变画个球<style type=“text/css”> * { margin: 0; padding: 0; } html,body { width: 100%; height: 100%; background-color: #ccc; } .box { width: 400px; height: 400px; background-color: #fff; margin: 50px auto; border-radius: 50%; background-image: radial-gradient( 300px at 100px 100px, rgba(0,0,0,.1), rgba(0,0,0,.8) ); }</style><div class=“box”></div>效果图:8. 过渡过渡是CSS3中具有颠覆性的特征之一,可以实现元素不同状态间的平滑过渡(补间动画),经常用来制作动画效果。8.1 帧动画通过一帧一帧的画面按照固定顺序和速度播放,如电影胶片。示例代码:<!– baidu.png这个背景图由64张图片横向组成,我们通过动态改变图片的位置,实现动画效果–><style> body { margin: 0; padding: 0; background-color: #F7F7F7; } .logo { width: 270px; height: 129px; margin: 100px auto; background-image: url(./baidu.png); background-position: 0 0; }</style><div class=“logo”></div><script> var logo = document.querySelector(’.logo’); var offset = -270; var n = 0; setInterval(function () { n++; logo.style.backgroundPosition = offset * n + ‘px 0px’; if(n >= 64) n = 0; },100);</script>效果图:这里不做详细了解,主要是为了区分与补间动画的区别。8.2 补间动画自动完成从起始状态到终止状态的的过渡。语法:transition当前元素只要有“属性”发生变化时,可以平滑的进行过渡,并不仅仅局限于hover状态。transition-property设置过渡属性/设置哪些属性要参加到动画效果中/transition-property: all;transition-duration设置动画过渡执行时间transition-duration: 2s;transition-timing-function设置过渡速度类型transition-timing-function:linear; / ease| ease-in | ease-out | ease-in-out | linear; /transition-delay设置过渡延时/1s后,过渡动画开始过渡/transition-delay: 1s;连写:/ 属性 执行时间 延时时间 过渡类型*/transition: all 2s 1s linear;示例代码:<style type=“text/css”> .box { width: 250px; height: 250px; background-color: pink; margin: 100px auto; transition: all 2s 1s linear; } .box:hover { width: 200px; height: 200px; border-radius:50%; background-color: rgb(25, 221, 247); }</style><div class=“box”></div>效果图:我们可以看到,触发hover事件的时候延迟1s后开始执行动画。示例代码:过渡的实际应用<style type=“text/css”> html,body { margin: 0; padding: 0; width: 100%; height: 100%; } .box { width: 100%; height: 100%; background-color: #eee; } .l_box { float: left; width: 234px; height: 300px; margin: 100px 50px; cursor: pointer; transition: all 0.5s linear; } .l_box:hover { box-shadow: -2px -2px 20px #777; margin-top: 90px; } .r_box { width: 234px; height: 300px; float: left; margin: 100px 0px; background-color: #fff; text-align: center; position: relative; } .cover { position: absolute; bottom: 0; height: 0px; width: 234px; background-color: orange; transition: all 1s linear; } .r_box:hover .cover { height: 100px; }</style><div class=“box”> <div class=“l_box”> <img src=“img/1.jpg” alt=""> </div> <div class=“r_box”> <img src=“img/2.jpg” alt=""> <div class=“cover”></div> </div></div>效果图:9. 2D转换转换(transform)是CSS3中具有颠覆性的特征之一,可以实现元素的位移、旋转、变形、缩放,甚至支持矩阵方式,配合即将学习的过渡和动画知识,可以取代大量之前只能靠Flash才可以实现的效果。9.1 位移CSS3中,通过translate属性对元素进行位移。移动translate(x, y)可以改变元素的位置,x、y可为负值;a、移动位置相当于自身原来位置b、y轴正方向朝下c、除了可以像素值,也可以是百分比,相对于自身的宽度或高度transform: translate(100px, 30px);示例代码<style type=“text/css”> .line { height: 200px; background-color: pink; } .box { width: 100px; height: 100px; background-color: rgb(30, 230, 245); transition: all 1s linear; } .line:hover .box { /* 位移 / transform: translate(100px, 30px); }</style><div class=“line”> <div class=“box”></div></div>效果图:我们可以看到,鼠标移上去之后,蓝色盒子,分别向左和向下移动了一段距离。9.2 缩放缩放scale(x, y)可以对元素进行水平和垂直方向的缩放,x、y的取值可为小数;/宽和高都放大1倍/transform: scale(1.5);示例代码:<style type=“text/css”> .box { width: 200px; height: 200px; background-color: pink; margin: 50px auto; transition: all 2s linear; } .box:hover { / 缩放 / transform: scale(0.5); }</style><div class=“box”></div>效果图:9.3 旋转旋转rotate(deg)可以对元素进行旋转,正值为顺时针,负值为逆时针;a、当元素旋转以后,坐标轴也跟着发生的转变b、调整顺序可以解决,把旋转放到最后/ 顺时针旋转 90度 /transform: rotate(90deg);示例代码:<style type=“text/css”> .box { width: 200px; height: 200px; background-color: #0df3cd; margin: 100px auto; transition: all 2s linear; } .box:hover { transform: rotate(-90deg); }</style><div class=“box”></div>效果图:旋转原点:默认情况下,旋转是按照元素的中心点旋转的,但是我们可以手动设置元素旋转的中心点。transform-origin: 30px 40px;示例代码:<style type=“text/css”> .box { width: 150px; height: 150px; background-color: cyan; margin: 100px auto; transition: all 1s linear; / 设置旋转原点位置 / / transform-origin: left top; / transform-origin: 30px 40px; } .box:hover { transform: rotate(90deg); }</style><div class=“box”></div>效果图:示例代码:扑克牌<style type=“text/css”> * { margin: 0; padding: 0; } .box { width: 310px; height: 438px; border: 1px solid #ccc; margin: 50px auto; position: relative; } .box img { position: absolute; transform-origin:bottom; transition: all 2s linear; } .box:hover img:nth-child(1) { transform: rotate(10deg); } .box:hover img:nth-child(2) { transform: rotate(20deg); } .box:hover img:nth-child(3) { transform: rotate(30deg); } .box:hover img:nth-child(4) { transform: rotate(40deg); } .box:hover img:nth-child(5) { transform: rotate(50deg); } .box:hover img:nth-child(6) { transform: rotate(60deg); }</style><div class=“box”> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""></div>效果图:9.4 倾斜倾斜skew(deg, deg)可以使元素按一定的角度进行倾斜,可为负值,第二个参数不写默认为0。transform: skew(30deg,30deg);示例代码:<style type=“text/css”> .box { width: 150px; height: 150px; background-color: cyan; margin: 100px auto; transition: all 2s linear; } .box:hover { / 倾斜 / transform: skew(30deg, 30deg); }</style><div class=“box”></div>效果图:9.5 矩阵矩阵matrix()把所有的2D转换组合到一起,需要6个参数。transform-origin可以调整元素转换的原点,但是对于transform: translate(x,y)没有影响。我们可以同时使用多个转换,其格式为:transform: translate() rotate() scale() …等,其顺序会影转换的效果。详细可参见10. 3D 转换10.1 3D 坐标轴用X、Y、Z分别表示空间的3个维度,三条轴互相垂直。如下图:网格状的正方形,可以想象成是我们的电脑桌面2D平面。在 3D 转换中,前面 2D 转换的属性在这都可以使用:位移transform:translate(100px,100px,100px);旋转transform:rotate(30deg,30deg,30deg);缩放transform:scale(2,0.5,0.5);倾斜transform:skew(30deg,30deg,30deg);在3D转换中,一定要加上一个透视属性,才能在电脑2D平面中显示出3D的效果,透视属性请看下章。10.2 透视(perspective)电脑显示屏是一个2D平面,图像之所以具有立体感(3D效果),其实只是一种视觉呈现,通过透视可以实现此目的。perspective通过透视产生的3D效果,只是视觉呈现而已,并不是真正的3d立体的盒子;就是近大远小的效果。透视的概念其实很简单,就是“近大远小”。举个例子:在2D转换的时候,我们知道一个translate属性,他分别可以设置向左向右或者向上向下平移,但是却不能向里面或外面平移。<style> .box{ width: 550px; height: 150px; margin: 100px auto; padding: 6px; border: 1px dashed #ccc; } .box li{ float: left; width: 150px; height: 150px; padding: 0; list-style: none; margin-right: 50px; transition: all 0.5s linear; } .box li:first-child{ background: salmon; } .box li:nth-child(2){ background: deepskyblue; } .box li:last-child{ background: khaki; margin-right: 0px; } / 第一个盒子向X轴的负方向移动100px / .box li:first-child:hover{ transform: translateX(-100px); } / 第二个盒子向Y轴的正方向移动100px / .box li:nth-child(2):hover{ transform: translateY(100px); } / 第三个盒子Z轴的负方向移动100px / .box li:last-child:hover{ transform: translateZ(-100px); }</style><ul class=“box”> <li></li> <li></li> <li></li></ul>效果图:没有加透视属性的时候,因为z轴是垂直电脑平面射出来的,translateZ是看不出效果的。如何设置透视属性?给当前元素的直接父元素添加perspective: 800px;透视属性,注意这个值可以是随意的,但是最佳展现距离是600px~1000px。<style> .box{ width: 550px; height: 150px; margin: 100px auto; padding: 6px; border: 1px dashed #ccc; / 给变换的 li 的直接父元素 ul 添加透视属性 perspective / perspective: 800px; } .box li{ float: left; width: 150px; height: 150px; padding: 0; list-style: none; margin-right: 50px; transition: all 0.5s linear; } .box li:first-child{ background: salmon; } .box li:nth-child(2){ background: deepskyblue; } .box li:last-child{ background: khaki; margin-right: 0px; } / 第一个盒子向X轴的负方向移动100px / .box li:first-child:hover{ transform: translateX(-100px); } / 第二个盒子向Y轴的正方向移动100px / .box li:nth-child(2):hover{ transform: translateY(100px); } / 第三个盒子Z轴的负方向移动100px / .box li:last-child:hover{ transform: translateZ(-100px); }</style><ul class=“box”> <li></li> <li></li> <li></li></ul>效果图:如图所示,在ul加上透视属性后,第三个盒子向着z轴的负方向移动了100px。透视可以将一个2D平面,在转换的过程当中,呈现3D效果。(没有perspective,便“没有”Z轴)并非任何情况下都需要透视效果。10.3 3D呈现(transform-style)什么是3D呈现呢?不要与前面的透视搞混,透视只能使一个物体呈现近大远小的状态,不能呈现出一个立体感的东西,比如我在页面上用六个面组成一个正方形,通过透视你可能只能改变他的远近距离,并不能让他看起来像个立体的盒子,所以这里需要用到另一个属性:transform-style。transform-style: preserve-3d | flatflat:所有子元素在2D平面呈现preserve-3d:保留3D空间必须设置在父元素身上并且父元素有转换(就是有变形)并且子元素也得有转换(变形)才能看得到效果。1、示例代码:正方体<style type=“text/css”> * { margin: 0; padding: 0; } .box { width: 200px; height: 200px; margin: 150px auto; position: relative; / 透视 / / perspective: 1000px; / / 转为立方体 / transform-style: preserve-3d; transform: rotateY(45deg) rotateX(30deg); } .box>div { position: absolute; width: 100%; height: 100%; } .left { background-color: pink; transform: rotateY(-90deg) translateZ(150px); } .right { background-color: green; transform: rotateY(90deg) translateZ(150px); } .top { background-color: orange; transform: rotateX(90deg) translateZ(150px); } .bottom { background-color: blue; transform: rotateX(-90deg) translateZ(150px); } .before { background-color: red; transform: translateZ(150px); } .back { background-color: greenyellow; transform: translateZ(-150px); }</style><div class=“box”> <div class=“left”></div> <div class=“right”></div> <div class=“top”></div> <div class=“bottom”></div> <div class=“before”></div> <div class=“back”></div></div>效果图:2、示例代码:3D 导航<style type=“text/css”> * { margin: 0; padding: 0; list-style: none; } nav { width: 600px; height: 60px; line-height: 60px; margin: 200px auto; } li { height: 60px; width: 150px; float: left; position: relative; transform-style: preserve-3d; transition: all 0.5s ease-in; } span { position: absolute; width: 150px; height: 60px; display: block; color: #fff; text-align: center; } span:first-child{ background-color: #ff287a; transform: rotateX(90deg) translateZ(30px); } span:last-child{ transform: translateZ(30px); background-color: #00bdab; } li:hover { transform: rotateX(-90deg); }</style><nav> <ul> <li> <span>Home</span> <span>主页</span> </li> <li> <span>Menu</span> <span>菜单</span> </li> <li> <span>Admin</span> <span>管理</span> </li> <li> <span>About</span> <span>关于我们</span> </li> </ul></nav>效果图:10.4 3D呈现案例:3D轮播图1、普通版 3D 轮播图实现思路:通过CSS3中transform-style: preserve-3d的概念,将视图设置成3D展示模式;四张图片,分别设置其绕着X轴旋转的角度,分别对应四个立体面;将旋转好的图片沿着Z轴平移盒子宽度的一半;定义一个全局变量num,用来记录按钮点击的次数,当当按动按钮的时候,让ul旋转num90°;示例代码:<style> * { padding: 0; margin: 0; list-style-type: none; } .box { width: 960px; height: 540px; /* border: 5px solid #999; / margin: 150px auto; } .box ul { width: 960px; height: 540px; position: relative; transform-style: preserve-3d; transition: transform .6s; } .box ul li { width: 100%; height: 100%; position: absolute; left: 0; top: 0; } img { width: 100%; height: 100%; } .box ul li:nth-child(1) { transform: rotateX(90deg) translateZ(270px); } .box ul li:nth-child(2) { transform: rotateX(-90deg) translateZ(270px); } .box ul li:nth-child(3) { transform: rotateX(180deg) translateZ(270px); } .box ul li:nth-child(4) { transform: translateZ(270px); } .btn { width: 1080px; margin: 0 auto; position: relative; height: 0px; top: -470px; } .btn button { width: 40px; height: 80px; border-radius: 10px; background: rgba(0, 0, 0, 0.2); border: none; outline: none; font: 900 24px/80px ‘宋体’; color: #fff; } .btn button:frist-child { float: left; } .btn button:last-child { / border-radius: 0 10px 10px 0; / float: right; }</style><div class=“box”> <ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li> </ul></div><div class=“btn”> <button>&lt;</button> <button>&gt;</button></div><script type=“text/javascript”> var btn = document.querySelectorAll(‘button’); var box = document.querySelector(’.box’); var _ul = box.querySelector(‘ul’); var num = 0; // btn 获取到的是一个伪数组 btn[1].onclick = function () { num++; _ul.style.transform = ‘rotateX(’ + num * 90 + ‘deg)’ } btn[0].onclick = function () { num–; _ul.style.transform = ‘rotateX(’ + num * 90 + ‘deg)’; }</script>效果图:2、切割版 3D 轮播图实现思路:结构上将之前定义好的ul重复四次;设定延时,整个动画执行时间是0.8s,设定每一个ul延迟执行0.2s,即第一个延时0s,第二个延时0.2s,第三个延时0.4s,第四个延时0.6s;其余步骤同上,着重强调的是,相对于上面的案例,本案例给按钮加了限制,监听第一次所有的ul旋转结束之后,按钮才能再一次被点击。示例代码:<style> * { padding: 0; margin: 0; list-style-type: none; } .box { width: 960px; height: 540px; margin: 150px auto; display: flex; } .box ul { width: 960px; height: 540px; position: relative; transform-style: preserve-3d; transition: transform .8s; } .box ul li { width: 100%; height: 100%; position: absolute; left: 0; top: 0; overflow: hidden; } img { width: 960px; height: 540px; } / 设定延时 / .box ul:nth-child(1) { transition-delay:0s; } .box ul:nth-child(2) { transition-delay:.2s; } .box ul:nth-child(3) { transition-delay:.4s; } .box ul:nth-child(4) { transition-delay:.6s; } / 拼凑图片 / .box ul:nth-child(2) img { margin-left: -240px; } .box ul:nth-child(3) img { margin-left: -480px; } .box ul:nth-child(4) img { margin-left: -720px; } .box ul li:nth-child(1) { transform: rotateX(90deg) translateZ(270px); } .box ul li:nth-child(2) { transform: rotateX(-90deg) translateZ(270px); } .box ul li:nth-child(3) { transform: rotateX(180deg) translateZ(270px); } .box ul li:nth-child(4) { transform: translateZ(270px); } .btn { width: 1080px; margin: 0 auto; position: relative; height: 0px; top: -470px; } .btn button { width: 40px; height: 80px; border-radius: 10px; background: rgba(0, 0, 0, 0.2); border: none; outline: none; font: 900 24px/80px ‘宋体’; color: #fff; } .btn button:frist-child { float: left; } .btn button:last-child { / border-radius: 0 10px 10px 0; */ float: right; }</style><div class=“box”><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul></div><div class=“btn”><button>&lt;</button><button>&gt;</button></div><script type=“text/javascript”> var btn = document.querySelectorAll(‘button’); var box = document.querySelector(’.box’); var _ul = box.querySelectorAll(‘ul’); var num = 0; var flag = true; // btn 获取到的是一个伪数组 btn[1].onclick = function () { if (flag) { flag = false; num++; for (var i = 0; i < _ul.length; i++) { _ul[i].style.transform = ‘rotateX(’ + num * 90 + ‘deg)’; } // 监听最后一个transition事件结束的时候 将flag 置为 true 防止重复点击 _ul[_ul.length - 1].addEventListener(’transitionend’, function () { flag = true; }) } } btn[0].onclick = function () { if (flag) { flag = false; num–; for (var i = 0; i < _ul.length; i++) { _ul[i].style.transform = ‘rotateX(’ + num * 90 + ‘deg)’; } _ul[_ul.length - 1].addEventListener(’transitionend’, function () { flag = true; }) } }</script>效果图:10.5 backface-visibilitybackface-visibility属性定义当元素不面向屏幕时是否可见。如果在旋转元素不希望看到其背面时,该属性很有用。有两个属性:1、visible背面是可见的2、hidden背面是不可见的 ...

November 26, 2018 · 11 min · jiezi

CSS3 入门详解(一)

前言如同人类的的进化一样,CSS3是CSS2的“进化”版本,在CSS2基础上,增强或新增了许多特性,弥补了CSS2的众多不足之处,使得Web开发变得更为高效和便捷。1.选择器CSS3新增了许多灵活查找元素的方法,极大的提高了查找元素的效率和精准度。CSS3选择器与jQuery中所提供的绝大部分选择器兼容。1.1 属性选择器属性选择器就是通过属性来选择元素。选择器含义[attr]存在attr属性即可[attr=val]属性值完全等于val[attr*=val]属性值里包含val字符并且在“任意”位置[attr^=val]属性值里包含val字符并且在“开始”位置[attr$=val]属性值里包含val字符并且在“结束”位置1、[attr]<style> /* 所有拥有class属性的标签,添加color样式 / [class]{ color: #333; }</style>2、[attr=val]<style> / 所有拥有class属性全等于“one”的标签,添加color样式 / [class = “one”]{ color: #333; }</style>3、[attr=val]<style> /* class属性的值里面包含“one”的标签,添加color样式 / [attr=“one”]{ color: #333; }</style>4、[attr^=val]<style> /* class属性的值以“one”开始的标签,添加color样式 / [attr ^= “one”]{ color: #333; }</style>5、[attr$=val]<style> / class属性的值以“one”结束的标签,添加color样式 / [attr $= “one”]{ color: #333; }</style>1.2 伪类选择器除了以前介绍的的:link、:active、:visited、:hover,CSS3又新增了其它的伪类选择器。1、结构(位置)伪类选择器含义:first-child其父元素的第1个子元素:last-child其父元素的最后1个子元素:nth-child(n)其父元素的第n个子元素:nth-last-child(n)其父元素的第n个子元素(倒着数)2、空伪类:empty选中没有任何子节点的元素<style> div:empty { / 没有子元素的div元素 / width: 100px; height: 100px; background-color: pink; }</style><!– css 样式不起作用 –><div class=“one”>阿斯蒂芬</div> <!– css样式不起作用 –><div> <p></p></div><!– css样式生效 –><div></div>3、目标伪类:target结合锚点进行使用,处于当前锚点的元素会被选中;<style type=“text/css”> / 使用锚链接指向当前标签的时候 / .one:target { background-color: pink; font-size: 30px; }</style><a href="#hh">找笑脸去</a><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p id=“hh” class=“one”>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p><p>阿斯顿发撒旦法撒打发放大法的撒双方都</p>4、排除伪类:not(selector)除selector(任意选择器)外的元素会被选中;<style> / 除了类名是“.not”的div元素 / div:not(.one) { width: 100px; height: 100px; background-color: pink; }</style><!– css样式不生效 –><div class=“one”></div><!– css样式生效 –><p></p><!– css样式生效 –><div></div>1.3 伪元素选择器1、::first-letter文本的第一个单词或字(如中文、日文、韩文等)2、::first-line 文本第一行;3、::selection 可改变选中文本的样式;4、::before和::after示例代码:伪元素实现横竖分割线<style type=“text/css”> * { margin: 0; padding: 0; list-style: none; } .box { width: 300px; height: 200px; background-color: pink; margin: 50px auto; } .box li { width: 100px; height: 100px; float: left; background-color: #555; position: relative; overflow: hidden; } li:before { content: “”; display: block; width: 90px; height: 1px; background-color: #ccc; position: absolute; top: 97px; left: 5px; } li:after { content: “”; display: block; width: 1px; height: 90px; background-color: #ccc; position: absolute; left: 0px; top: 4px; } li:nth-child(1):after,li:nth-child(4):after { display: none; } li:nth-last-child(-n+3):before { display: none; }</style><div class=“box”> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul></div>效果图::after、:before在旧版本里是伪元素,CSS3的规范里“:”用来表示伪类,“::”用来表示伪元素,但是在高版本浏览器下:after、:before会被自动识别为::after、::before,这样做的目的是用来做兼容处理。2. 颜色新增了RGBA、HSLA模式,其中的A表示透明度,即可以设置颜色值的透明度,相较opacity,它们不具有继承性,即不会影响子元素的透明度。2.1 RGBARed、Green、Blue、Alpha即RGBA,R、G、B取值范围0255。<style> #box{ width:100px; height:100px; background: rgba(200,200,200,.5); }</style><div id=“box”></div>2.2 HSLAH 色调 取值范围0360,0/360表示红色、120表示绿色、240表示蓝色S 饱和度 取值范围0%100%L 亮度 取值范围0%100%A 透明度 取值范围01<style> #box{ width:100px; height:100px; background: hsla(200,50%,50%,.5); }</style><div id=“box”></div>2.3 关于 CSS 的透明度Alpha和opacity的区别主要就是,opacity具有继承性,父盒子设置该属性,下面所有的子元素都会继承该属性。transparent不可调节透明度,始终完全透明。3. 文本阴影text-shadow,可分别设置偏移量、模糊度、颜色(可设透明度)。如:text-shadow: 2px 2px 2px #CCC;1、水平偏移量 正值向右 负值向左;2、垂直偏移量 正值向下 负值向上;3、模糊度是不能为负值;4、阴影颜色。示例代码:文字浮雕<style> html,body { margin: 0; padding: 0; width: 100%; height: 100%; background-color: #999; font-size: 50px; text-align: center; line-height: 260px; color: #999; } .one { text-shadow: -1px -1px 1px #fff,1px 1px 1px #000; } .two { text-shadow: -1px -1px 1px #000,1px 1px 1px #fff; }</style><div class=“one”>我是凸起文字</div><div class=“two”>我是凹下去的文字</div>效果图:4. 盒模型CSS3中可以通过box-sizing来指定盒模型,即可指定为content-box、border-box,这样我们计算盒子大小的方式就发生了改变。可以分成两种情况:1、box-sizing: border-box盒子大小为width2、box-sizing: content-box盒子大小为width + padding + border注:上面的标注的width指的是CSS属性里设置的width: length,content的值是会自动调整的。示例代码:<style type=“text/css”> .box { width: 316px; height: 170px; float: left; margin-left: 20px; box-sizing: border-box; } .box img { width: 100%; height: 100%; } .box:hover { border: 10px solid #00eeff; }</style><div class=“box”> <img src=“1.jpg” alt=""></div><div class=“box”> <img src=“1.jpg” alt=""></div>效果图:可以看出通过设置盒子模型后,虽然.box设置了边框,但是整个box的盒子大小没有改变。5. 边框边框中的边框圆角、边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特征。5.1 边框圆角通过border-radius属性,设置边框圆角,圆角处理时,脑中要形成圆、圆心、横轴、纵轴的概念,正圆是椭圆的一种特殊情况。为了方便表述,我们将四个角标记成1、2、3、4,如2代表右上角,CSS里提供了border-radius来设置这些角横纵轴半径值。分别设置横纵轴半径,以“/”进行分隔,遵循“1,2,3,4”规则,“/”前面的14个用来设置横轴半径(分别对应横轴1、2、3、4位置),“/”后面1~4个参数用来设置纵轴半径(分别对应纵轴1、2、3、4位置 )。<style type=“text/css”> .box { margin: 50px auto; width: 300px; height: 500px; border: 1px solid #ccc; border-radius: 10px 20px 50px 70px / 10px 20px 50px 70px; }</style><div class=“box”></div>效果图:一般情况下,我们用不到这么复杂的,除非特殊需求的时候。支持简写模式,具体如下:1、border-radius: 10px;表示四个角的横纵轴半径都为10px;2、border-radius: 10px 5px;表示1和3角横纵轴半径都为10px,2和4角横纵轴半径为5px;3、border-radius: 10px 5px 8px;表示1角模纵轴半径都为10px,2和4角横纵轴半径都为8px,3角的横纵轴半径都为8px;4、border-radius: 10px 8px 6px 4px;表示1角横纵轴半径都为10px,表示2角横纵轴半径都为8px,表示3角横纵轴半径都为6px,表示4角横纵轴半径都为6px;椭圆的画法:<style type=“text/css”> .box { margin: 50px auto; width: 300px; height: 500px; border: 1px solid #ccc; / 当盒子长宽不一致时,圆角属性 分别设置宽度的一半,以及长度的一半,即是椭圆 / / 或者直接 border-radius:50%; / border-radius: 150px 250px; }</style><div class=“box”></div>如果不想计算,直接设百分比:“50%”。正圆的画法:<style type=“text/css”> .box { margin: 50px auto; width: 200px; height: 200px; border: 1px solid #ccc; / 当盒子长宽相等时,圆角属性分别设置宽度的一半,以及长度的一半,即是正圆 / / 或者直接 border-radius:50%; / border-radius: 100px; }</style><div class=“box”></div>示例代码:边框圆角合集<style> * { margin: 0; padding: 0; list-style: none; background-color: wheat; overflow: hidden; } .box { width: 980px; height: 400px; background-color: #fff; margin: 50px auto; } .box li { float: left; width: 193px; height: 193px; background-color: #fff; margin:5px; box-shadow: 2px 3px 5px #aaa; } li:first-child:after { content: “”; height: 130px; width: 130px; margin: 30px auto; display: block; border: 1px solid orangered; border-radius: 50%; } li:nth-child(2):after { content: “”; display: block; height: 130px; width: 130px; border: 1px solid orangered; margin: 30px auto; border-radius: 65px 65px 0px 0px; } li:nth-child(3):after { content: “”; display: block; width: 130px; height: 65px; border: 1px solid orangered; margin: 50px auto; border-radius: 65px 65px 0px 0px; } li:nth-child(4):after { content: “”; display: block; width: 130px; height: 130px; border: 1px solid orangered; margin: 20px auto; border-radius: 65px 0px 0px 0px; } li:nth-child(5):after { content: “”; width: 130px; height: 65px; display: block; border: 1px solid orangered; border-radius: 50%; margin: 50px auto; } li:nth-child(6):after{ content: “”; height: 130px; width: 65px; display: block; border: 1px solid orangered; border-radius: 50%; margin: 20px auto; } li:nth-child(7):after { content: “”; height: 130px; width: 130px; display: block; border: 1px solid orangered; margin: 20px auto; border-radius: 135px 0px 0px 0px; } li:nth-child(8):after { content: “”; width: 135px; height: 30px; display: block; margin: 30px auto; border: 1px solid orangered; border-radius: 65px 65px 0px 0px / 30px 30px 0px 0px; }</style><div class=“box”> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> </ul></div>效果图:5.2 边框阴影box-shadow,与文字阴影类似,可分别设置盒子阴影偏移量、模糊度、颜色(可设透明度)。如:box-shadow: 5px 5px 5px #CCC1、水平偏移量 正值向右 负值向左;2、垂直偏移量 正值向下 负值向上;3、模糊度是不能为负值;4、inset可以设置内阴影;注:设置边框阴影不会改变盒子的大小,即不会影响其兄弟元素的布局。可以设置多重边框阴影,实现更好的效果,增强立体感,符合渐进增强,实际开发中可以大胆使用。示例代码:<style> .box { width: 200px; height: 200px; margin: 50px auto; border: 1px dashed #000; box-shadow: 2px 3px 4px rgba(0, 247, 255, 0.452),inset 5px 6px 7px rgba(255, 0, 140, 0.562); }</style><div class=“box”></div>效果图:我们通过上图可以看到,虚线是盒子的位置,粉色阴影是inset属性设置的,所以是内阴影,浅蓝色是直接设置的外阴影,效果一目了然。6. 背景背景在CSS3中也得到很大程度的增强,比如背景图片尺寸、背景裁切区域、背景定位参照点、多重背景等。6.1 background-size通过background-size设置背景图片的尺寸,就像我们设置img的尺寸一样,在移动Web开发中做屏幕适配应用非常广泛。其参数设置如下:可以设置长度单位(px)或百分比(设置百分比时,参照盒子的宽高)设置为cover时,会自动调整缩放比例,保证图片始终填充满背景区域,如有溢出部分则会被隐藏。设置为contain会自动调整缩放比例,保证图片始终完整显示在背景区域。6.2 background-origin通过background-origin可以设置背景图片定位(background-position)的参照原点。其参数设置如下:border-box以边框做为参考原点;padding-box以内边距做为参考原点;content-box以内容区做为参考点;示例代码:<style type=“text/css”> .box1,.box2,.box3 { width: 200px; height: 200px; display: inline-block; margin: 50px 30px; border: 10px dashed aquamarine; padding: 10px; background-image: url(bg.jpg); background-repeat: no-repeat; } .box1{ background-origin: padding-box; } .box2{ background-origin: content-box; } .box3{ background-origin: border-box; }</style><div class=“box1”></div><div class=“box2”></div><div class=“box3”></div>效果图:6.3 background-clip通过background-clip,可以设置对背景区域进行裁切,即改变背景区域的大小。其参数设置如下:border-box裁切边框以内为背景区域;padding-box裁切内边距以内为背景区域;content-box裁切内容区做为背景区域;6.4 多背景以逗号分隔可以设置多背景,可用于自适应布局。在一个盒子里可以设置多个背景图片,通过背景定位的功能将两张图片组装起来。示例代码:<style type=“text/css”> .box { width: 320px; height: 410px; margin: 50px auto; background: url(head.jpg) no-repeat left top, url(foot.jpg) no-repeat left bottom; background-size: contain; background-color: #ccc; }</style><div class=“box”></div>效果图:从效果图中我们可以看到,在盒子里面设置了两张背景图,分别是上面一部分,下面一部分。这里故意给盒子高度拉长了一点,并且设置了一个灰色的背景,为的就是大家能够清楚的看到上下两部分的背景图。7. 渐变渐变是CSS3当中比较丰富多彩的一个特性,通过渐变我们可以实现许多炫丽的效果,有效的减少图片的使用数量,并且具有很强的适应性和可扩展性。7.1 线性渐变linear-gradient线性渐变指沿着某条直线朝一个方向产生渐变效果。1、必要的元素:借助Photoshop总结得出线性渐变的必要元素a、方向b、起始色c、终止色d、渐变距离2、关于方向通过具体的方位词指定to leftto rightto topto bottom通过角度改变渐变的方向0°,从下往上渐变90°,从左向右渐变示例代码:<style type=“text/css”> .box { width: 400px; height: 150px; margin: 100px auto; / 线性渐变 / background-image: linear-gradient( /渐变的方向/ 45deg, /渐变开始的颜色/ #88f5ea, /渐变结束的颜色/ #d36be7 ); }</style><div class=“box”></div>效果图:3、渐变范围如果不设置范围,默认渐变的范围是父盒子的宽度,如果通过background-size设置宽度的话,渐变范围即为设置的宽度。<style> .box { width: 500px; height: 100px; margin: 100px auto; background-image: linear-gradient( 135deg, yellow 20%, black 20%, black 40%, yellow 40%, yellow 60%, black 60%, black 80%, yellow 80%, yellow ); background-size: 66px 100px; }</style><div class=“box”></div>效果图:7.2 径向渐变radial-gradient径向渐变指从一个中心点开始沿着四周产生渐变效果。1、必要的元素:a、辐射范围即圆半径b、中心点 即圆的中心c、渐变起始色d、渐变终止色e、渐变范围2、关于中心点中心位置参照的是盒子的左上角,例如:<style> #div{ width:200px; height:200px; background: radial-gradient( / 100px是渐变辐射的范围 0 0 指的是圆心在盒子的左上角 / 100px at 0 0, /渐变起始色/ orange, /渐变终止色/ #ff4500 ) }</style><div id=“box”></div>示例代码:镜像渐变画个球<style type=“text/css”> * { margin: 0; padding: 0; } html,body { width: 100%; height: 100%; background-color: #ccc; } .box { width: 400px; height: 400px; background-color: #fff; margin: 50px auto; border-radius: 50%; background-image: radial-gradient( 300px at 100px 100px, rgba(0,0,0,.1), rgba(0,0,0,.8) ); }</style><div class=“box”></div>效果图:8. 过渡过渡是CSS3中具有颠覆性的特征之一,可以实现元素不同状态间的平滑过渡(补间动画),经常用来制作动画效果。8.1 帧动画通过一帧一帧的画面按照固定顺序和速度播放,如电影胶片。示例代码:<!– baidu.png这个背景图由64张图片横向组成,我们通过动态改变图片的位置,实现动画效果–><style> body { margin: 0; padding: 0; background-color: #F7F7F7; } .logo { width: 270px; height: 129px; margin: 100px auto; background-image: url(./baidu.png); background-position: 0 0; }</style><div class=“logo”></div><script> var logo = document.querySelector(’.logo’); var offset = -270; var n = 0; setInterval(function () { n++; logo.style.backgroundPosition = offset * n + ‘px 0px’; if(n >= 64) n = 0; },100);</script>效果图:这里不做详细了解,主要是为了区分与补间动画的区别。8.2 补间动画自动完成从起始状态到终止状态的的过渡。语法:transition当前元素只要有“属性”发生变化时,可以平滑的进行过渡,并不仅仅局限于hover状态。transition-property设置过渡属性/设置哪些属性要参加到动画效果中/transition-property: all;transition-duration设置动画过渡执行时间transition-duration: 2s;transition-timing-function设置过渡速度类型transition-timing-function:linear; / ease| ease-in | ease-out | ease-in-out | linear; /transition-delay设置过渡延时/1s后,过渡动画开始过渡/transition-delay: 1s;连写:/ 属性 执行时间 延时时间 过渡类型*/transition: all 2s 1s linear;示例代码:<style type=“text/css”> .box { width: 250px; height: 250px; background-color: pink; margin: 100px auto; transition: all 2s 1s linear; } .box:hover { width: 200px; height: 200px; border-radius:50%; background-color: rgb(25, 221, 247); }</style><div class=“box”></div>效果图:我们可以看到,触发hover事件的时候延迟1s后开始执行动画。示例代码:过渡的实际应用<style type=“text/css”> html,body { margin: 0; padding: 0; width: 100%; height: 100%; } .box { width: 100%; height: 100%; background-color: #eee; } .l_box { float: left; width: 234px; height: 300px; margin: 100px 50px; cursor: pointer; transition: all 0.5s linear; } .l_box:hover { box-shadow: -2px -2px 20px #777; margin-top: 90px; } .r_box { width: 234px; height: 300px; float: left; margin: 100px 0px; background-color: #fff; text-align: center; position: relative; } .cover { position: absolute; bottom: 0; height: 0px; width: 234px; background-color: orange; transition: all 1s linear; } .r_box:hover .cover { height: 100px; }</style><div class=“box”> <div class=“l_box”> <img src=“img/1.jpg” alt=""> </div> <div class=“r_box”> <img src=“img/2.jpg” alt=""> <div class=“cover”></div> </div></div>效果图:9. 2D转换转换(transform)是CSS3中具有颠覆性的特征之一,可以实现元素的位移、旋转、变形、缩放,甚至支持矩阵方式,配合即将学习的过渡和动画知识,可以取代大量之前只能靠Flash才可以实现的效果。9.1 位移CSS3中,通过translate属性对元素进行位移。移动translate(x, y)可以改变元素的位置,x、y可为负值;a、移动位置相当于自身原来位置b、y轴正方向朝下c、除了可以像素值,也可以是百分比,相对于自身的宽度或高度transform: translate(100px, 30px);示例代码<style type=“text/css”> .line { height: 200px; background-color: pink; } .box { width: 100px; height: 100px; background-color: rgb(30, 230, 245); transition: all 1s linear; } .line:hover .box { /* 位移 / transform: translate(100px, 30px); }</style><div class=“line”> <div class=“box”></div></div>效果图:我们可以看到,鼠标移上去之后,蓝色盒子,分别向左和向下移动了一段距离。9.2 缩放缩放scale(x, y)可以对元素进行水平和垂直方向的缩放,x、y的取值可为小数;/宽和高都放大1倍/transform: scale(1.5);示例代码:<style type=“text/css”> .box { width: 200px; height: 200px; background-color: pink; margin: 50px auto; transition: all 2s linear; } .box:hover { / 缩放 / transform: scale(0.5); }</style><div class=“box”></div>效果图:9.3 旋转旋转rotate(deg)可以对元素进行旋转,正值为顺时针,负值为逆时针;a、当元素旋转以后,坐标轴也跟着发生的转变b、调整顺序可以解决,把旋转放到最后/ 顺时针旋转 90度 /transform: rotate(90deg);示例代码:<style type=“text/css”> .box { width: 200px; height: 200px; background-color: #0df3cd; margin: 100px auto; transition: all 2s linear; } .box:hover { transform: rotate(-90deg); }</style><div class=“box”></div>效果图:旋转原点:默认情况下,旋转是按照元素的中心点旋转的,但是我们可以手动设置元素旋转的中心点。transform-origin: 30px 40px;示例代码:<style type=“text/css”> .box { width: 150px; height: 150px; background-color: cyan; margin: 100px auto; transition: all 1s linear; / 设置旋转原点位置 / / transform-origin: left top; / transform-origin: 30px 40px; } .box:hover { transform: rotate(90deg); }</style><div class=“box”></div>效果图:示例代码:扑克牌<style type=“text/css”> * { margin: 0; padding: 0; } .box { width: 310px; height: 438px; border: 1px solid #ccc; margin: 50px auto; position: relative; } .box img { position: absolute; transform-origin:bottom; transition: all 2s linear; } .box:hover img:nth-child(1) { transform: rotate(10deg); } .box:hover img:nth-child(2) { transform: rotate(20deg); } .box:hover img:nth-child(3) { transform: rotate(30deg); } .box:hover img:nth-child(4) { transform: rotate(40deg); } .box:hover img:nth-child(5) { transform: rotate(50deg); } .box:hover img:nth-child(6) { transform: rotate(60deg); }</style><div class=“box”> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""> <img src=“img/pk1.png” alt=""></div>效果图:9.4 倾斜倾斜skew(deg, deg)可以使元素按一定的角度进行倾斜,可为负值,第二个参数不写默认为0。transform: skew(30deg,30deg);示例代码:<style type=“text/css”> .box { width: 150px; height: 150px; background-color: cyan; margin: 100px auto; transition: all 2s linear; } .box:hover { / 倾斜 / transform: skew(30deg, 30deg); }</style><div class=“box”></div>效果图:9.5 矩阵矩阵matrix()把所有的2D转换组合到一起,需要6个参数。transform-origin可以调整元素转换的原点,但是对于transform: translate(x,y)没有影响。我们可以同时使用多个转换,其格式为:transform: translate() rotate() scale() …等,其顺序会影转换的效果。详细可参见10. 3D 转换10.1 3D 坐标轴用X、Y、Z分别表示空间的3个维度,三条轴互相垂直。如下图:网格状的正方形,可以想象成是我们的电脑桌面2D平面。在 3D 转换中,前面 2D 转换的属性在这都可以使用:位移transform:translate(100px,100px,100px);旋转transform:rotate(30deg,30deg,30deg);缩放transform:scale(2,0.5,0.5);倾斜transform:skew(30deg,30deg,30deg);在3D转换中,一定要加上一个透视属性,才能在电脑2D平面中显示出3D的效果,透视属性请看下章。10.2 透视(perspective)电脑显示屏是一个2D平面,图像之所以具有立体感(3D效果),其实只是一种视觉呈现,通过透视可以实现此目的。perspective通过透视产生的3D效果,只是视觉呈现而已,并不是真正的3d立体的盒子;就是近大远小的效果。透视的概念其实很简单,就是“近大远小”。举个例子:在2D转换的时候,我们知道一个translate属性,他分别可以设置向左向右或者向上向下平移,但是却不能向里面或外面平移。<style> .box{ width: 550px; height: 150px; margin: 100px auto; padding: 6px; border: 1px dashed #ccc; } .box li{ float: left; width: 150px; height: 150px; padding: 0; list-style: none; margin-right: 50px; transition: all 0.5s linear; } .box li:first-child{ background: salmon; } .box li:nth-child(2){ background: deepskyblue; } .box li:last-child{ background: khaki; margin-right: 0px; } / 第一个盒子向X轴的负方向移动100px / .box li:first-child:hover{ transform: translateX(-100px); } / 第二个盒子向Y轴的正方向移动100px / .box li:nth-child(2):hover{ transform: translateY(100px); } / 第三个盒子Z轴的负方向移动100px / .box li:last-child:hover{ transform: translateZ(-100px); }</style><ul class=“box”> <li></li> <li></li> <li></li></ul>效果图:没有加透视属性的时候,因为z轴是垂直电脑平面射出来的,translateZ是看不出效果的。如何设置透视属性?给当前元素的直接父元素添加perspective: 800px;透视属性,注意这个值可以是随意的,但是最佳展现距离是600px~1000px。<style> .box{ width: 550px; height: 150px; margin: 100px auto; padding: 6px; border: 1px dashed #ccc; / 给变换的 li 的直接父元素 ul 添加透视属性 perspective / perspective: 800px; } .box li{ float: left; width: 150px; height: 150px; padding: 0; list-style: none; margin-right: 50px; transition: all 0.5s linear; } .box li:first-child{ background: salmon; } .box li:nth-child(2){ background: deepskyblue; } .box li:last-child{ background: khaki; margin-right: 0px; } / 第一个盒子向X轴的负方向移动100px / .box li:first-child:hover{ transform: translateX(-100px); } / 第二个盒子向Y轴的正方向移动100px / .box li:nth-child(2):hover{ transform: translateY(100px); } / 第三个盒子Z轴的负方向移动100px / .box li:last-child:hover{ transform: translateZ(-100px); }</style><ul class=“box”> <li></li> <li></li> <li></li></ul>效果图:如图所示,在ul加上透视属性后,第三个盒子向着z轴的负方向移动了100px。透视可以将一个2D平面,在转换的过程当中,呈现3D效果。(没有perspective,便“没有”Z轴)并非任何情况下都需要透视效果。10.3 3D呈现(transform-style)什么是3D呈现呢?不要与前面的透视搞混,透视只能使一个物体呈现近大远小的状态,不能呈现出一个立体感的东西,比如我在页面上用六个面组成一个正方形,通过透视你可能只能改变他的远近距离,并不能让他看起来像个立体的盒子,所以这里需要用到另一个属性:transform-style。transform-style: preserve-3d | flatflat:所有子元素在2D平面呈现preserve-3d:保留3D空间必须设置在父元素身上并且父元素有转换(就是有变形)并且子元素也得有转换(变形)才能看得到效果。1、示例代码:正方体<style type=“text/css”> * { margin: 0; padding: 0; } .box { width: 200px; height: 200px; margin: 150px auto; position: relative; / 透视 / / perspective: 1000px; / / 转为立方体 / transform-style: preserve-3d; transform: rotateY(45deg) rotateX(30deg); } .box>div { position: absolute; width: 100%; height: 100%; } .left { background-color: pink; transform: rotateY(-90deg) translateZ(150px); } .right { background-color: green; transform: rotateY(90deg) translateZ(150px); } .top { background-color: orange; transform: rotateX(90deg) translateZ(150px); } .bottom { background-color: blue; transform: rotateX(-90deg) translateZ(150px); } .before { background-color: red; transform: translateZ(150px); } .back { background-color: greenyellow; transform: translateZ(-150px); }</style><div class=“box”> <div class=“left”></div> <div class=“right”></div> <div class=“top”></div> <div class=“bottom”></div> <div class=“before”></div> <div class=“back”></div></div>效果图:2、示例代码:3D 导航<style type=“text/css”> * { margin: 0; padding: 0; list-style: none; } nav { width: 600px; height: 60px; line-height: 60px; margin: 200px auto; } li { height: 60px; width: 150px; float: left; position: relative; transform-style: preserve-3d; transition: all 0.5s ease-in; } span { position: absolute; width: 150px; height: 60px; display: block; color: #fff; text-align: center; } span:first-child{ background-color: #ff287a; transform: rotateX(90deg) translateZ(30px); } span:last-child{ transform: translateZ(30px); background-color: #00bdab; } li:hover { transform: rotateX(-90deg); }</style><nav> <ul> <li> <span>Home</span> <span>主页</span> </li> <li> <span>Menu</span> <span>菜单</span> </li> <li> <span>Admin</span> <span>管理</span> </li> <li> <span>About</span> <span>关于我们</span> </li> </ul></nav>效果图:10.4 3D呈现案例:3D轮播图1、普通版 3D 轮播图实现思路:通过CSS3中transform-style: preserve-3d的概念,将视图设置成3D展示模式;四张图片,分别设置其绕着X轴旋转的角度,分别对应四个立体面;将旋转好的图片沿着Z轴平移盒子宽度的一半;定义一个全局变量num,用来记录按钮点击的次数,当当按动按钮的时候,让ul旋转num90°;示例代码:<style> * { padding: 0; margin: 0; list-style-type: none; } .box { width: 960px; height: 540px; /* border: 5px solid #999; / margin: 150px auto; } .box ul { width: 960px; height: 540px; position: relative; transform-style: preserve-3d; transition: transform .6s; } .box ul li { width: 100%; height: 100%; position: absolute; left: 0; top: 0; } img { width: 100%; height: 100%; } .box ul li:nth-child(1) { transform: rotateX(90deg) translateZ(270px); } .box ul li:nth-child(2) { transform: rotateX(-90deg) translateZ(270px); } .box ul li:nth-child(3) { transform: rotateX(180deg) translateZ(270px); } .box ul li:nth-child(4) { transform: translateZ(270px); } .btn { width: 1080px; margin: 0 auto; position: relative; height: 0px; top: -470px; } .btn button { width: 40px; height: 80px; border-radius: 10px; background: rgba(0, 0, 0, 0.2); border: none; outline: none; font: 900 24px/80px ‘宋体’; color: #fff; } .btn button:frist-child { float: left; } .btn button:last-child { / border-radius: 0 10px 10px 0; / float: right; }</style><div class=“box”> <ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li> </ul></div><div class=“btn”> <button>&lt;</button> <button>&gt;</button></div><script type=“text/javascript”> var btn = document.querySelectorAll(‘button’); var box = document.querySelector(’.box’); var _ul = box.querySelector(‘ul’); var num = 0; // btn 获取到的是一个伪数组 btn[1].onclick = function () { num++; _ul.style.transform = ‘rotateX(’ + num * 90 + ‘deg)’ } btn[0].onclick = function () { num–; _ul.style.transform = ‘rotateX(’ + num * 90 + ‘deg)’; }</script>效果图:2、切割版 3D 轮播图实现思路:结构上将之前定义好的ul重复四次;设定延时,整个动画执行时间是0.8s,设定每一个ul延迟执行0.2s,即第一个延时0s,第二个延时0.2s,第三个延时0.4s,第四个延时0.6s;其余步骤同上,着重强调的是,相对于上面的案例,本案例给按钮加了限制,监听第一次所有的ul旋转结束之后,按钮才能再一次被点击。示例代码:<style> * { padding: 0; margin: 0; list-style-type: none; } .box { width: 960px; height: 540px; margin: 150px auto; display: flex; } .box ul { width: 960px; height: 540px; position: relative; transform-style: preserve-3d; transition: transform .8s; } .box ul li { width: 100%; height: 100%; position: absolute; left: 0; top: 0; overflow: hidden; } img { width: 960px; height: 540px; } / 设定延时 / .box ul:nth-child(1) { transition-delay:0s; } .box ul:nth-child(2) { transition-delay:.2s; } .box ul:nth-child(3) { transition-delay:.4s; } .box ul:nth-child(4) { transition-delay:.6s; } / 拼凑图片 / .box ul:nth-child(2) img { margin-left: -240px; } .box ul:nth-child(3) img { margin-left: -480px; } .box ul:nth-child(4) img { margin-left: -720px; } .box ul li:nth-child(1) { transform: rotateX(90deg) translateZ(270px); } .box ul li:nth-child(2) { transform: rotateX(-90deg) translateZ(270px); } .box ul li:nth-child(3) { transform: rotateX(180deg) translateZ(270px); } .box ul li:nth-child(4) { transform: translateZ(270px); } .btn { width: 1080px; margin: 0 auto; position: relative; height: 0px; top: -470px; } .btn button { width: 40px; height: 80px; border-radius: 10px; background: rgba(0, 0, 0, 0.2); border: none; outline: none; font: 900 24px/80px ‘宋体’; color: #fff; } .btn button:frist-child { float: left; } .btn button:last-child { / border-radius: 0 10px 10px 0; */ float: right; }</style><div class=“box”><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul><ul> <li><img src="./imgs/1.jpg" alt=""></li> <li><img src="./imgs/2.jpg" alt=""></li> <li><img src="./imgs/3.jpg" alt=""></li> <li><img src="./imgs/4.jpg" alt=""></li></ul></div><div class=“btn”><button>&lt;</button><button>&gt;</button></div><script type=“text/javascript”> var btn = document.querySelectorAll(‘button’); var box = document.querySelector(’.box’); var _ul = box.querySelectorAll(‘ul’); var num = 0; var flag = true; // btn 获取到的是一个伪数组 btn[1].onclick = function () { if (flag) { flag = false; num++; for (var i = 0; i < _ul.length; i++) { _ul[i].style.transform = ‘rotateX(’ + num * 90 + ‘deg)’; } // 监听最后一个transition事件结束的时候 将flag 置为 true 防止重复点击 _ul[_ul.length - 1].addEventListener(’transitionend’, function () { flag = true; }) } } btn[0].onclick = function () { if (flag) { flag = false; num–; for (var i = 0; i < _ul.length; i++) { _ul[i].style.transform = ‘rotateX(’ + num * 90 + ‘deg)’; } _ul[_ul.length - 1].addEventListener(’transitionend’, function () { flag = true; }) } }</script>效果图:10.5 backface-visibilitybackface-visibility属性定义当元素不面向屏幕时是否可见。如果在旋转元素不希望看到其背面时,该属性很有用。有两个属性:1、visible背面是可见的2、hidden背面是不可见的 ...

November 22, 2018 · 11 min · jiezi

如何用CSS3画出懂你的3D魔方?

作者:首席填坑官∙苏南公众号:honeyBadger8,群:912594095,本文原创,著作权归作者所有,转载请注明原链接及出处。前言 最近在写《每周动画点点系列》文章,上一期分享了< 手把手教你如何绘制一辆会跑车 >,本期给大家带来是结合CSS3画出来的一个立体3d魔方,结合了js让你随心所欲想怎么转,就怎么转,这里是 @IT·平头哥联盟,我是首席填坑官∙苏南(South·Su),我们先来看看效果,然后再分解它的实现过程吧绘制过程: 好吧,gif图看着好像有点不是很清晰,想在线预览的同学,可点击在线预览 ????,废话不多扯了,先来分析一下,看如何实现这个功能吧。∙ API预热 :本次示例是一个立体的正方形,既然有立体效果,肯定少不了CSS3中的 -webkit-perspective-透视、preserve-3d-三维空间,这个两个是重点哦,当然还有transform-origin、transition、transform等,先来回故一下 API 怎么是讲的吧:perspective 取值 :none :不指定透视 ;length :指定观察者与「z=0」平面的距离,使具有三维位置变换的元素产生透视效果。「z>0」的三维元素比正常大,而「z<0」时则比正常小,大小程度由该属性的值决定,不允许负值。transform-style 取值 :flat :指定子元素位于此元素所在平面内;preserve-3d :指定子元素定位在三维空间内,当该属性值为 preserve-3d时,元素将会创建局部堆叠上下文;小结 :决定一个变换元素看起来是处在三维空间还是平面内,需要该元素的父元素上定义 <’ transform-style ‘> 属性,也就是说想某元素有三维效果,需要设定它的父级有 preserve-3d 。transform-origin 取值 :percentage:用百分比指定坐标值。可以为负值;length:用长度值指定坐标值。可以为负值;left:指定原点的横坐标为left;center①:指定原点的横坐标为center;right:指定原点的横坐标为right;top:指定原点的纵坐标为top;center②:指定原点的纵坐标为center;bottom:指定原点的纵坐标为bottom;transform、transition等,就不介绍了/* perspective 使用示例:*/div{ -webkit-perspective:600px; perspective:600px;}/transform-style 使用示例:/.preserve{ transform-style:preserve-3d; -webkit-transform-style:preserve-3d;} /transform-origin 使用示例:/.preserve{ -webkit-transform-origin:50% 50% -100px; or -webkit-transform-origin:bottom; or -webkit-transform-origin:top; …………} ∙ 绘制6个面 :是的,我没有说错,就是6个面:上、正面、下、背面、左、右,上面API讲了这么多,来实践试一下吧,写6个div,结构大概是这样的,也是接下来的魔方需要的结构:<div class=“cube”> <div class=“cube-inner running”> <p class=“single-side s1”><span>最</span></p> <p class=“single-side s2”><span>懂</span></p> <p class=“single-side s3”><span>你</span></p> <p class=“single-side s4”><span>的</span></p> <p class=“single-side s5”><span>魔</span></p> <p class=“single-side s6”><span>方</span></p> </div></div>!!!发生了什么??是不是很吃惊??说好的值越大,透视效果越强的呢?后面明明藏了个妹子,怎么看没有透视出来?开始我也是跟你一样吃惊的,但瞬间就悟透了,少了rotate,加个它再来看看效果吧:.cube{ width:200px; height:200px; margin:10px auto; padding:260px; position:relative; -webkit-perspective:600px; perspective:600px; transition: .5s ;}.cube-inner{ width:200px; height:200px; position:relative; -webkit-transform-style:preserve-3d; transition:.3s; -webkit-transform-origin:50% 50% -100px; transform: rotateX(45deg);}.cube:hover{ /鼠标经过时,把 perspective 过渡到100 / -webkit-perspective:100px; perspective:100px;}既然API有效,那么拉下来我们就画出6个面吧,按:上、正面、下、背面、左、右,这个顺序来设置吧;首先,我们要指定它们是在三维空间内的preserve-3d,也就是6个面的父级要设置 transform-style 样式;以上都设置好后,再来看看6个面吧,为了便于区分,给它们每个都设置了不同颜色(用了css3的渐变 radial-gradient)——不想手写的同学推荐一个网站可在线设置你要的效果,复制样式即可,先来一睹风采,为了便于观察,整体角度旋转了10deg:说到渐变,偶然之间发现了一个有意思的东西hue-rotate,它能在你初始的颜色基础上旋转元素的色调及其内容,从而达到不同的效果。了解更多hue-rotate : The hue-rotate() CSS function rotates the hue of an element and its contents. Its result is a <filter-function>. 上 - “最”:.cube-inner .single-side.s1{ /s1顶部/ left:0;top:-200px; background: radial-gradient(circle, rgba(255,255,255,.88), #00adff); background: -webkit-radial-gradient(circle, rgba(255,255,255,.88), #00adff); transform-origin:bottom; -webkit-transform-origin:bottom; transform:rotateX(90deg); -webkit-transform:rotateX(90deg);} 正面 - “懂”:下面就是默认的,什么都不用设置,所以就不展示了 ; 下面 - “你”:即底部,底部的设置,正好跟顶部它是相反的,一个origin 以 bottom为基准为坐标,一个以top为基准为坐标;.cube-inner .single-side.s3{ /s3底部/ left:0;top:200px; background: radial-gradient(circle, rgba(255,255,255,.88), #100067); background: -webkit-radial-gradient(circle, rgba(255,255,255,.88), #100067); transform-origin:top; -webkit-transform-origin:top; transform:rotateX(-90deg); -webkit-transform:rotateX(-90deg);} 背面 - “的”:即正面的后边,整体旋转了 135deg,让背面更直观能看到;translateZ 、rotateX 同时移动,形成透视的关系,让它看起来,在正面面的后面;下图二,把默认的正面,设置了透明度,可以看出,背面的透视效果;.cube-inner .single-side.s4{ /s4背部/ z-index:2; left:0;top:0; background: radial-gradient(circle, rgba(255,255,255,.88), #F0C); background: -webkit-radial-gradient(circle, rgba(255,255,255,.88), #F0C); transform:translateZ(-200px) rotateX(180deg) ; -webkit-transform:translateZ(-200px) rotateX(180deg) ; /rotateZ(-180deg) 左右旋转的时候,Z轴旋转180°,因为字是倒着的/} 左侧面 - “魔”:origin以right为基准,left负元素的宽度,rotateY轴旋转90deg;.cube-inner .single-side.s5{ /s5左侧/ left:-200px;top:0; background: radial-gradient(circle, rgba(255,255,255,.88),rgba(33,33,33,1)); background: -webkit-radial-gradient(circle, rgba(255,255,255,.88),rgba(33,33,33,1)); transform-origin:right; -webkit-transform-origin:right; transform:rotateY(-90deg) -webkit-transform:rotateY(-90deg)} 右侧面 - “方”:同理右侧,与左侧正好相反;.cube-inner .single-side.s6{ /s6右侧/ right:-200px;top:0; transform-origin:left; -webkit-transform-origin:left; background: radial-gradient(circle, rgba(255,255,255,.88), #f00); background: -webkit-radial-gradient(circle, rgba(255,255,255,.88), #f00); transform:rotateY(90deg); -webkit-transform:rotateY(90deg);}小结 : 嗯,以上魔方的6个面的绘制过程,基本已经完成,主要在在于transform-origin、rotate、translate等属性的应用,但为了让它更炫酷一些,我们还要给边角加一些光感。∙ 添加高光 :细心的宝宝,前面的布局应该已经发现了,每一行布局的p标签里,都多套了一层span,就是为高光光感,埋下的伏笔,一个平面正方形有四个边,after、before只有两,那么肯定要再套一层,当然方法很多,比如直接用border也是可以的,但比较麻烦,我就选择了现在要讲的这种:after、before设置1px的边框,设置一个线性渐变,中间是白色,两断是过渡到透明的,这样高光就有了,来看一组图吧:∙ CSS 360°旋转 :上面是一个鼠标经过的过渡动画,可以看出立体效果是已经有了,接下来就写一个CSS animation的动画,让它360度旋转,每个角都能看到,这样会显的很666;animation 配合 keyframes 使用,请看代码示例:.cube .cube-inner{ /-webkit-transform:rotateX(180deg) rotateY(0deg) ;/ animation: elfCube 10s infinite ease-in-out; -webkit-animation: elfCube 10s infinite ease alternate;}@keyframes elfCube { 0% { transform: rotateX(0deg) rotateY(0deg); } 50% { transform: rotateX(360deg) rotateY(360deg); } 100% { transform: rotateX(0deg) rotateY(0deg); }}@-webkit-keyframes elfCube { 0% { -webkit-transform: rotateX(0deg) rotateY(0deg); } 50% { -webkit-transform: rotateX(360deg) rotateY(360deg); } 100% { transform: rotateX(0deg) rotateY(0deg); }}∙ 跟随鼠标旋转 :说好的随着鼠标旋转呢??别慌,接下来就是带你装逼,带你飞的时候,首先我们要了解,鼠标在容器内所在的位置,X = e.pageX - ele.offsetLeft, Y = e.pageY - ele.offsetTop;同时要知道元素内的中心点:centerX = width/2,centerY =height/2;然后得出值:axisX = X - centerX,axisY = Y - centerY;PS : 开始尝试想的是鼠标从哪个方向进入,得到它的角度,但发现旋转效果不明显 ,有兴趣的同学可以尝试一下:(((Math.atan2(Y, X) * (180 / Math.PI)) + 180) / 90),参考司徒大神的JS判断鼠标从什么方向进入一个容器;最后,给容器绑上事件:mouseover、mousemove、mouseout,鼠标进入时,暂停css的动画,不然会相互打架哦! ……getAxisX(e){ let left = this.cubeEle.offsetLeft; return e.pageX - left - (this.cubeW/2) * (this.cubeW>this.cubeH ? this.cubeH/this.cubeW : 1);}getAxisY(e){ let top = this.cubeEle.offsetTop; return e.pageY - top - (this.cubeH/2) * (this.cubeH>this.cubeW ? this.cubeW/this.cubeH : 1);} ………… …………run(){ this.cubeEle.addEventListener(‘mouseover’,(e)=>this.hoverOut(e),false); this.cubeEle.addEventListener(‘mousemove’,(e)=>this.move(e),false); this.cubeEle.addEventListener(‘mouseout’,(e)=>this.hoverOut(e),false);}hoverOut(e){ //进入/离开 e.preventDefault(); this.axisX = this.getAxisX(e), this.axisY = this.getAxisY(e); if(e.type == ‘mouseout’){ //离开 this.axisX=0; this.axisY = 0; console.log(“离开”) this.cubeInner.className=“cube-inner running”; }else{ this.cubeInner.className=“cube-inner”; console.log(“进入”) }; let rotate = rotateX(${-this.axisY}deg) rotateY(${-this.axisX}deg); this.cubeInner.style.WebkitTransform = this.cubeInner.style.transform = rotate;} ……结尾:-webkit-perspective,-webkit-transform-style,-webkit-transform-origin,radial-gradient、linear-gradient,transform:rotate、translate、scale,transition,animation;以上就是今天为大家带来的分享,以及使用到的知识点API,如文章中有不对之处,烦请各位大神斧正,想学习更多前端知识,记得关注我的公众号哦文章源码获取-> blog-resource ????想直接在线预览 ????作者:苏南 - 首席填坑官交流群:912594095,公众号:honeyBadger8本文原创,著作权归作者所有。商业转载请联系@IT·平头哥联盟获得授权,非商业转载请注明原链接及出处。 ...

October 29, 2018 · 2 min · jiezi

浮动的label

在web项目中,有一个很重的模块就是登陆/注册模块,这个模块的主体部分就是一个form表单,这个form表单包含两个重要input组(用户名/密码),每个input组都包含label和input,而关于 label+input 的布局方案多种多样,不同的设计师有不同的设计风格,不同的前端工程师又有不同的实现方式。通过对比发现,现在的方案是既注重美观,又注重性能。那么,关于label和input都有哪些布局方案呢?label+input的布局方案将label和input(palcholder关键字提示)分为上下两部分; // 很久以前采用,现在偶尔使用将label和input(palcholder关键字提示)分为左右两部分(label占据一定的宽度,而label中的字体采用左对齐,右对齐,两端对齐这三种常见的方案); // 案例:微博登陆,jd wap登陆页面等label和input(palcholder关键字提示)还是分为左右两部分,不同的是label中的字体使用图标代替; // 案例:segment fault社区登陆页面等只包含input(palcholder关键字提示); // 案例:手淘的登陆页面,掘金开发社区的登陆页面等只显示input(palcholder关键字提示),而label采用浮动并隐藏,当触发input的焦点事件时label显示。 // 案例:手淘的之前登陆页面,或者参考JVFloatLabeledTextField等这几种方案各有优劣,使用label字体用图标代替更形象,但是增加了图标的url访问;label的中的字体个数不一致,看起来不美观,字数相同,这种方案只能说中规中矩;而直接丢弃label,可以使界面简洁美观,但是label有label的作用(下面会详细说label和placeholder的作用);使用浮动的label,增加了动画效果,但需要引入js,这种方案性能,比起不使用js的明显要高(有一种不用js,但兼容性不是太好的方案)。label vs placholderlabel: 描述表单元素的角色,用来指定input的是唯一字段名称placeholder: 它提示用户输入内容的格式它们两个看似类似,但是它们的职责不同,很多同学在这里犯了比较大的错误。如果需要知道它们更多的内容可参考MDN带动画的label(no-js)在做用户交互的页面时,带上动画的用户交互,往往更容易打动用户。下面就介绍一个用伪类实现的浮动label。HTML代码:<div class=“input-group”> <input type=“text” id=“userName” placeholder=“用户名/邮箱/卡号”> <label for=“userName”>账号</label></div>基本布局css代码:.input-group { position: relative; margin: 100px 20px; font-size: 16px;}.input-group>input { display: block; box-sizing: border-box; width: 100%; padding: 16px; font-size: 16px; line-height: 1.0; border: none; border-bottom: 1px solid #cdcdcd; border-radius: 0.4em; transition: box-shadow 0.3s;}.input-group input::placeholder { color: #cdcdcd;}.input-group>input:focus { outline: none; box-shadow: 0.2em 0.8em 1.6em #cdcdcd;}.input-group>label { position: absolute; bottom: 50%; left: 0; z-index: -1; visibility: hidden; color: #050505; opacity: 0;}首先,设置了 label 的位置(posiion: absolute),并定义了它的层级(z-index: -1), 显隐性为(visibility: hidden),透明度(opacity: 0);然后,设置了input的 placeholder样式,可使用伪元素 ::placeholder 设置其样式;最后,设置了一个过渡动画效果,当input元素标签获得焦点时,使用伪类 :focus 定义了input元素标签获得焦点时的阴影样式(box-shadow)和轮廓样式(outline)。label浮动效果样式 .input-group>label { … -webkit-transform-origin: 0 0; transform-origin: 0 0; -webkit-transform: translate3d(0, 0, 0) scale(0); transform: translate3d(0, 0, 0) scale(0); -webkit-transition: opacity 0.3s, visibility 0.3s, transform 0.3s, z-index 0.3s; transition: opacity 0.3s, visibility 0.3s, transform 0.3s, z-index 0.3s; }.input-group>input:focus ~ label { z-index: 1; visibility: visible; opacity: 1; -webkit-transform: translate3d(0, -36px, 0) scale(1); transform: translate3d(0, -36px, 0) scale(1);}在定义 label 样式的集合内,添加它的初始 transform 形变效果,同时设置 transition 过渡效果样式 ,然后定义当 input 获得焦点时,它的兄弟元素 label 的样式即可。这种label浮动的效果和JVFloatLabeledTextField的效果不同,前者是获取到焦点,label立马开始浮动,而后者是当用户输入内容时(也就是placeholder消失时),label开始浮动。要使两者的效果相同,我们可以使用伪类可以嵌套的特性,修改 .input-group>input:focus ~ label 为 .input-group>input:focus:not(:placeholder-shown) ~ label ,这里的 :placeholder-shown 可以定义 placeholder 的显隐效果,但它的兼容性不太好,ie/edge 压根不支持,chrome和safari,以及Firefox还可以,具体可参考can i use。更多伪类和伪元素知识点总结,可参考pseudo案例展示demo其他更多关于html layout文章css3 动画 ...

October 14, 2018 · 1 min · jiezi