分享一下项目中用到过的小知识

1、jq上传进度

var xhrOnProgress = function (fun) {
xhrOnProgress.onprogress = fun;
return function () {
    var xhr = $.ajaxSettings.xhr();
    if (typeof xhrOnProgress.onprogress !== 'function')
        return xhr;
    if (xhrOnProgress.onprogress && xhr.upload) {
        xhr.upload.onprogress = xhrOnProgress.onprogress;
    }
    return xhr;
}
};
$.ajax({
    type: "POST",
    url: "http://180.97.83.70:30990/wxapp/addUserPhotoAlbumCmd/",
    data: formData,
    processData: false, //必须false才会自动加上正确的Content-Type
    contentType: false,
    xhr: xhrOnProgress(function (e) {
        var percent = 100 * e.loaded / e.total;//计算百分比
        $("#upinfo").html(Math.floor(percent) + '%');
    }),
    success: function (res) {
        
    },
    error: function (e) {

    }
});
});

2、json排序

var testList = [
    {n: "1"},
    {n: "2"},
    {n: "3"},
    {n: "4"},
    {n: "5"}
];

function sortList(a, b) {
    return b.n - a.n
}
testList.sort(sortList)
console.log(testList)

3、获取7天时间日期

var dates=[];
var datestr;
function getDate() {
    var myDate = new Date();
    myDate.setDate(myDate.getDate()-6); //前7天
    for (var i = 0; i < 7; i++) {
        datestr=Number(myDate.getMonth()) + 1 + "-" + myDate.getDate();
        dates.push(datestr);
        myDate.setTime(myDate.getTime() + 1000*60*60*24);
        //console.log(datestr);
    }
    //$scope.proSevDay=dates;//日期
    //console.log(dates);
}

4、判断页面滚动到底部

$(document).ready(function () {
$(window).scroll(function () {
    if ($(document).scrollTop() >= $(document).height() - $(window).height()) {
        console.log("到底了")
    }
});
});

5、相同key值加入新数组

    var b = {};
    result.forEach(function (obj) {
        var array = b[obj['time']] || [];
        array.push(obj);
        b[obj['time']] = array;
    });
    console.log(b);

6、meta

<!-- 启用360浏览器的极速模式(webkit) -->
<meta name="renderer" content="webkit">
<!-- 避免IE使用兼容模式 -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->
<meta name="HandheldFriendly" content="true">
<!-- 微软的老式浏览器 -->
<!-- 微软的老式浏览器 -->
<meta name="MobileOptimized" content="320">
<!-- uc强制竖屏 -->
<meta name="screen-orientation" content="portrait">
<!-- QQ强制竖屏 -->
<meta name="x5-orientation" content="portrait">
<!-- windows phone 点击无高光 -->
<meta name="msapplication-tap-highlight" content="no">
    原文作者:Rossy1
    原文地址: https://segmentfault.com/a/1190000014729861
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞