简单几步,用纯CSS3实现3D翻转效果!

作为前端开发人员的必修课,CSS3能带我们完成许多基本动效,本期我们将用CSS3实现hover翻转效果~第一步非常简单,我们简单画1个演示方块,为其添加transition和transform属性:// 本示例均使用Sass语法.block { width: 200px; height: 200px; background: brown; cursor: pointer; transition: 0.8s; &:hover { transform: rotateY(180deg); }}我们看一看这时候的效果:这里需要注意的是:transition属性要写在.block上而不是hover上,如果只在hover上写transition,则鼠标移出时并没有transition的过渡效果,如果我们只将transition写在hover上:第二步比较关键:我们不难发现始终在1个平面上翻转,不够有立体感,因此我们需要稍加改变思路——用2层div嵌套// html部分<div class=“block”> <div class=“block-in”></div></div>// CSS部分.block { width: 200px; height: 200px; cursor: pointer; &-in { background: brown; height: 100%; transition: 0.8s; } &:hover .block-in { transform: rotateY(180deg); }}此时效果没变,如下:这个时候关键的1步来了:我们需要给外层添加perspective和transform-style属性,为整个动画增添3D变形效果:.block { width: 200px; height: 200px; cursor: pointer; /* 3D变形 */ transform-style: preserve-3d; -webkit-perspective: 1000; -moz-perspective: 1000; -ms-perspective: 1000; perspective: 1000; &-in { background: brown; height: 100%; transition: 0.8s; } &:hover .block-in { transform: rotateY(180deg); }}最终实现效果如下:最终我们总结一下思路:1.建立内外2层div,鼠标 hover 到外层时,内层div添加翻转 transform: rotateY(180deg)2.注意将 transition 属性添加到需要翻转的div上,而不是 hover 时3.外层div添加 perspective 和 transform-style 属性,最终实现3D翻转效果 ...

January 17, 2019 · 1 min · jiezi

[LeetCode] 61. Rotate List

ProblemGiven a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplanation:rotate 1 steps to the right: 5->1->2->3->4->NULLrotate 2 steps to the right: 4->5->1->2->3->NULLExample 2:Input: 0->1->2->NULL, k = 4Output: 2->0->1->NULLExplanation:rotate 1 steps to the right: 2->0->1->NULLrotate 2 steps to the right: 1->2->0->NULLrotate 3 steps to the right: 0->1->2->NULLrotate 4 steps to the right: 2->0->1->NULLSolutionclass Solution { public ListNode rotateRight(ListNode head, int k) { if (head == null || head.next == null || k == 0) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListNode fast = dummy, slow = dummy; int n = 0; while (fast.next != null) { fast = fast.next; n++; } k = n-k%n; while (k > 0) { slow = slow.next; k–; } fast.next = dummy.next; dummy.next = slow.next; slow.next = null; return dummy.next; }} ...

December 30, 2018 · 1 min · jiezi

前端每日实战:123# 视频演示如何用纯 CSS 创作一架双冀飞机

效果预览按下右侧的“点击预览”按钮可以在当前页面预览,点击链接可以全屏预览。https://codepen.io/comehope/pen/yxVYRL可交互视频此视频是可以交互的,你可以随时暂停视频,编辑视频中的代码。请用 chrome, safari, edge 打开观看。https://scrimba.com/p/pEgDAM/cmVLRc9源代码下载每日前端实战系列的全部源代码请从 github 下载:https://github.com/comehope/front-end-daily-challenges代码解读定义 dom,容器中包含 3 个子元素,分别表示机冀、螺旋桨和轮子,机冀有 4 片叶片,轮子左右各一只:<div class=“plane”> <div class=“wings”></div> <div class=“fans”> <span></span> <span></span> <span></span> <span></span> </div> <div class=“wheels”> <span class=“left”></span> <span class=“right”></span> </div></div>定义容器尺寸:.plane { width: 28em; height: 13em; font-size: 10px;}定义子元素整体布局和共有属性:.plane { display: flex; justify-content: center; position: relative;}.plane > * { position: absolute;}.plane > *::before,.plane > *::after { content: ‘’; position: absolute;}定义基本色:.plane { color: black;}画出双冀:.wings { width: inherit; display: flex; justify-content: center;}.wings::before { width: inherit; height: 0.5em; background-color: currentColor;}.wings::after { top: 4em; width: 90%; height: 0.4em; background-color: currentColor;}画出螺旋桨的中心:.fans { width: 11em; height: 11em; outline: 1px dashed; background: radial-gradient( black 2.5em, transparent 2.5em );}定义叶片的形状为半圆形:.fans span { width: inherit; height: inherit;}.fans span::before { width: inherit; height: 50%; background-color: rgba(255, 255, 255, 0.4); border-radius: 50% 50% 0 0 / 100% 100% 0 0;}分别旋转叶片的角度,使 4 个页片均匀分布在一个圆内:.fans span::before { transform-origin: bottom; transform: rotate(calc((var(–n) - 1) * 90deg));}.fans span:nth-child(1) { –n: 1;}.fans span:nth-child(2) { –n: 2;}.fans span:nth-child(3) { –n: 3;}.fans span:nth-child(4) { –n: 4;}画出 2 个轮子:.wheels { width: 16em; height: 2em; outline: 1px dashed; bottom: 0; display: flex; justify-content: space-between;}.wheels span { position: static; width: 1em; height: inherit; background-color: currentColor; border-radius: 0.5em;}画出轮子的 2 个支架:.wheels span { display: flex; justify-content: center;}.wheels span::before { width: 0.2em; height: 8em; background-color: currentColor; transform-origin: bottom; bottom: 1em; z-index: -1;}.wheels .left::before { transform: rotate(45deg);}.wheels .right::before { transform: rotate(-45deg);}接下来制作动画效果。增加螺旋桨旋转的动画效果:.fans span { animation: fans-rotating 0.8s linear infinite; animation-delay: calc(var(–n) * 0.1s);}@keyframes fans-rotating { to { transform: rotate(-1turn); }}增加飞机飞行的动画效果:.plane { animation: fly 5s infinite;}@keyframes fly { 10%, 50%, 100% { top: 0; } 25% { top: 1em; } 75% { top: -1em; }}再增加飞机旋转的动画效果:.plane { animation: plane-rotating 10s infinite, fly 5s infinite;}@keyframes plane-rotating { 10%, 30%, 50% { transform: rotate(0deg); } 20% { transform: rotate(-4deg); } 80% { transform: rotate(8deg); } 100% { transform: rotate(-1turn); }}大功告成! ...

September 1, 2018 · 2 min · jiezi