UNPKG

2.54 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
21function switchPage (oldUrlParam, newUrlParam) {
22 var oldPage = oldUrlParam
23 var newPage = newUrlParam
24 let newPagParamList = newPage.split('&')
25 if (newPage) newPage = newPagParamList[0]
26 // 查找页面跳转前的page页(dom节点)
27 // console.log(oldUrlParam)
28 // 如果源地址获取不到 那么一般是因为源页面为首页
29 if (oldPage === undefined) {
30 oldPage = globalConfig.entry
31 } else {
32 oldPage = oldPage.split('&')[0]
33 }
34 var oldDom = document.getElementById('ox-' + oldPage)
35 var newDom = document.getElementById('ox-' + newPage)
36 if (!newDom) {
37 console.error('页面不存在!')
38 return
39 }
40 // 判断是否有动画效果
41 if (newPagParamList.length > 1) {
42 var animationIn = getQueryString(newUrlParam, 'in')
43 var animationOut = getQueryString(newUrlParam, 'out')
44 // 如果没用动画参数则使用默认效果
45 if (!animationIn || !animationOut) {
46 dispalyEffect(oldDom, newDom)
47 return
48 }
49 oldDom.style.position = 'absolute'
50
51 newDom.style.display = 'block'
52 newDom.style.position = 'absolute'
53 // document.body.style.overflow = 'hidden'
54 animationIn.split(',').forEach(value => {
55 oldDom.classList.add('ox-page-' + value)
56 })
57 animationOut.split(',').forEach(value => {
58 newDom.classList.add('ox-page-' + value)
59 })
60 oldDom.addEventListener("animationend", function() {
61 // 隐藏掉旧的节点
62 oldDom.style.display = 'none'
63 oldDom.style.position = ''
64 // 清除临时设置的class
65 animationIn.split(',').forEach(value => {
66 oldDom.classList.remove('ox-page-' + value)
67 })
68 })
69 newDom.addEventListener("animationend", function() {
70 // 清除临时设置的style
71 newDom.style.position = ''
72 animationOut.split(',').forEach(value => {
73 newDom.classList.remove('ox-page-' + value)
74 })
75 })
76 } else {
77 dispalyEffect(oldDom, newDom)
78 }
79
80 window.ozzx.activePage = newPage
81 runPageFunction(newPage, newDom)
82}
\No newline at end of file