公共工具库commonUtil.js

/********************
 * 日币转人民币,Jprice:日币;JP2CN:汇率
 *******************/
function formatJP2CN(Jprice, JP2CN) {
    return Math.ceil(parseInt(Jprice) * JP2CN);
}

/********************
 * 取窗口滚动条高度 
 ******************/
function getScrollTop() {
    var scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        scrollTop = document.documentElement.scrollTop;
    }
    else if (document.body) {
        scrollTop = document.body.scrollTop;
    }
    return scrollTop;
}

/********************
 * 取窗口可视范围的高度 
 *******************/
function getClientHeight() {
    var clientHeight = 0;
    if (document.body.clientHeight && document.documentElement.clientHeight) {
        var clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
    }
    else {
        var clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
    }
    return clientHeight;
}

/********************
 * 取文档内容实际高度 
 *******************/
function getScrollHeight() {
    return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
}

/********************
 * 取图片路径
 *******************/
function getPicURL(url, specs) {
    return (url || "").trim() + (specs || "").trim();
}

/********************
 * 取设备类型,ios返回true;android返回false.
 *******************/
function getUA() {
    var ua = navigator.userAgent.toLowerCase();
    if (/iphone|ipad|ipod/.test(ua)) {
        return true;
    } else if (/android/.test(ua)) {
        return false;
    }
}
/********************
 * 滚动顶部
 *******************/
function scrollToTop() {
      $("html, body").animate({ scrollTop: 0 }, 120);
}

学会封装一个工具类:

var Util = (function() {
    var prefix = 'html5_reader_';
    var StorageGetter = function(key) {
      return localStorage.getItem(key);
    }
    var StorageSetter = function(key, val) {
      return localStorage.setItem(key, val);
    }

    var getBSONP = function(url, callback) {
      return $.jsonp({
        url: url,
        cache: true,
        callback: "duokan_fiction_chapter",
        success: function(result) {
          console.log(result);
          // debugger;
          var data = $.base64.decode(result); //json
          var json = decodeURIComponent(escape(data));
          callback && callback(json);
        }
      })
    }


    return {
      StorageGetter: StorageGetter,
      StorageSetter: StorageSetter,
      getBSONP: getBSONP,
      scrollToTop: scrollToTop
    }
  })();

热评文章