基于javascript实现漂亮的页面过渡动画效果附源码下载


Posted in Javascript onOctober 26, 2015

用户通过点击页面左侧的菜单,对应的页面加载时伴随着滑动过滤动画,并带有进度条效果。当然页面的加载是Ajax驱动的,整个加载过渡过程非常流畅,非常好的用户体验。

HTML

HTML结构中,.cd-main包含页面主体内容,.cd-side-navigation包含着侧边导航条,#cd-loading-bar则是用来做进度条动画用的。

<nav class="cd-side-navigation"> 
  <ul> 
    <li> 
      <a href="index.html" class="selected" data-menu="index"> 
        <svg><!-- svg content here --></svg> 
        Intro 
      </a> 
    </li> 
    <li> 
      <!-- ... --> 
    </li> 
    <!-- other list items here --> 
  </ul> 
</nav> <!-- .cd-dashboard --> 
<main class="cd-main"> 
  <section class="cd-section index visible"> 
    <header> 
      <div class="cd-title"> 
        <h2>Animated Page Transition #2</h2> 
        <span>Some text here</span> 
      </div> 
      <a href="#index-content" class="cd-scroll">Scroll Down</a> 
    </header> 
    <div class="cd-content" id="index-content"> 
      <!-- content here --> 
    </div> <!-- .cd-content --> 
  </section> <!-- .cd-section --> 
</main> <!-- .cd-main --> 
<div id="cd-loading-bar" data-scale="1" class="index"></div> <!-- lateral loading bar -->

CSS

我们将.cd-side-navigation固定在页面左侧,并且设置它的高度为100%,这样左侧导航菜单就始终占据左侧边栏,右侧主体内容滚动时,左侧导航菜单不动。

.cd-side-navigation { 
 position: fixed; 
 z-index: 3; 
 top: 0; 
 left: 0; 
 height: 100vh; 
 width: 94px; 
 overflow: hidden; 
} 
.cd-side-navigation ul { 
 height: 100%; 
 overflow-y: auto; 
} 
.cd-side-navigation::before { 
 /* background color of the side navigation */ 
 content: ''; 
 position: absolute; 
 top: 0; 
 left: 0; 
 height: 100%; 
 width: calc(100% - 4px); 
 background-color: #131519; 
} 
.cd-side-navigation li { 
 width: calc(100% - 4px); 
} 
.cd-side-navigation a { 
 display: block; 
 position: relative; 
} 
.cd-side-navigation a::after { 
 /* 4px line to the right of the item - visible on hover */ 
 content: ''; 
 position: absolute; 
 top: 0; 
 right: -4px; 
 height: 100%; 
 width: 4px; 
 background-color: #83b0b9; 
 opacity: 0; 
} 
.no-touch .cd-side-navigation a:hover::after { 
 opacity: 1; 
}

JavaScript

当我们点击左侧菜单时,调用triggerAnimation()函数,这个函数会触发加载进度条动画函数loadingBarAnimation(),接着加载页面内容函数:loadNewContent()。

function loadingBarAnimation() { 
  var scaleMax = loadingBar.data('scale'); 
  if( scaleY + 1 < scaleMax) { 
    newScaleValue = scaleY + 1; 
  } 
  // ... 
   
  loadingBar.velocity({ 
    scaleY: newScaleValue 
  }, 100, loadingBarAnimation); 
}

当新页面被选中时,一个新的元素.cd-section将会被创建并且插入到DOM中,然后load()新的url内容。

function loadNewContent(newSection) { 
  var section = $('<section class="cd-section overflow-hidden '+newSection+'"></section>').appendTo(mainContent); 
   
  //load the new content from the proper html file 
  section.load(newSection+'.html .cd-section > *', function(event){ 
     
    loadingBar.velocity({ 
      scaleY: scaleMax //this is the scaleY value to cover the entire window height (100% loaded) 
    }, 400, function(){ 
       
      section.addClass('visible'); 
 
      var url = newSection+'.html'; 
 
      if(url!=window.location){ 
        //add the new page to the window.history 
        window.history.pushState({path: url},'',url); 
      } 
 
      // ... 
    }); 
  }); 
}

通过异步加载的页面要返回上一页历史浏览记录的话,可以点击浏览器上的返回即可。返回上一页同样有过渡动画效果。

Javascript 相关文章推荐
Jquery实现点击按钮,连续地向textarea中添加值的实例代码
Mar 08 Javascript
javascript函数式编程实例分析
Apr 25 Javascript
JavaScript的Ext JS框架中的GridPanel组件使用指南
May 21 Javascript
jQuery通过改变input的type属性实现密码显示隐藏切换功能
Feb 08 Javascript
将 vue 生成的 js 上传到七牛的实例
Jul 28 Javascript
vue打包后显示空白正确处理方法
Nov 01 Javascript
jQuery实现浏览器之间跳转并传递参数功能【支持中文字符】
Mar 28 jQuery
vue结合axios与后端进行ajax交互的方法
Jul 06 Javascript
JavaScript函数式编程(Functional Programming)声明式与命令式实例分析
May 21 Javascript
js脚本中执行java后台代码方法解析
Oct 11 Javascript
如何用vue-cli3脚手架搭建一个基于ts的基础脚手架的方法
Dec 12 Javascript
vue 扩展现有组件的操作
Aug 14 Javascript
JS实现的页面自定义滚动条效果
Oct 26 #Javascript
JS实现的鼠标跟随代码(卡通手型点击效果)
Oct 26 #Javascript
JS响应鼠标点击实现两个滑块区间拖动效果
Oct 26 #Javascript
javaScript实现可缩放的显示区效果代码
Oct 26 #Javascript
JS基于VML技术实现的五角星礼花效果代码
Oct 26 #Javascript
jquery获取url参数及url加参数的方法
Oct 26 #Javascript
JavaScritp添加url参数并将参数加入到url中及更改url参数的方法
Oct 26 #Javascript
You might like
ThinkPHP3.1基础知识快速入门
2014/06/19 PHP
Thinkphp和onethink实现微信支付插件
2016/04/13 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
2017/12/25 PHP
PHP支付宝当面付2.0代码
2018/12/21 PHP
laravel model模型定义实现开启自动管理时间created_at,updated_at
2019/10/17 PHP
JScript中的undefined和&quot;undefined&quot;的区别
2007/03/08 Javascript
Javascript 读书笔记索引贴
2010/01/11 Javascript
JavaScript DOM学习第四章 getElementByTagNames
2010/02/19 Javascript
Jquery动态更改一张位图的src与Attr的使用
2013/07/31 Javascript
JavaScript Sort 的一个错误用法示例
2015/03/20 Javascript
js简单实现Select互换数据的方法
2015/08/17 Javascript
JavaScript获取浏览器信息的方法
2015/11/20 Javascript
基于jquery实现表格无刷新分页
2016/01/07 Javascript
JavaScript中 ES6 generator数据类型详解
2016/08/11 Javascript
在DWR中实现直接获取一个JAVA类的返回值的两种方法
2016/12/25 Javascript
Vue2.x中的父子组件相互通信的实现方法
2017/05/02 Javascript
vue移动端实现红包雨效果
2020/06/23 Javascript
JavaScript模块管理的简单实现方式详解
2019/06/15 Javascript
vue实现信息管理系统
2020/05/30 Javascript
Python批量重命名同一文件夹下文件的方法
2015/05/25 Python
Python网络爬虫项目:内容提取器的定义
2016/10/25 Python
详解关于Django中ORM数据库迁移的配置
2018/10/08 Python
python求最大值,不使用内置函数的实现方法
2019/07/09 Python
Python logging设置和logger解析
2019/08/28 Python
使用Python串口实时显示数据并绘图的例子
2019/12/26 Python
Python新手学习标准库模块命名
2020/05/29 Python
python实现文件+参数发送request的实例代码
2021/01/05 Python
英国运动服、设备及配件网站:DW Sports
2019/12/04 全球购物
英语一分钟演讲稿
2014/04/29 职场文书
就职演讲稿范文
2014/05/19 职场文书
文化产业实施方案
2014/06/07 职场文书
学习保证书
2015/01/17 职场文书
vue点击弹窗自动触发点击事件的解决办法(模拟场景)
2021/05/25 Vue.js
解析Java异步之call future
2021/06/14 Java/Android
i5-10400f处理相当于i7多少水平
2022/04/19 数码科技
Apache自带的ab压力测试工具的实现
2022/07/23 Servers