jQuery 扩展功能
源码如下:
/*! * 说明:Jquery库扩展 * 创建时间: leo 2016/10/13 */ (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); //把相差的毫秒数转换为天数 }, }, /*操作*/ do: { // 指定 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); } }, // 处理多行文本溢出 末尾显示省略号 html布局限制 textOverflow: function (obj) { var oH, oText; oH = $(obj).height(); oText = $(obj).children(); while (oText.outerHeight() > oH) { oText.html(oText.html().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "...")); } }, // 删除数组里某个元素 removeItem: function (arr, item) { if (arr.length) { var index = arr.indexOf(item); if (index > -1) arr.splice(index, 1); } } }, //定位 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; }, // 获取地址栏特定name的值 getUrlParam: function (name) { var reg, oName; reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); oName = window.location.search.substr(1).match(reg); if (oName != null) return unescape(oName[2]); return null; } }, }) })(window, jQuery)
countMonth 这个函数是为了处理项目中购买数据的时间段,其中用到的 getLastDay 函数是用来获取某年某月有多少天,比较巧妙的运用了setDate(1),然后setMonth()。setTimeoutWidthNum 函数使用了递归的用法,对于 textOverflow 方法可以参考 这里 ,
div 垂直居中是大部分页面弹窗的需求,所以就通过 center 函数把 css 样式,通过 js 赋给 div。Cookie 值的保存、获取、移除也是很多的登录页的需求。
引入时需要先引入jquery.js,例如:
<script type="text/javascript" src="jquery-3.1.0.min.js"></script> <script type="text/javascript" src="leo.js"></script>
使用时直接调用,例如:$.date.countMonth('2015,10,30', 4)等。
本人会持续更新,谢谢支持 \(^o^)/~