其他代码

CSS3超酷页面切换过渡动画特效

阿里云


这是一款 4 种效果非常炫酷的纯 CSS3 页面切换过渡动画特效。该 CSS3 页面过渡动画使用 CSS keyframes 制作而成,所有的动画效果和延时都是使用 CSS 属性,没有任何的 javascript timeout()代码。所有的 4 个页面切换效果都使用相同的 js 文件(用于点击事件和页面关闭事件)和 CSS 文件。每个 demo 的 class 名称略有区别。所有的 demo 都在 Chrome、Safari、Firefox、Opera、IE11 和 IE10 浏览器上做了测试(还有 iOS 也做了测试)。下面文章中的代码没有添加浏览器厂商的前缀,实际代码中需要添加-webkit-和-moz-前缀, Opera 和 IE10+浏览器不需要添加厂商的前缀。关于浏览器厂商的前缀你可以阅读 W3C 关于 vendor prefixes 的资料。关于基本的 CSS3 Animation ,CSS3 的 keyframes 属性可以让我们非常方便的在某个时间内控制某个元素的 CSS 样式。你可以控制动画的开始时间为 0%,结束时间为 100%。关键字 from 和 to 等效于 0%和 100%。由于兼容性的原因,请确保动画的起始和结束时间值总是 0%和 100%。

keyframes

  1. /* Percentage */
  2. @keyframes moveTop {
  3.     0%      { top: 0px; }
  4.     100%    { top: 100px; }
  5. }
  6.  
  7. /* From -> To */
  8. @keyframes moveTop {
  9.     from    { top: 0px; }
  10.     to      { top: 100px; }
  11. }
也想出现在这里?联系我们
创客主机

在声明了上面的语句之后,你可以通过 animation 属性为任何元素添加 moveTop 动画规则。关于 CSS3 animations 可以阅读 W3C 的 CSS3 animations 相关文档。

  1. <h2>animation</h2>
  2. .animated { animation: moveTop 2s 3; }

上面的语句将在.animated 元素上执行 moveTop 帧动画,动画在 2 秒时间内执行 3 次。

HTML 结构

第一个 DEMO 的 HTML 结构十分简单,4 个导航按钮放置在<nav>元素中,<section>元素中的内容时要切换的页面内容,默认是隐藏的。

  1. <!-- Navigation -->
  2. <nav class="nav clearfix"> 
  3.     <button class="nav-el" id="el-topleft" data-id="ov-topleft">
  4.         <span class="icon-heart"></span>
  5.     </button>
  6.     ... 
  7. </nav>
  8. <!-- Overlays -->
  9. <section class="overlay" id="ov-topleft">
  10.     <div class="wrap">
  11.         <h1>Section 1</h1>
  12.         <p>Lorem ipsum dolor sit amet...</p>
  13.     </div>
  14.     <button class="close"><span class="mfg-cancel"></span></button>
  15. </section>
  16. <section class="overlay" id="ov-topright">
  17.     <div class="wrap">
  18.         <h1>Section 2</h1>
  19.         <p>Lorem ipsum dolor sit amet...</p>
  20.     </div>
  21.     <button class="close"><span class="mfg-cancel"></span></button>
  22. </section>

代码中为导航按钮使用了 data attribute 属性用于和它们各自的遮罩页面相关联。这些将在后面使用 js 来处理。

CSS 样式

下面是导航按钮的一些基本样式。这些按钮分别是一个大的矩形,中间放置一个图标。它们被设计为响应式的布局方式。并且为每一个按钮制定.active、.active_reverse 和.inactive 状态下的样式。

  1. .nav {
  2.     width: 80%;
  3.     max-width: 420px;
  4.     margin: 90px auto 90px;
  5.     font-size: 33px;
  6. }
  7. /* Nav elements */
  8. .nav-el {
  9.     position:relative;
  10.     display: inline-block;
  11.     float: right;
  12.     width: 47.5%;
  13.     padding-bottom: 47.5%;
  14.     color: white;
  15.     background-color: #16a085;
  16.     transition: background-color .3s ease-in;
  17.     z-index: 10;
  18. }
  19.  
  20. .nav-el:hover, .nav-el.active {
  21.     background-color: #107360;
  22. }
  23.  
  24. .nav-el.active_reverse {
  25.     background-color: transparent;
  26. }
  27.  
  28. /* Active button always on top */
  29. .nav-el.active, .nav-el.active_reverse {
  30.     z-index: 11;
  31. }
  32.  
  33. /* Prevent click/hover on inactive buttons */
  34. .nav-el.inactive {
  35.     pointer-events: none;
  36.     cursor: default;
  37. }
  38.  
  39. /* Specific floating and margin */
  40. .nav-el:nth-of-type(2n+1) { float: left; }
  41. .nav-el:nth-of-type(n+3) { margin-top: 5%; }
  42.  
  43. /* Icons are horizontally/vertically centered */
  44. [class^="icon-"], [class*=" icon-"] {
  45.     position: absolute;
  46.     display: inline-block;
  47.     top: 50%;
  48.     left: 50%;
  49.     line-height: 0;
  50.     width: 1em;

所有的遮罩层都是固定定位,默认情况下是隐藏的。

  1. /* Overlay */
  2. .overlay {
  3.     position: fixed;
  4.     top: 0;
  5.     left: 0;
  6.     width: 100%;
  7.     height: 100%;
  8.     overflow: auto;
  9.     z-index: 9999;
  10.     visibility: hidden;
  11. }
  12.  
  13. .close {
  14.     position: absolute;
  15.     top: 50px;
  16.     right: 50px;
  17.     font-size: 36px;
  18. }

还有一个额外的在元素隐藏遮罩层的 class noscroll,稍后会用到它:

  1. .noscroll {
  2.     overflow: hidden;
  3.     height: 100%;
  4.     width: 100%;
  5. }

CSS3 Animations 动画

激活状态

这个状态时整个页面过渡动画的核心,它在 1.6 秒时间内将页面中所有的动画元素进行一次动画:

当每一个按钮被点击之后的动画

当另一个按钮被点击之后其它按钮的动画

按钮点击之后的遮罩层效果
当使用 CSS animation 或 transition 来移动一个元素的时候,都会使用到 translate3d 属性。这种方式会大大增加用户体验度,但是对浏览器的性能要求会有所提高。

  1. @keyframes fx-el_topleft-active {
  2.     0%   {}
  3.     16%  { transform: translate3d(-27.5%, -27.5%, 0); }
  4.     50%  { transform: translate3d(55.1%, 55.1%, 0) scale(1); color: #FFF;}
  5.     62%  { color: transparent;  }
  6.     100% { transform: translate3d(55.1%, 55.1%, 0) scale(20); color: transparent; }
  7. }
  8. @keyframes fx-el_topright-active {
  9.     0%   {}
  10.     16%  { transform: translate3d(27.5%, -27.5%, 0); }
  11.     50%  { transform: translate3d(-55.1%, 55.1%, 0) scale(1); color: #FFF;}
  12.     62%  { color: transparent;  }
  13.     100% { transform: translate3d(-55.1%, 55.1%, 0) scale(20); color: transparent; }
  14. }
  15. @keyframes fx-el_btmleft-active {
  16.     0%   {}
  17.     16%  { transform: translate3d(-27.5%, 27.5%, 0); }
  18.     50%  { transform: translate3d(55.1%, -55.1%, 0) scale(1); color: #FFF;}
  19.     62%  { color: transparent;  }
  20.     100% { transform: translate3d(55.1%, -55.1%, 0) scale(20); color: transparent;}
  21. }
  22. @keyframes fx-el_btmright-active {
  23.     0%   {}
  24.     16%  { transform: translate3d(27.5%, 27.5%, 0); }
  25.     50%  { transform: translate3d(-55.1%, -55.1%, 0) scale(1); color: #FFF;}
  26.     62%  { color: transparent;  }
  27.     100% { transform: translate3d(-55.1%, -55.1%, 0) scale(20); color: transparent; }
  28. }
  29.  
  30. #el-topleft.active {
  31.     animation: fx-el_topleft-active 1.6s 1 ease-in-out;
  32. }
  33. #el-topright.active {
  34.     animation: fx-el_topright-active 1.6s 1 ease-in-out;
  35. }
  36. #el-btmleft.active {
  37.     animation: fx-el_btmleft-active 1.6s 1 ease-in-out;
  38. }
  39. #el-btmright.active {
  40.     animation: fx-el_btmright-active 1.6s 1 ease-in-out;
  41. }

下面是没有被点击的按钮的动画。它们以相同的方式移动,最终被隐藏(opacity: 0)。

  1. @keyframes fx-el_topleft-inactive {
  2. 0% { transform: translate3d(0%, 0%, 0); opacity: 1; }
  3. 16% { transform: translate3d(-27.5%, -27.5%, 0);opacity: 1; }
  4. 40% { opacity: 0;}
  5. 50% { transform: translate3d(55.1%, 55.1%, 0); }
  6. 100% { transform: translate3d(55.1%, 55.1%, 0); opacity: 0; }
  7. }
  8. @keyframes fx-el_topright-inactive {
  9. 0% { transform: translate3d(0%, 0%, 0); opacity: 1; }
  10. 16% { transform: translate3d(27.5%, -27.5%, 0); opacity: 1; }
  11. 40% { opacity: 0;}
  12. 50% { transform: translate3d(-55.1%, 55.1%, 0); }
  13. 100% { transform: translate3d(-55.1%, 55.1%, 0); opacity: 0; }
  14. }
  15. @keyframes fx-el_btmleft-inactive {
  16. 0% { transform: translate3d(0%, 0%, 0); opacity: 1; }
  17. 16% { transform: translate3d(-27.5%, 27.5%, 0); opacity: 1; }
  18. 40% { opacity: 0;}
  19. 50% { transform: translate3d(55.1%, -55.1%, 0); }
  20. 100% { transform: translate3d(55.1%, -55.1%, 0); opacity: 0; }
  21. }
  22. @keyframes fx-el_btmright-inactive {
  23. 0% { transform: translate3d(0%, 0%, 0); opacity: 1; }
  24. 16% { transform: translate3d(27.5%, 27.5%, 0); opacity: 1; }
  25. 40% { opacity: 0;}
  26. 50% { transform: translate3d(-55.1%, -55.1%, 0); }
  27. 100% { transform: translate3d(-55.1%, -55.1%, 0); opacity: 0; }
  28. }
  29.  
  30. #el-topleft.inactive {
  31. animation: fx-el_topleft-inactive 1.6s 1 ease-in-out;
  32. }
  33. #el-topright.inactive {
  34. animation: fx-el_topright-inactive 1.6s 1 ease-in-out;
  35. }
  36. #el-btmleft.inactive {
  37. animation: fx-el_btmleft-inactive 1.6s 1 ease-in-out;
  38. }
  39. #el-btmright.inactive {
  40. animation: fx-el_btmright-inactive 1.6s 1 ease-in-out;
  41. }

接下来是遮罩层的动画效果,代码非常容易理解。

  1. @keyframes fx-overlay {
  2.     0%   { visibility: visible; color: transparent; }
  3.     50%  { background-color: #107360; color: white; }
  4.     100% { visibility: visible; background-color: #107360; color: #FFF; }
  5. }
  6.  
  7. .overlay.active {
  8.     animation: fx-overlay .8s 1.25s 1 ease-in-out forwards;
  9. }

遮罩层的动画延时时间为 1.25 秒,这将使遮罩层的内容在按钮动画结束之后才显示出来。forwards 属性时指定动画结束的时间值,在这里表示保存遮罩层一直可见。

Active_reverse

当用户点击遮罩层上的关闭按钮时,会使用该状态来反向执行动画:遮罩层消失,按钮回到原来的位置上。

  1. @keyframes fx-el_topleft-active_reverse {
  2.     0%   { transform: translate3d(55.1%, 55.1%, 0) scale(20);
  3.             color: transparent; background-color: #107360; }
  4.     38%  { color: transparent;}
  5.     50%  { transform: translate3d(55.1%, 55.1%, 0) scale(1); color: #FFF; background-color: #107360; }
  6.     82%  { transform: translate3d(-27.5%, -27.5%, 0); background-color: #16a085;}
  7.     100% { transform: translate3d(0%, 0%, 0); }
  8. }
  9. @keyframes fx-el_topright-active_reverse {
  10.     0%   { transform: translate3d(-55.1%, 55.1%, 0) scale(20);
  11.             color: transparent; background-color: #107360; }
  12.     38%  { color: transparent; }
  13.     50%  { transform: translate3d(-55.1%, 55.1%, 0) scale(1); color: #FFF; background-color: #107360;}
  14.     82%  { transform: translate3d(27.5%, -27.5%, 0); background-color: #16a085;}
  15.     100% { transform: translate3d(0%, 0%, 0); }
  16. }
  17. @keyframes fx-el_btmleft-active_reverse {
  18.     0%   { transform: translate3d(55.1%, -55.1%, 0) scale(20);
  19.             color: transparent; background-color: #107360; }
  20.     38%  { color: transparent; }
  21.     50%  { transform: translate3d(55.1%, -55.1%, 0) scale(1); color: #FFF; background-color: #107360;}
  22.     82%  { transform: translate3d(-27.5%, 27.5%, 0); background-color: #16a085;}
  23.     100% { transform: translate3d(0%, 0%, 0); }
  24. }
  25. @keyframes fx-el_btmright-active_reverse {
  26.     0%   { transform: translate3d(-55.1%, -55.1%, 0) scale(20);
  27.             color: transparent; background-color: #107360; }
  28.     38%  { color: transparent; }
  29.     50%  { transform: translate3d(-55.1%, -55.1%, 0) scale(1); color: #FFF; background-color: #107360;}
  30.     82%  { transform: translate3d(27.5%, 27.5%, 0); background-color: #16a085;}
  31.     100% { transform: translate3d(0%, 0%, 0); }
  32. }
  33.  
  34. #el-topleft.active_reverse {
  35.     animation: fx-el_topleft-active_reverse 1.6s 1 ease-in-out;
  36. }
  37. #el-topright.active_reverse {
  38.     animation: fx-el_topright-active_reverse 1.6s 1 ease-in-out;
  39. }
  40. #el-btmleft.active_reverse {
  41.     animation: fx-el_btmleft-active_reverse 1.6s 1 ease-in-out;
  42. }
  43. #el-btmright.active_reverse {
  44.     animation: fx-el_btmright-active_reverse 1.6s 1 ease-in-out;
  45. }

未被点击的按钮的反向动画代码如下:

  1. @keyframes fx-el_topleft-inactive_reverse {
  2.     0%   { transform: translate3d(55.1%, 55.1%, 0);  opacity: 0; }
  3.     50%  { transform: translate3d(55.1%, 55.1%, 0); }
  4.     82%  { transform: translate3d(-27.5%, -27.5%, 0); }
  5.     45%  { opacity: 0; }
  6.     100% { transform: translate3d(0%, 0%, 0); opacity: 1; }
  7. }
  8. @keyframes fx-el_topright-inactive_reverse {
  9.     0%   { transform: translate3d(-55.1%, 55.1%, 0);  opacity: 0; }
  10.     50%  { transform: translate3d(-55.1%, 55.1%, 0); }
  11.     82%  { transform: translate3d(27.5%, -27.5%, 0); }
  12.     45%  { opacity: 0; }
  13.     100% { transform: translate3d(0%, 0%, 0);opacity: 1; }
  14. }
  15. @keyframes fx-el_btmleft-inactive_reverse {
  16.     0%   { transform: translate3d(55.1%, -55.1%, 0); opacity: 0; }
  17.     50%  { transform: translate3d(55.1%, -55.1%, 0); }
  18.     82%  { transform: translate3d(-27.5%, 27.5%, 0); }
  19.     45%  { opacity: 0; }
  20.     100% { transform: translate3d(0%, 0%, 0);opacity: 1; }
  21. }
  22. @keyframes fx-el_btmright-inactive_reverse {
  23.     0%   { transform: translate3d(-55.1%, -55.1%, 0); opacity: 0; }
  24.     50%  { transform: translate3d(-55.1%, -55.1%, 0); }
  25.     82%  { transform: translate3d(27.5%, 27.5%, 0); }
  26.     45%  { opacity: 0; }
  27.     100% { transform: translate3d(0%, 0%, 0); opacity: 1; }
  28. }
  29.  
  30. #el-topleft.inactive_reverse {
  31.     animation: fx-el_topleft-inactive_reverse 1.6s 1 ease-in-out;
  32. }
  33. #el-topright.inactive_reverse {
  34.     animation: fx-el_topright-inactive_reverse 1.6s 1 ease-in-out;
  35. }
  36. #el-btmleft.inactive_reverse {
  37.     animation: fx-el_btmleft-inactive_reverse 1.6s 1 ease-in-out;
  38. }
  39. #el-btmright.inactive_reverse {
  40.     animation: fx-el_btmright-inactive_reverse 1.6s 1 ease-in-out;
  41. }

遮罩层的反向动画代码如下:

  1. @keyframes fx-overlay-reverse {
  2.     0% { visibility: visible; background-color: #107360; color: #FFF;}
  3.     40% { background-color: #107360; color: transparent;}
  4.     85% {background-color: transparent; }
  5.     100% {visibility: invisible; color: transparent; background-color: transparent; }
  6. }
  7.  
  8. .overlay.active_reverse {
  9.     animation: fx-overlay-reverse .8s 1 ease-in backwards;
  10. }

Backwards 属性的作用和 forwards 属性类似。当它应用到目标元素上时,动画会将这些值应用到第一个关键帧上。

CSS3 超酷页面切换过渡动画特效

已有 623 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!