所有的4个页面切换效果都使用相同的js文件(用于点击事件和页面关闭事件)和CSS文件。每个demo的class名称略有区别。所有的demo都在 Chrome、Safari、Firefox、Opera、IE11 和 IE10浏览器上做了测试(还有iOS也做了测试)。下面文章中的代码没有添加浏览器厂商的前缀,实际代码中需要添加-webkit-和-moz-前缀, Opera 和 IE10+浏览器不需要添加厂商的前缀。关于浏览器厂商的前缀你可以阅读W3C关于vendor prefixes的资料。
CSS3的keyframes属性可以让我们非常方便的在某个时间内控制某个元素的CSS样式。你可以控制动画的开始时间为0%,结束时间为100%。关键字from和to等效于0%和100%。由于兼容性的原因,请确保动画的起始和结束时间值总是0%和100%。
CSS keyframes的语法格式如下:
1 2 3 4 5 6 7 8 9 10 11 | /* Percentage */@keyframes moveTop { 0% { top: 0px; } 100% { top: 100px; }} /* From -> To */@keyframes moveTop { from { top: 0px; } to { top: 100px; }} |
在声明了上面的语句之后,你可以通过animation属性为任何元素添加moveTop动画规则。关于CSS3 animations可以阅读W3C的CSS3 animations相关文档。
CSS animation语法格式如下:
1 | .animated { animation: moveTop 2s 3; } |
上面的语句将在.animated元素上执行moveTop帧动画,动画在2秒时间内执行3次。
下面将以第一种效果为例讲解这些页面加载过渡动画效果的制作方法。
HTML结构
第一个DEMO的HTML结构十分简单,4个导航按钮放置在<nav>元素中,<section>元素中的内容时要切换的页面内容,默认是隐藏的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!-- Navigation --><nav class="nav clearfix"> <button class="nav-el" id="el-topleft" data-id="ov-topleft"> <span class="icon-heart"></span> </button> ... </nav><!-- Overlays --><section class="overlay" id="ov-topleft"> <div class="wrap"> <h1>Section 1</h1> <p>Lorem ipsum dolor sit amet...</p> </div> <button class="close"><span class="mfg-cancel"></span></button></section><section class="overlay" id="ov-topright"> <div class="wrap"> <h1>Section 2</h1> <p>Lorem ipsum dolor sit amet...</p> </div> <button class="close"><span class="mfg-cancel"></span></button></section> |
代码中为导航按钮使用了data attribute属性用于和它们各自的遮罩页面相关联。这些将在后面使用js来处理。
CSS样式
下面是导航按钮的一些基本样式。这些按钮分别是一个大的矩形,中间放置一个图标。它们被设计为响应式的布局方式。并且为每一个按钮制定.active、.active_reverse和.inactive状态下的样式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | .nav { width: 80%; max-width: 420px; margin: 90px auto 90px; font-size: 33px;}/* Nav elements */.nav-el { position:relative; display: inline-block; float: right; width: 47.5%; padding-bottom: 47.5%; color: white; background-color: #16a085; transition: background-color .3s ease-in; z-index: 10;} .nav-el:hover, .nav-el.active { background-color: #107360;} .nav-el.active_reverse { background-color: transparent;} /* Active button always on top */.nav-el.active, .nav-el.active_reverse { z-index: 11;} /* Prevent click/hover on inactive buttons */.nav-el.inactive { pointer-events: none; cursor: default;} /* Specific floating and margin */.nav-el:nth-of-type(2n+1) { float: left; }.nav-el:nth-of-type(n+3) { margin-top: 5%; } /* Icons are horizontally/vertically centered */[class^="icon-"], [class*=" icon-"] { position: absolute; display: inline-block; top: 50%; left: 50%; line-height: 0; width: 1em; |
所有的遮罩层都是固定定位,默认情况下是隐藏的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* Overlay */.overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; overflow: auto; z-index: 9999; visibility: hidden;} .close { position: absolute; top: 50px; right: 50px; font-size: 36px;} |
还有一个额外的在<body>元素隐藏遮罩层的class noscroll,稍后会用到它:
1 2 3 4 5 | .noscroll { overflow: hidden; height: 100%; width: 100%;} |
激活状态
这个状态时整个页面过渡动画的核心,它在1.6秒时间内将页面中所有的动画元素进行一次动画:
当每一个按钮被点击之后的动画
当另一个按钮被点击之后其它按钮的动画
按钮点击之后的遮罩层效果
当使用CSS animation或transition来移动一个元素的时候,都会使用到translate3d属性。这种方式会大大增加用户体验度,但是对浏览器的性能要求会有所提高。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | @keyframes fx-el_topleft-active { 0% {} 16% { transform: translate3d(-27.5%, -27.5%, 0); } 50% { transform: translate3d(55.1%, 55.1%, 0) scale(1); color: #FFF;} 62% { color: transparent; } 100% { transform: translate3d(55.1%, 55.1%, 0) scale(20); color: transparent; }}@keyframes fx-el_topright-active { 0% {} 16% { transform: translate3d(27.5%, -27.5%, 0); } 50% { transform: translate3d(-55.1%, 55.1%, 0) scale(1); color: #FFF;} 62% { color: transparent; } 100% { transform: translate3d(-55.1%, 55.1%, 0) scale(20); color: transparent; }}@keyframes fx-el_btmleft-active { 0% {} 16% { transform: translate3d(-27.5%, 27.5%, 0); } 50% { transform: translate3d(55.1%, -55.1%, 0) scale(1); color: #FFF;} 62% { color: transparent; } 100% { transform: translate3d(55.1%, -55.1%, 0) scale(20); color: transparent;}}@keyframes fx-el_btmright-active { 0% {} 16% { transform: translate3d(27.5%, 27.5%, 0); } 50% { transform: translate3d(-55.1%, -55.1%, 0) scale(1); color: #FFF;} 62% { color: transparent; } 100% { transform: translate3d(-55.1%, -55.1%, 0) scale(20); color: transparent; }} #el-topleft.active { animation: fx-el_topleft-active 1.6s 1 ease-in-out;}#el-topright.active { animation: fx-el_topright-active 1.6s 1 ease-in-out;}#el-btmleft.active { animation: fx-el_btmleft-active 1.6s 1 ease-in-out;}#el-btmright.active { animation: fx-el_btmright-active 1.6s 1 ease-in-out;} |
下面是没有被点击的按钮的动画。它们以相同的方式移动,最终被隐藏(opacity: 0)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | @keyframes fx-el_topleft-inactive { 0% { transform:translate3d(0%,0%,0); opacity:1;}16% { transform:translate3d(-27.5%,-27.5%,0); opacity:1;}40% { opacity:0;}50% { transform:translate3d(55.1%,55.1%,0);}100% { transform:translate3d(55.1%,55.1%,0); opacity:0;}}@keyframes fx-el_topright-inactive { 0% { transform:translate3d(0%,0%,0); opacity:1;}16% { transform:translate3d(27.5%,-27.5%,0); opacity:1;}40% { opacity:0;}50% { transform:translate3d(-55.1%,55.1%,0);}100% { transform:translate3d(-55.1%,55.1%,0); opacity:0;}}@keyframes fx-el_btmleft-inactive { 0% { transform:translate3d(0%,0%,0); opacity:1;}16% { transform:translate3d(-27.5%,27.5%,0); opacity:1;}40% { opacity:0;}
|