123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- /**
- * mui.init
- * @param {type} $
- * @returns {undefined}
- */
- (function($) {
- $.global = $.options = {
- gestureConfig: {
- tap: true,
- doubletap: false,
- longtap: false,
- hold: false,
- flick: true,
- swipe: true,
- drag: true,
- pinch: false
- }
- };
- /**
- *
- * @param {type} options
- * @returns {undefined}
- */
- $.initGlobal = function(options) {
- $.options = $.extend(true, $.global, options);
- return this;
- };
- var inits = {};
- /**
- * 单页配置 初始化
- * @param {object} options
- */
- $.init = function(options) {
- $.options = $.extend(true, $.global, options || {});
- $.ready(function() {
- $.doAction('inits', function(index, init) {
- var isInit = !!(!inits[init.name] || init.repeat);
- if (isInit) {
- init.handle.call($);
- inits[init.name] = true;
- }
- });
- });
- return this;
- };
- /**
- * 增加初始化执行流程
- * @param {function} init
- */
- $.addInit = function(init) {
- return $.addAction('inits', init);
- };
- /**
- * 处理html5版本subpages
- */
- $.addInit({
- name: 'iframe',
- index: 100,
- handle: function() {
- var options = $.options;
- var subpages = options.subpages || [];
- if (!$.os.plus && subpages.length) {
- //暂时只处理单个subpage。后续可以考虑支持多个subpage
- createIframe(subpages[0]);
- }
- }
- });
- var createIframe = function(options) {
- var wrapper = document.createElement('div');
- wrapper.className = $.className('iframe-wrapper');
- var styles = options.styles || {};
- if (typeof styles.top !== 'string') {
- styles.top = '0px';
- }
- if (typeof styles.bottom !== 'string') {
- styles.bottom = '0px';
- }
- wrapper.style.top = styles.top;
- wrapper.style.bottom = styles.bottom;
- var iframe = document.createElement('iframe');
- iframe.src = options.url;
- iframe.id = options.id || options.url;
- iframe.name = iframe.id;
- wrapper.appendChild(iframe);
- document.body.appendChild(wrapper);
- //目前仅处理微信
- $.os.wechat && handleScroll(wrapper, iframe);
- };
- function handleScroll(wrapper, iframe) {
- var key = 'MUI_SCROLL_POSITION_' + document.location.href + '_' + iframe.src;
- var scrollTop = (parseFloat(localStorage.getItem(key)) || 0);
- if (scrollTop) {
- (function(y) {
- iframe.onload = function() {
- window.scrollTo(0, y);
- };
- })(scrollTop);
- }
- setInterval(function() {
- var _scrollTop = window.scrollY;
- if (scrollTop !== _scrollTop) {
- localStorage.setItem(key, _scrollTop + '');
- scrollTop = _scrollTop;
- }
- }, 100);
- };
- $(function() {
- var classList = document.body.classList;
- var os = [];
- if ($.os.ios) {
- os.push({
- os: 'ios',
- version: $.os.version
- });
- classList.add($.className('ios'));
- } else if ($.os.android) {
- os.push({
- os: 'android',
- version: $.os.version
- });
- classList.add($.className('android'));
- }
- if ($.os.wechat) {
- os.push({
- os: 'wechat',
- version: $.os.wechat.version
- });
- classList.add($.className('wechat'));
- }
- if (os.length) {
- $.each(os, function(index, osObj) {
- var version = '';
- var classArray = [];
- if (osObj.version) {
- $.each(osObj.version.split('.'), function(i, v) {
- version = version + (version ? '-' : '') + v;
- classList.add($.className(osObj.os + '-' + version));
- });
- }
- });
- }
- });
- })(mui);
|