UNPKG

3.15 kBJavaScriptView Raw
1// 页面切换效果
2
3// 获取URL参数
4function getQueryString(newUrlParam, name) {
5 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i")
6 var r = newUrlParam.match(reg)
7 if (r != null) return unescape(r[2])
8 return null;
9}
10
11// 无特效翻页
12function dispalyEffect (oldDom, newDom) {
13 if (oldDom) {
14 // 隐藏掉旧的节点
15 oldDom.style.display = 'none'
16 }
17 // 查找页面跳转后的page
18 newDom.style.display = 'block'
19}
20
21// 切换页面动画
22function animation (oldDom, newDom, animationIn, animationOut) {
23 oldDom.addEventListener("animationend", oldDomFun)
24 newDom.addEventListener("animationend", newDomFun)
25
26 oldDom.style.position = 'absolute'
27
28 newDom.style.display = 'block'
29 newDom.style.position = 'absolute'
30 // document.body.style.overflow = 'hidden'
31 animationIn.split(',').forEach(value => {
32 console.log('add:' + value)
33 oldDom.classList.add('ox-page-' + value)
34 })
35 animationOut.split(',').forEach(value => {
36 console.log('add:' + value)
37 newDom.classList.add('ox-page-' + value)
38 })
39 // 旧DOM执行函数
40 function oldDomFun () {
41 // 隐藏掉旧的节点
42 oldDom.style.display = 'none'
43 // console.log(oldDom)
44 oldDom.style.position = ''
45 // 清除临时设置的class
46 animationIn.split(',').forEach(value => {
47 console.log('del:' + value)
48 oldDom.classList.remove('ox-page-' + value)
49 })
50 // 移除监听
51 oldDom.removeEventListener('animationend', oldDomFun, false)
52 }
53
54 // 新DOM执行函数
55 function newDomFun () {
56 // 清除临时设置的style
57 newDom.style.position = ''
58 animationOut.split(',').forEach(value => {
59 console.log('del:' + value)
60 newDom.classList.remove('ox-page-' + value)
61 })
62 // 移除监听
63 newDom.removeEventListener('animationend', newDomFun, false)
64 }
65}
66
67
68// 切换页面前的准备工作
69function switchPage (oldUrlParam, newUrlParam) {
70 var oldPage = oldUrlParam
71 var newPage = newUrlParam
72 let newPagParamList = newPage.split('&')
73 if (newPage) newPage = newPagParamList[0]
74
75 // 查找页面跳转前的page页(dom节点)
76 // console.log(oldUrlParam)
77 // 如果源地址获取不到 那么一般是因为源页面为首页
78 if (oldPage === undefined) {
79 oldPage = globalConfig.entry
80 } else {
81 oldPage = oldPage.split('&')[0]
82 }
83 var oldDom = document.getElementById('ox-' + oldPage)
84 var newDom = document.getElementById('ox-' + newPage)
85
86 if (!newDom) {
87 console.error('页面不存在!')
88 return
89 }
90 // 判断是否有动画效果
91 if (newPagParamList.length > 1) {
92 var animationIn = getQueryString(newUrlParam, 'in')
93 var animationOut = getQueryString(newUrlParam, 'out')
94
95 // 如果没用动画参数则使用默认效果
96 if (!animationIn || !animationOut) {
97 dispalyEffect(oldDom, newDom)
98 return
99 }
100 animation(oldDom, newDom, animationIn, animationOut)
101
102
103 } else {
104 dispalyEffect(oldDom, newDom)
105 }
106
107 window.ozzx.activePage = newPage
108 runPageFunction(newPage, newDom)
109}
\No newline at end of file