41-紧贴底部的页脚
背景知识:视口相关的长度单位 vw
, vh
,calc()
介绍:当页面较短时,底部页脚 foolter 区域不能紧贴在底部,而是紧跟在内容的下方。这里提供了2 种方案来解决这个问题。
固定高度的解决方案
<header> <h1>Site name</h1> </header> <main> <input type="checkbox" id="contents" /><label for="contents">Toggle contents</label> <p>Bacon ipsum dolor sit amet turkey veniam shankle, culpa short ribs kevin t-bone occaecat. Et laborum venison nostrud, ut veniam sint kielbasa ullamco pancetta. Qui drumstick tail, bacon leberkas shoulder capicola laborum. Minim ipsum bacon, mollit laboris t-bone pariatur. Ham hock reprehenderit sint beef, sausage pig eiusmod t-bone shankle strip steak.</p> <p>Cow enim excepteur, boudin dolore lorem magna fugiat consequat voluptate. Picanha fugiat chicken, cupim aliquip magna filet mignon prosciutto ut nostrud. Kielbasa rump frankfurter sunt corned beef. Andouille in cillum deserunt, rump et picanha landjaeger tongue anim.</p> <p>Ad meatball ipsum ground round shank. Quis ipsum meatball exercitation. Laborum swine spare ribs, sunt ball tip magna t-bone venison. Velit doner voluptate non occaecat do ribeye kevin strip steak et. Esse biltong shank ribeye dolor pariatur aute deserunt non est eiusmod pork belly pancetta pork chop. Pork chop id consectetur rump, meatball short loin brisket tail andouille deserunt alcatra irure prosciutto do.</p> <p>Dolore reprehenderit ex, meatball doner commodo consectetur ea ribeye. Ad aliqua kevin, chuck excepteur minim et cow esse ham hock landjaeger. Alcatra bresaola dolore tempor do, excepteur in velit flank officia dolore meatloaf corned beef picanha. Eu pancetta brisket eiusmod ipsum aute sausage, culpa rump shoulder excepteur nostrud venison sed pork loin. Tempor proident do magna ground round. Ut venison frankfurter et veniam dolore. Pig pork belly beef ribs kevin, doner exercitation magna esse shankle.</p> <p>Flank anim chuck boudin id consectetur bresaola ham pork loin cupim andouille frankfurter. Proident do ball tip nostrud nulla sed, frankfurter ut commodo corned beef ut. Ex aute in, pig deserunt beef ribs turducken pastrami irure ball tip pork belly pork chop sausage id. Chicken sunt nisi tempor sed. In eiusmod non fatback tempor tenderloin pastrami adipisicing cow lorem ut tail jerky cupidatat venison. Jowl consequat commodo pork loin ipsum pork belly prosciutto aute beef. Ball tip shoulder aliqua, fugiat landjaeger kevin pork chop beef ribs leberkas hamburger cillum turkey ut doner culpa.</p> </main> <footer> <p>© 2015 No rights reserved.</p> <p>Made with ♥ by an anonymous pastafarian.</p> </footer> /** * Sticky footer with fixed height */ main { min-height: calc(100vh - 5em - 7em); } /* Toggle checkbox to alternate between short/long content */ #contents:checked ~ p { display: none } /* Basic styling */ body { margin: 0; font: 100%/1.5 Palatino Linotype, Palatino, serif; } h1 { margin: .5em 0 0; } header { text-align: center; height: 3em; } main, footer { display: block; padding: .5em calc(50% - 400px); } footer { background: linear-gradient(#222, #444); color: white; height: 6em; }
更灵活的解决方案 Flexbox
/** * Sticky footer with flexible height */ body { display: flex; flex-direction: column; min-height: 100vh; } main { flex: 1; } /* Toggle checkbox to alternate between short/long content */ #contents:checked ~ p { display: none } /* Basic styling */ body { margin: 0; font: 100%/1.5 Baskerville, Palatino Linotype, Palatino, serif; } h1 { margin: .5em 0 0; } header { text-align: center; height: 3em; } main, footer { display: block; padding: .5em calc(50% - 400px); } footer { background: linear-gradient(#222, #444); color: white; }
42-缓动效果
背景知识:基本的 css 过渡,基本的 css 动画
介绍:给过渡和动画加上缓动效果可以使界面显得更加的生动和真实,毕竟在现实世界中,物体从A 点到 B 点到移动往往不是完全匀速的。
下面基于 css 动画实现了一个回弹效果
弹跳动画
<div class="ball"></div> /** * Bouncing animation */ @keyframes bounce { 60%, 80%, to { transform: translateY(400px); animation-timing-function: ease; } 70% { transform: translateY(300px); } 90% { transform: translateY(360px); } } .ball { width: 0; height: 0; padding: 1.5em; border-radius: 50%; margin: auto; background: red radial-gradient(at 30% 30%, #fdd, red); animation: bounce 2s cubic-bezier(.1,.25,1,.25) forwards; } body { background: linear-gradient(skyblue, white 450px, yellowgreen 0); min-height: 100vh; }
弹性过渡
下面这个输入框在 focus 的时候会显示出一个 tips , 我们通过过渡和自定义调速函数 cubic-bezier()
来改善下效果,使其更加生动。
<label> Your username: <input value="leaverou"></input> <span class="callout"> Only letters, numbers, underscores (_) and hyphens (-) allowed! </span> </label> /** * Elastic transitions */ input:not(:focus) + .callout:not(:hover) { transform: scale(0); transition: .25s transform; } .callout { transition: .5s cubic-bezier(.25,.1,.3,1.5) transform; transform-origin: 1.4em -.4em; } /* Styling */ body { padding: 1.5em; font: 200%/1.6 Baskerville; } input { display: block; padding: 0 .4em; font: inherit; } .callout { position: absolute; max-width: 14em; padding: .6em .8em; border-radius: .3em; margin: .3em 0 0 -.2em; background: #fed; border: 1px solid rgba(0,0,0,.3); box-shadow: .05em .2em .6em rgba(0,0,0,.2); font-size: 75%; } .callout:before { content: ""; position: absolute; top: -.4em; left: 1em; padding: .35em; background: inherit; border: inherit; border-right: 0; border-bottom: 0; transform: rotate(45deg); }
反面案例-对颜色的弹性过渡
虽然这种效果挺有趣,但是在 UI 的场景下通常是不合适的。
/** * Elastic color */ html { background: rgb(100%, 0%, 40%); transition: 1s cubic-bezier(.25,.1,.2,3); } html:hover { background: gray; }
43-逐帧动画
背景知识:基本的 css 动画,“缓动效果”
介绍: 使用 css 实现一个 loading 动画, 主要原理是对一张 png 图片进行 background-position
偏移动画。
<div class="loader">Loading…</div> /** * Frame-by-frame animations */ @keyframes loader { to { background-position: -800px 0; } } .loader { width: 100px; height: 100px; text-indent: 999px; overflow: hidden; /* Hide text */ background: url(http://dabblet.com/img/loader.png) 0 0; animation: loader 1s infinite steps(8); }