omi-mp - record the location of scroll bars

This commit is contained in:
dntzhang 2018-11-12 11:24:22 +08:00
parent 38d28480b7
commit 82dd8cac52
5 changed files with 29 additions and 19 deletions

View File

@ -189,25 +189,25 @@ gulp.task('route', ['copy'], (cb) => {
let json = require('./src/mp/app.json')
//fs.unlinkSync('./src/index.js')
fs.writeFile('./src/index.js', `import { render } from 'omi'
import 'omi-router'
import './utils/mp'
fs.writeFile('./src/index.js', `import 'omi-router'
import { routeUpdate } from './utils/mp'
import './mp/app'
const root = document.querySelector('#root')
route('*', evt => {
title('index')
empty('#root')
render(<we-index />, '#root')
routeUpdate(<we-index />, 'we-index', evt.byNative, root)
})
${route(json.pages)}
function empty(selector) {
const node = document.querySelector(selector)
while (node.firstChild) {
node.removeChild(node.firstChild)
}
document.documentElement.scrollTop = 0
document.body.scrollTop = 0
window.onscroll = function(){
root.childNodes.forEach(child => {
if(child.style.display !== 'none'){
child._preScrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
}
})
}
function title(value) {
@ -223,8 +223,7 @@ function route(arr) {
const name = item.split('/')[1]
result.push(`route('${item.replace('pages', '..')}', evt => {
title('${name}')
empty('#root')
render(<we-${name} />, '#root')
routeUpdate(<we-${name} />, 'we-${name}', evt.byNative, root)
})
`)
})

View File

@ -49,7 +49,7 @@
"jest": "20.0.4",
"object-assign": "4.1.1",
"omi": "latest",
"omi-router": "^2.0.5",
"omi-router": "latest",
"postcss-flexbugs-fixes": "3.2.0",
"postcss-loader": "2.0.8",
"prettier": "^1.14.3",

View File

@ -19,6 +19,16 @@ route('../logs/logs', evt => {
routeUpdate(<we-logs />, 'we-logs', evt.byNative, root)
})
window.onscroll = function(){
root.childNodes.forEach(child => {
if(child.style.display !== 'none'){
child._preScrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
}
})
}
function title(value) {
document.title = value
}

View File

@ -22,7 +22,10 @@ const mpOption = Page({
//事件处理函数
bindViewTap: function() {
wx.navigateTo({
url: '../logs/logs?name=dntzhang&age=18'
url: '../logs/logs?name=dntzhang&age=18',
// alive: true, //当前视图是否alive默认ture
// scollTop: 0 //目标url的scollTop不传scollTop的话使用以前保留的
// 浏览器后退行为刚好 alive用truescollTop不传
})
},
onLoad: function () {
@ -103,3 +106,4 @@ function render() {
}
customElements.define('we-index', Element)

View File

@ -75,9 +75,6 @@ function getUrlParams(url) {
export function routeUpdate(node, selector, byNative, root) {
root.childNodes.forEach(child => {
if(child.style.display !== 'none'){
child._preScrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop
}
child.style.display = 'none'
})
if (byNative) {