jQuery擴充功能

來源:互聯網
上載者:User

標籤:

源碼如下:

/*!* 說明:Jquery庫擴充* 建立時間: leo 2016-8-4 
* 更新時間:2016-8-5
*/(function (window, jQuery, undefined) { jQuery.extend({ /*日期時間處理*/ // data參數的格式為日期格式 date: { // 添加/減去月份 num為正就是加 為負就是減 countMonth: function(date, num){ var oDate, oMonth, oDay; oDate = new Date(date); oDay = oDate.getDate(); oDate.setDate(1); oDate.setMonth(oDate.getMonth() + num); oMonth = oDate.getMonth() + 1; oDay = $.date.getLastDay(oDate.getFullYear(), oDate.getMonth() + 1); return oDate.getFullYear() + ‘-‘ + (oMonth < 10 ? ‘0‘ : ‘‘) + oMonth + ‘-‘ + (oDay < 10 ? ‘0‘ : ‘‘) + oDay; }, // 擷取最後一天 getLastDay: function(year, month){ var oDt; oDt = new Date(year, month - 1, ‘01‘); oDt.setDate(1); oDt.setMonth(oDt.getMonth() + 1); return new Date(oDt.getTime() - 1000*60*60*24).getDate(); }, // 計算 date1 - date2 得到相差天數 countDay: function (date1, date2) { return parseInt(Math.abs(new Date(date1) - new Date(date2)) / 86400000); //把相差的毫秒數轉換為天數 }, }, /*操作*/ action: { // 指定 setTimeout 執行的次數 預設時間1秒 次數5次 setTimeoutWidthNum: function (fun, time = 1000, num = 5) { if (fun != null) { setTimeout(function () { fun(); num --; if (num > 0) $.action.setTimeoutWidthNum(fun, time, num); }, time); } }, }, //定位 position: { //使頁面元素置中 isTure1代表是否左右置中 isTure2代表是否垂直置中 預設上下垂直置中 center: function (el, isTure1 = true, isTure2 = true) { var obj = $(el); if (obj.length > 0) { obj.each(function () { var _this, oW, oH; _this = $(this); oW = _this.width(); oH = _this.height(); _this.css(‘position‘, ‘absolute‘); if (isTure1) _this.css({ left: ‘50%‘, marginLeft: - oW / 2 }); if (isTure2) _this.css({ top: ‘50%‘, marginTop: - oH / 2 }); }) } } }, /*瀏覽器*/ browser: { // 設定名稱為name 值為value的Cookie value為空白就移除Cookie 儲存時間預設3天 setCookie: function (name, value, time = 3 * 24 * 60 * 60 * 1000) { if (value == ‘‘) time = 0; var expdate = new Date(); //初始化時間 expdate.setTime(expdate.getTime() + time); //時間 document.cookie = name + "=" + value + ";expires=" + expdate.toGMTString() + ";path=/"; }, // 擷取cookie的value getCookie: function (cookieName) { var cookieValue, cookieStartAt, cookieEndAt; cookieValue = document.cookie; cookieStartAt = cookieValue.indexOf("" + cookieName + "="); if (cookieStartAt == -1) cookieStartAt = cookieValue.indexOf(cookieName + "="); if (cookieStartAt == -1) { cookieValue = null; }else { cookieStartAt = cookieValue.indexOf("=", cookieStartAt) + 1; cookieEndAt = cookieValue.indexOf(";", cookieStartAt); if (cookieEndAt == -1) cookieEndAt = cookieValue.length; cookieValue = unescape(cookieValue.substring(cookieStartAt, cookieEndAt));//解碼latin-1 } return cookieValue; }, }, })})(window, jQuery)

 

引入時需要先引入jquery.js,如下:

<script type="text/javascript" src="jquery-3.1.0.min.js"></script><script type="text/javascript" src="leo.js"></script>

 

使用時直接調用,如下:

console.log($.date.countMonth(‘2015,10,30‘, 4));          // 2016-02-29console.log($.date.countMonth(‘2015,10-30‘, -8));         // 2015-02-28console.log($.date.countDay(‘2016-8/2‘,‘2016,7-20‘));      // 13var n = 0;$.action.setTimeoutWidthNum(func);                  // 預設調用5次function func(){    console.log(n);                           // 0, 1, 2, 3, 4    n ++;}$.position.center(‘#box‘);                        // 上下垂直置中
$.position.center(‘#box‘, true, false);            // 左右置中
$.position.center(‘#box‘, false);               // 上下置中
$.browser.setCookie(‘uname‘, ‘leo‘);    console.log($.browser.getCookie(‘uname‘));             // leo$.browser.setCookie(‘uname‘,‘‘);console.log($.browser.getCookie(‘uname‘));             // null

本人會持續更新,謝謝支援 \(^o^)/~

jQuery擴充功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.