UNPKG

landers.base

Version:

landers.base

378 lines (357 loc) 11.8 kB
(function($){ if (!$.browser) $.browser = { msie: Landers.utils.is_ie(), version: '' }; if ($.fn.off && !$.fn.die) $.fn.die = $.fn.off; /** * [ifShow description] * @param {[type]} condition [description] * @param {Function} callback [description] * @return {[type]} [description] */ $.fn.ifShow = function( condition, callback ){ var me = this, $me = $(me); if ( condition ) $me.show(function(){ callback && callback.call(me, condition); }); else $me.hide(function(){ callback && callback.call(me, condition); }); }; /** * jQuery.toTop.js v1.0 $('.to-top').toTop({ //options with default values autohide: true, //boolean 'true' or 'false' offset: 420, //numeric value (as pixels) for scrolling length from top to hide automatically speed: 500, //numeric value (as mili-seconds) for duration right: 15, //numeric value (as pixels) for position from right bottom: 30 //numeric value (as pixels) for position from bottom }); **/ $.fn.toTop = function(t) { var e = this, i = $(window), s = $("html, body"), n = $.extend({ autohide: false, offset: 420, speed: 500, right: 15, bottom: 30 }, t); e.css({ right: n.right, bottom: n.bottom, cursor: "pointer" }), n.autohide && e.css("display", "none"), e.click(function() { s.animate({ scrollTop: 0 }, n.speed) }), i.scroll(function() { var o = i.scrollTop(); n.autohide && (o > n.offset ? e.fadeIn(n.speed) : e.fadeOut(n.speed)) }) }; /** * [visible description] * @return {[type]} [description] */ $.fn.visible = function(){ return $(this).css('visibility', 'visible'); }; /** * [hidden description] * @return {[type]} [description] */ $.fn.hidden = function(){ return $(this).css('visibility', 'hidden'); }; /** * [boxsize description] * @param {[type]} size [description] * @return {[type]} [description] */ $.fn.boxsize = function(size) { if (size) { $(this).width(size.width).height(size.height); return this; } else { return { width: $(this).width(), height: $(this).height() } } }; /** * [setCenter description] */ $.fn.setCenter = function() { var $jq = $(this); $jq.css({ 'text-align': 'center', 'vertical-align': 'middle', 'left': '50%', 'top': '50%' }); var pos = $jq.css('position'); if (pos == 'absolute' || pos == 'fixed') { $jq.css({ 'margin-left': -$jq.width() / 2 + 'px', 'margin-top': -$jq.height() / 2 + 'px' }); } else { $jq.css({ magin: '0 auto' }); }; return $jq; }; /** * [tag description] * @return {[type]} [description] */ $.fn.tag = function() { if (!this.length) return ''; return this.get(0).tagName.toLowerCase(); }; /** * [outerhtml description] * @return {[type]} [description] */ $.fn.outerhtml = $.fn.outerHtml = function() { var $tmp = $('<p></p>'), $clone = this.clone().appendTo($tmp), html = $tmp.html(); $tmp.remove(); return html; }; /** * [response description] * @param {[type]} dat [description] * @param {[type]} time [description] * @return {[type]} [description] */ $.fn.response = function(dat, time) { var $jq = $(this); var delay = 300, msg; if (dat === null) msg = ''; else if (typeof dat === 'string') msg = dat; else if ($.isArray(dat)) msg = dat[1]; if (time === true) time = 8 * delay; var hide = function($_o, _time) { setTimeout(function() { $_o.fadeOut(delay); }, _time); }; $jq.addClass('response'); var $o = $jq.children(), _class; if (dat !== null) { if (typeof dat === 'string') _class = 'info'; else if (typeof dat[0] === 'boolean') _class = dat[0] ? 'error' : 'success'; else if ($.isNumeric(dat[0])) _class = dat[0] < 10 ? (dat[0] == 0 ? 'success' : 'info') : 'error'; else _class = !!dat[0] ? 'error' : 'success'; }; if (msg) { $o.remove(); $o = $('<span class="text"></span>') .addClass(_class) .hide() .appendTo($jq) .html(msg) .fadeIn(delay, function() { if (time) hide($(this), time); }); } else { hide($o, time); } return $jq; }; /** * [showSpin description] * @param {[type]} opts [description] * @return {[type]} [description] */ $.fn.showSpin = function(opts) { this.each(function() { var $this = $(this), data = $this.data(); if (data.spinner) { data.spinner.stop(); delete data.spinner }; if (opts !== false) { opts = $.extend({ color: $this.css('color') }, opts); data.spinner = new Spinner(opts).spin(this) }; }); return this; }; /** * [mySize description] * @return {[type]} [description] */ $.fn.mySize = function() { var $jq = $(this), o = $jq.get(0); if (!o) { o = window; $jq = $(o); } var w, sw, h, sh, ret = {}, debug = false; if ($.isWindow(o)) { var win = o, doc = document, body = doc.body; if (win.innerHeight) { if (debug) alert(11); w = doc.documentElement.clientWidth; w = w ? w : win.innerWidth, h = win.innerHeight } else if (doc.documentElement && doc.documentElement.clientHeight) { if (debug) alert(12); w = doc.documentElement.clientWidth; h = doc.documentElement.clientHeight } else if (body) { if (debug) alert(13); w = body.clientWidth; h = body.clientHeight }; ret.width = w; ret.height = h; if (win.innerHeight && win.scrollMaxY) { if (debug) alert(21); sw = win.innerWidth + win.scrollMaxX; sh = win.innerHeight + win.scrollMaxY; } else if (body.scrollHeight > body.offsetHeight) { if (debug) alert(22); sw = body.scrollWidth; sh = body.scrollHeight; } else { if (debug) alert(23); sw = body.offsetWidth; sh = body.offsetHeight; }; ret.scroll_width = sw; ret.scroll_height = sh; } else { ret = { width: $jq.width(), height: $jq.height(), scroll_width: o.scrollWidth, scroll_height: o.scrollHeight } }; return ret; }; /** * [overlay description] * @param {[type]} act [description] * @param {[type]} opts [description] * @return {[type]} [description] */ $.fn.overlay = function(act, opts) { act = act.toLowerCase(); opts = opts || {}; var $jq = $(this), delay = 300, data_key = 'overlay'; if (!$.isWindow($jq.get(0)) && !$jq.tag() == 'body') { $jq.css({ position: 'relative' }); }; switch (act) { case 'show': var mysize = $jq.mySize(); var opacity = opts.alpha || opts.opacity; if (opacity === undefined) opacity = 0.2; if (opacity > 1) opacity = opacity / 100; $jq.data(data_key, $('<div></div>').css({ width: '100%', height: Math.max(mysize.height, mysize.scroll_height), position: 'absolute', left: '0px', top: '0px', filter: 'alpha(opacity=' + (opacity * 100) + ')', opacity: opacity, 'background-color': opts.bgcolor || '#000000', 'z-index': opts.zindex || 4001 }).appendTo($jq).fadeIn(delay)); break; case 'hide': $($jq.data(data_key)).fadeOut(delay, function() { $(this).remove(); }); $jq.data(data_key, null); } return this; }; /* var gotoTag = function(id, val) { if (val == undefined) { val = 0; } var id = (id) ? $(id).offset().top : 0, str = id - val; $('html, body').stop().animate({scrollTop: str}, 800, 'easeInOutQuart'); }; */ /** * [scrollMe description] * @param {[type]} offset [description] * @return {[type]} [description] */ $.fn.scrollMe = function(offset) { var $jq = $(this), offset = offset = 0; $('html, body').stop().animate({ scrollTop: $jq.offset().top-offset }, 800, 'easeInOutQuart'); }; /** * 设置状态 * [status description] * @return {[type]} [description] */ $.fn.status = function(status, options){ var texts = {}; var $me = $(this); if ( !$me.data('original-html') ) { $me.data('original-html', $me.html()); } var styles = { loading: { html: '<i class="glyphicon glyphicon-cog glyphicon-spin fa fa-cog fa-spin"></i>', disabled: true, }, success: { html: '<i class="glyphicon glyphicon-ok"></i>', disabled: true, }, error: { html: '<i class="glyphicon glyphicon-minus-sign"></i>', disabled: false, }, countdown: { html: '', disabled: true, callback: function(){ var seconds = options.seconds; var callback = options.callback; var scene = options.scene || 'sms'; switch (options.scene){ case 'sms': Landers.helpers.countdown.sms($me, seconds, function(){ callback && callback($me, seconds); }); break; } } } }; for (var k in styles) { var html = $me.data('status-' + k); styles[k].html = html ? styles[k].html + ' ' + html : $me.data('original-html'); }; var style = styles[status]; if ( style ) { $me.html(style.html).prop('disabled', style.disabled); if ( style.disabled ) { $me.addClass('disabled'); } else { $me.removeClass('disabled'); } style.callback && style.callback(); } return this; }; })(jQuery);