关于MUI框架---返回顶部功能

最近用mui开发一个项目,遇到了不少的问题,当时也怪自己没熟读mui文档,以至于出现问题找不到头绪,下面我把这个返回顶部的功能说一下,mui有自己的方法来返回顶部,就是下边这个方法

mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 1000);//滚动到顶部

其实还好吧,但是没卵用,需要再加几行代码,下边的代码我也是网上找的,是自己改过的,下边贴出来

var scrollToTopBox = document.getElementById('scrollToTop');
//返回按钮tap
scrollToTopBox.addEventListener('tap', function(e) {
e.stopPropagation();
mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 1000);//滚动到顶部
window.scrollTo(0, 1000);
});

对了  没错 还得需要它自己的tap,来调用,很是无耻吧,这样可以了,这样的话兼容还是不够的,再来加几行代码

//监听div滚动,其实它这个是分别监听两个事件的,但是无耻的我让安卓和IOS都用div滚动,这样可以解决兼容性的问题,之前不兼容安卓,在安卓上不现实返回顶部按钮,但是如果你要是想用mui写的打包成APP的话,请看下边 介绍,这儿只适用于兼容靠浏览器访问的设备,不适合安装软件的使用

document.getElementById('pullrefresh').addEventListener('scrollend', function() {
if (mui('#pullrefresh').pullRefresh().y <= window.innerHeight * (-1) && scrollToTopBox.classList.contains('hide'))
scrollToTopBox.classList.remove('hide');
else if (mui('#pullrefresh').pullRefresh().y > window.innerHeight * (-1) && !scrollToTopBox.classList.contains('hide'))
scrollToTopBox.classList.add('hide');
});

在这儿贴出来在APP上返回顶部按钮的兼容代码,砸门需要再加几行,下边这个是在安卓监听body滚动的,上边那个是在IOS监听div滚动:

var scrollToTopBox = document.getElementById('scrollToTop'); //返回按钮tap scrollToTopBox.addEventListener('tap', function(e) { e.stopPropagation(); mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 100);//滚动到顶部 }); //Android上监听原生滚动,iOS上监听div滚动,上拉超过一屏后显示按钮,否则隐藏,可自行在条件判断中修改 if (mui.os.android) { window.addEventListener('scroll', function(e) { if (window.pageYOffset >= window.innerHeight && scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.remove('hide'); else if (window.pageYOffset < window.innerHeight && !scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.add('hide'); }); } else { document.getElementById('pullrefresh').addEventListener('scroll', function() { if (mui('#pullrefresh').pullRefresh().y <= window.innerHeight * (-1) && scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.remove('hide'); else if (mui('#pullrefresh').pullRefresh().y > window.innerHeight * (-1) && !scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.add('hide'); }); }

下边总结下以上代码:完整的,哈哈,被骗了吧,完整的代码如下

html 部分:

<a id="scrollToTop" class="backTop hide"> <span class="mui-icon mui-icon-arrowup"></span> </a>

CSS  部分:

.hide { display: none; } .backTop { background: #DDDDDD; border-radius: 50%; position: fixed; right: 10px; bottom: 15px; width: 38px; height: 38px; z-index: 9999; text-align: center; font-size: 18px; color: #666666; padding-top: 8px; opacity: 0.8; }

js 部分:

var scrollToTopBox = document.getElementById('scrollToTop'); //返回按钮tap scrollToTopBox.addEventListener('tap', function(e) { e.stopPropagation(); mui('#pullrefresh').pullRefresh().scrollTo(0, 0, 100);//滚动到顶部 }); //Android上监听原生滚动,iOS上监听div滚动,上拉超过一屏后显示按钮,否则隐藏,可自行在条件判断中修改 if (mui.os.android) { window.addEventListener('scroll', function(e) { if (window.pageYOffset >= window.innerHeight && scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.remove('hide'); else if (window.pageYOffset < window.innerHeight && !scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.add('hide'); }); } else { document.getElementById('pullrefresh').addEventListener('scroll', function() { if (mui('#pullrefresh').pullRefresh().y <= window.innerHeight * (-1) && scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.remove('hide'); else if (mui('#pullrefresh').pullRefresh().y > window.innerHeight * (-1) && !scrollToTopBox.classList.contains('hide')) scrollToTopBox.classList.add('hide'); }); }

 

备注:如果不是打包APP请在完整代码中把安卓监听部分去掉就可以解决兼容性问题,因为,打包成APP安卓监听部分才会生效,mui满满的都是坑啊!

posted @ 2016-05-19 10:55  绿罗兰  阅读(12933)  评论(1编辑  收藏  举报