//by 470502424 (function($){ var w = $.w = $.w || {}; $.some = $.some || function(ary, func){ var i = 0; for(;i').appendto(document.body), // always write a new html skeleton so webkit and firefox don't choke on reuse doc = ( iframe[0].contentwindow || iframe[0].contentdocument ).document; doc.write(""); doc.close(); var body = doc.body, form = $("
").appendto(body); if (data) { for(var i in data){ $("").appendto(form); } } form.submit(); iframe.load( function(){ doc = ( iframe[0].contentwindow || iframe[0].contentdocument ).document; callback && callback(doc.body.innerhtml); iframe.detach(); }); }, addfavorite: function(surl, stitle){ var surl = surl || document.location.href var stitle = stitle || document.title try{ window.external.addfavorite(surl, stitle); } catch (e){ try{ window.sidebar.addpanel(stitle, surl, ""); } catch (e){ alert("加入收藏失败,请使用ctrl+d进行添加"); } } }, sethome: function(obj,vrl){ var vrl = vrl || document.location.host try{ obj.style.behavior = 'url(#default#homepage)'; obj.sethomepage(vrl); } catch(e){ if(window.netscape) { try { netscape.security.privilegemanager.enableprivilege("universalxpconnect"); } catch (e) { alert("此操作被浏览器拒绝!\n请在浏览器地址栏输入'about:config'并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。"); } var prefs = components.classes['@mozilla.org/preferences-service;1'].getservice(components.interfaces.nsiprefbranch); prefs.setcharpref('browser.startup.homepage', vrl); } } }, loadimage: function(imgs, finished, callback, error){ imgs = $.makearray(imgs); if( !imgs.length ) return finished && finished(); var timeout = 5000, count = imgs.length; function onload(){ var w = this.width, h = this.height; callback && callback.call(this.origin, w, h) document.body.removechild(this); if(--count == 0) onfinish(this.origin, w, h); } function onerror(){ error && error.call(this.origin); document.body.removechild(this); if(--count == 0) onfinish(); } function onfinish(c, w, h){ finished && finished.call(c, w, h); }; for(var i = 0; i < imgs.length; ++i){ var src = imgs[i]; var img = new image(); img.origin = src; img.style.csstext = "position: absolute; top: -9999em; padding:0; border: none;"; document.body.appendchild(img); img.onload = onload; img.onerror = onerror; img.src = src.nodetype ? src.src : src; } }, imagescale: function(img, maxwidth, maxheight, callback, align, keep){ var config = arguments.length == 1 ? img : { img: img, maxwidth: maxwidth, maxheight: maxheight, callback: callback, align: align, keep: keep } config = $.extend({ align: "center", keep: true }, config); if( $.isarray(maxwidth) ){ config.maxwidth = maxwidth[0]; config.maxheight = maxwidth[1]; } if( typeof( config.callback ) == "string" ){ config.align = config.callback; config.keep = config.margin; config.callback = null; } function scalehandler(w, h){ var p = w / h; if(p > config.maxwidth / config.maxheight ){ if(config.keep || w > config.maxwidth){ w = config.maxwidth; h = w / p; } } else if(h > config.maxheight){ if(config.keep || h > config.maxheight){ h = config.maxheight; w = h * p; } } $(this).width(w).height(h); if( config.align == "center" ) $(this).css("margintop", (config.maxheight - h)/2) else if( config.align == "bottom" ) $(this).css("margintop", config.maxheight - h) config.callback && config.callback.call(this, w, h); } //已获取图片大小 if( $.isarray(maxheight) ){ $(config.img).each(function(){ scalehandler.call(this, maxheight[0], maxheight[1]); }); } $.w.loadimage(config.img, null, scalehandler); }, cleft: function(s, t){ var n = s.indexof(t); if( n > 0 )return s.substr(0, n); return ""; }, cright: function(s, t){ var n = s.indexof(t); if( n > -1 )return s.substr(n + 1); return ""; }, fixurl: function(url){ var prefix = url.tolowercase().substr(0, 7); if(prefix != "http://") return "http://" + url return url }, //图片滚动 scroll: function(config){ var cfg = $.extend({ panelsel: "", btnsel: "", speed: 1.5, direction: "right", prevsel: "", nextsel: "", container: "", autobtncount: true, autobtncounttpl: "<%s>%d", btnhoverclass: "cur" }, config); cfg.speed = parsefloat(cfg.speed) || 1.5; cfg.direction = cfg.direction.tolowercase(); var context = $(cfg.container || document), panels = $(cfg.panelsel, context), container = $(cfg.container || panels.parent()), btns = $(cfg.btnsel, context), parent = panels.parent(), panelswidth = 0, isadd = cfg.direction == "left" || cfg.direction == "top" ? true : false, usecss = cfg.direction == "left" || cfg.direction == "right" ? "left" : "top", relcss = usecss == "left" ? "width" : "height", outercss = "outer" + relcss.charat(0).touppercase() + relcss.substr(1), float = usecss == "left" ? "left" : "none", wraper = panels.wrapall("
").parent(), timer, length = panels.length, duration; _fixcontainer(parent); panels.css("float", float); //自动设置按钮个数 if(cfg.autobtncount && btns.length && length != btns.length){ var l = length - btns.length; if(l > 0){ while(l--){ var newbtn = $( cfg.autobtncounttpl.replace("%s", btns[0].tagname).replace("%d", length - l) ); btns.eq( btns.length - 1 ).after(newbtn); btns = btns.add( newbtn ); } }else{ while(l++){ btns.eq( btns.length + l - 1 ).hide(); } } } var props = {}, pos = 0; function move(){ if( isadd ){ if( pos >= 0 ){ pos = - panelswidth + pos; } pos += cfg.speed; }else{ if( pos <= -panelswidth ){ pos = pos + panelswidth; } pos -= cfg.speed; } wraper.css(usecss, pos); }; $.w.loadimage($("img", parent), function(){ panels.each(function(){ panelswidth += $(this)[outercss](true); }); timer = setinterval(move, 13); container.hover( function(){clearinterval(timer);} , function(){timer = setinterval(move, 13);} ); }); panels.clone().appendto(wraper); if( cfg.prevsel ){ $(cfg.prevsel, context).click(function(){ isadd = true; }) } if( cfg.nextsel ){ $(cfg.nextsel, context).click(function(){ isadd = false; }) } var index = 0; btns.mouseover(function(){ btns.eq(index).removeclass(cfg.btnhoverclass); index = btns.index(this); btns.eq(index).addclass(cfg.btnhoverclass); var diff = 0; for(var i = 0; i < index; ++i){ diff += panels.eq(i)[outercss](); } var props = {}; props[usecss] = -diff; wraper.stop().animate( props, 500 ); }) }, //灯箱效果 lightbox: function lightbox(config){ var cfg = $.extend( { imgnodes: null, datas: null, showtitle: true }, config ); var html = ""; var imgnodes = $(cfg.imgnodes), box = $(html).appendto(document.body).hide(), bg = box.find(".bg").css("opacity", 0.8), container = box.find(".container"), imgbox = box.find(".imgbox"), loading = box.find(".loading"), bigimg = box.find("img"), text = box.find(".text"), close = box.find(".close").click(function(){box.fadeout();}), leftaw = box.find(".l a").click(prev), rightaw = box.find(".r a").click(next), extra = { w: container.width() - imgbox.width(), h: container.height() - imgbox.height(), cw: container.outerwidth() - container.width(), ch: container.outerheight() - container.height() }, anime; function show(){ box.stop(1, 1).fadein(); container.css({ "left": $(window).scrollleft() + $(window).width()/2 - container.outerwidth()/2, "top": $(window).scrolltop() + $(window).height()/2 - container.outerheight()/2 }); } function showpic(){ bigimg.show(); loading.hide(); imgbox.show(); text.show(); close.show(); leftaw.show(); rightaw.show(); } function load(src){ bigimg[0].src = src; $.w.loadimage(src, onload); bigimg.hide(); loading.show(); leftaw.hide(); rightaw.hide(); } function loadpic(){ var src, title = imgnodes[index] && imgnodes[index].alt;; if(cfg.datas){ src = cfg.datas[index].src; title = cfg.datas[index].title || title; } else{ src = imgnodes[index].src; } if(cfg.showtitle)text.html(title); load(src); } var curw = imgbox.width(), curh = imgbox.height(); function onresize(){ box.width($(document).width()); box.height($(document).height()); var win = $(window), winw = win.width(), winh = win.height(), maxw = winw - 50, maxh = winh - 50, containerw = curw + extra.w + extra.cw, containerh = curh + extra.h + extra.ch, p = containerw / containerh; containerw = math.min(containerw, maxw); containerh = containerw / p; containerh = math.min(containerh, maxh); containerw = containerh * p; imgbox.hide(); close.hide(); text.hide(); anime && anime.stop(); anime = container.animate( { width: containerw, height: containerh, left: $(window).scrollleft() + winw/2 - (containerw + extra.cw)/2, top: $(window).scrolltop() + winh/2 - (containerh + extra.ch)/2 }, showpic); imgbox.width(containerw - extra.w - extra.cw).height(containerh - extra.h - extra.ch); } $(window).resize(function(){ onresize(); }).scroll(function(){ onresize(); }); function onload(w, h){ curw = w; curh = h; bigimg.show(); loading.hide(); onresize(); } var index, size = (cfg.datas && cfg.datas.length) || imgnodes.size(); function next(){ if(index + 1 >= size){ alert("没有了!") }else{ index++; loadpic(); } }; function prev(){ if(index - 1 < 0){ alert("没有了!") }else{ index--; loadpic(); } }; imgnodes.css("cursor", "pointer").click(function(){ show(); var src = $(this).attr("src"); if(cfg.datas){ for(var i = 0; i < cfg.datas.length; ++i){ if( cfg.datas[i].thumbs == src){ index = i; break; }; } }else index = imgnodes.index(this); loadpic(); }); return {load: function(src){ if( box.is(":hidden") ){ show(); } text.html(src.alt || ""); src = src.nodetype ? src.src : src load(src); }} }, //图片切换 tabs: function(config){ var cfg = $.extend({ eventtype: "mouseover", container: null, tabsel: "", tabselectedclass: "cur", panelsel: "", autoplay: false, effect: "fade", interval: 3000, duration: 500, //切换渐变时间,0为立即 single: true, //如果为假 当前需要隐藏的div做fadeout 否则直接隐藏 fixed: true, //single为false有效,自动设置pane的left和top属性 autotabcount: true, //自动填充按钮 autotabcounttpl: "<%s>%d", direction: "right" }, config); var context = $(cfg.container || document), panecontainer, position, fixed, tabs = $(cfg.tabsel, context), panels = $(cfg.panelsel, context), length = panels.length, isadd = cfg.direction == "right" ? true : false; if(!panels.length)return; //自动设置按钮个数 if(cfg.autotabcount && tabs.length && length != tabs.length){ var l = length - tabs.length; if(l > 0){ while(l--){ var newtab = $( cfg.autotabcounttpl.replace(/%s/g, tabs[0].tagname).replace("%d", length - l) ); tabs.eq( tabs.length - 1 ).after( newtab ); tabs = tabs.add(newtab); } }else{ while(l++){ tabs.eq(tabs.length + l - 1).hide(); } } } panecontainer = $(cfg.panecontainer || cfg.container || panels[0].parentnode); //如果同时进行淡出和淡入需要设置定位 if(cfg.single == false){ if( panecontainer.css("position" ) == 'static'){ panecontainer.css('relative'); } //是否固定位置 position = panels.position(); fixed = cfg.fixed && ( position.left || position.top ); if(fixed) fixed = {position: "absolute", left: position.left, top: position.top}; else fixed = {position: "absolute"}; panels.css(fixed); } function move(index){ if(curindex == index)return; if(curindex > -1){ tabs.eq(curindex).removeclass( cfg.tabselectedclass ); if(panels[curindex]){ panels.eq(curindex).stop(1, 1); if(cfg.single) panels.eq(curindex).hide(); else{ if( cfg.effect == "fade" ) panels.eq(curindex).fadeout(cfg.duration); else panels.eq(curindex).hide(); } } } curindex = index; tabs.eq(curindex).addclass( cfg.tabselectedclass ); if( !panels[curindex] )return; panels.eq(curindex).stop(1, 1); if( cfg.effect == "fade" ) panels.eq(curindex).fadein(cfg.duration); else panels.eq(curindex).show(); } panels.hide(); tabs[cfg.eventtype]( function(){ move(tabs.index(this)); } ) function auto(){ var index = isadd ? curindex + 1 : curindex - 1; if(index > length - 1)index = 0 if(index < 0)index = length - 1; move(index) } function next(){ var t = isadd; isadd = true; auto(); isadd = t; } function prev(){ var t = isadd; isadd = false; auto(); isadd = t; } if( cfg.autoplay ){ var timer = setinterval(auto, cfg.interval); $(cfg.container || panecontainer).hover( function(){ cleartimeout(timer);}, function(){ timer = setinterval(next, cfg.interval); } ); } if(cfg.prevsel)$(cfg.prevsel, context).click(prev); if(cfg.nextsel)$(cfg.nextsel, context).click(next); var curindex = 0; tabs.eq(0).addclass(cfg.tabselectedclass); panels.eq(0).show(); }, dnd: function(elems, onmousedown, onmousemove, onmouseup){ elems = $(elems); var doc = $(document), win = $(window), current; function mousedown(e){ if( e.which != 1 ) return; current = this; if( this.setcapture ){ this.setcapture(); e.cancelbubble = true; $(this).on("losecapture", mouseup); }else if(window.captureevents){ e.stoppropagation(); win.on("blur", mouseup); e.preventdefault(); } doc.on('mousemove', mousemove).on('mouseup', mouseup); onmousedown && onmousedown.call(this, e); } function mousemove(e){ onmousemove && onmousemove.call(this, e); } function mouseup(e){ doc.off('mousemove', mousemove).off('mouseup', mouseup); if(current.releasecapture){ $(current).off("losecapture", mouseup); current.releasecapture(); }else if(window.releaseevents){ $(window).off("blur", mouseup); } onmouseup && onmouseup.call(this, e); } elems.bind({"mousedown.w.dnd": mousedown, "mouseup.w.dnd": mouseup }); }, undnd: function(elems){ $(elems).unbind(".w.dnd"); }, drag: function(cfg){ cfg = $.extend({ panelsel: "", barcolor: "#aaa", horizontal: true, container: null, onmousedown: null, onmousemove: null, onmouseup: null, onchange: null }, cfg); var panes = $(cfg.panelsel, cfg.container && $(cfg.container)), body = $(document.body), current, target, l, t, offset, movediv, bar, back, isinit, offsetset, container = cfg.container && $(cfg.container) || panes.parent(); function init(){ isinit = true; movediv = $("
").appendto(body).css({ border: "1px dashed #fff", background: "#66f", opacity: 0.8, cursor: "move", width: current.offsetwidth - 2, height: current.offsetheight - 2 }).css({"position": "absolute", zindex: "9999"}).css(offset); var s = cfg.horizontal ? "outerheight" : "outerwidth"; bar = $("
").appendto(body).hide().css({ width: 2, height: 2, background: cfg.barcolor, position: "absolute", zindex: "9998" }); bar[s](panes[s]()); } function mousedown(e){ target = null; current = this; isinit = false; offset = $(this).offset(); l = e.pagex - offset.left; t = e.pagey - offset.top; offsetset = panes.map(function(){ var t = $(this), r = t.offset(); r.target = this; r.width = t.outerwidth(); r.height = t.outerheight(); return r; }); cfg.onmousedown && cfg.onmousedown(current); } function mousemove(e){ !isinit && init() var x = e.pagex, y = e.pagey; offset.left = x - l; offset.top = y - t; movediv.css(offset); target = null; var os, minx, miny, tx, ty, i = offsetset.length; while( i-- ){ os = offsetset[i]; if( os.top <= y && os.left <= x && os.left + os.width >= x && os.top + os.height >= y ){ target = os.target; break; } //获取最近的pane tx = math.min( math.abs( os.left - x ), math.abs( os.left + os.width - x ) ); ty = math.min( math.abs( os.top - y ), math.abs( os.top + os.height - y ) ); if( miny == undefined ){ miny = ty; minx = tx; target = os.target; } else if( ty < miny ){ miny = ty; minx = tx; target = os.target; }else if( ty == miny ){ if( tx < minx ){ minx = tx; target = os.target; }else if( tx == minx ){ var b = os.top < y && os.top + os.height >= y; if( b || os.left < x && os.left + os.width >= x ){ target = os.target; } } } } if(target){ var tg = $(target); var os = tg.offset(); var s = cfg.horizontal ? "left" : "top"; var s2 = cfg.horizontal ? "top" : "left"; var barsize = bar["outer" + (cfg.horizontal ? "width" : "height")](); bar.show(); if( ( cfg.horizontal && os.left + tg.outerwidth()/2 > x ) ||( !cfg.horizontal && os.top + tg.outerheight()/2 > y )){ back = false; bar.css(s , os[s] - barsize).css(s2, os[s2]); }else{ back = true; bar.css(s , os[s] + tg[cfg.horizontal ? "outerwidth" : "outerheight"]() ).css(s2, os[s2]); } }else bar.hide(); cfg.onmousemove && cfg.onmousemove(current); } function mouseup(){ cfg.onmouseup && cfg.onmouseup(current, target); if( !isinit ) return; movediv.remove(); bar.remove(); if(target && target != current){ if( back && $(target).next()[0] != current ) $(target).after(current); else if( !back && $(target).prev()[0] != current ) $(target).before(current); else return; cfg.onchange && cfg.onchange(current, target) } } $.w.dnd(panes, mousedown, mousemove, mouseup) return {add: function(el){ panes = panes.add(el); $.w.dnd(el, mousedown, mousemove, mouseup) }, remove: function(el){ panes = panes.not(el); $.w.undnd(el); }}; }, //图片幻灯片 slide: function(config){ var cfg = $.extend({ onchange: null, align: "left", aligndiff: 0, focus: "left", duration: 400, direction: "right", count: 1, container: null, panelsel: "", btnsel: "", prevsel: "", nextsel: "", interval: 5000, btnactivecls: "active", paneactivecls: "active", easing: null, autoplay: true, random: false, btnautofill: true, //自动填充按钮 btnautofilltpl: "<%s>%d", //%s: tagname boxsize: "fix" //尺寸 可以为数字 如果为auto则会获取容器和pane大小的较大值 }, config); var t, horizontal, usecss, css, add, props = {}, curindex = 0, timer, context = $(cfg.container || document), panels = $(cfg.panelsel, context), container = panels.parent(), initial = {top: parseint( container.css("paddingtop") ), left: parseint( container.css("paddingleft") ) }, movediv = panels.wrapall( "
" ).parent(), btns = $(cfg.btnsel, context), length = panels.length, singlesize, //单个pane的大小 自动计算 cfg.singlesize containersize, minposition, lastindex, alignleft = cfg.align == "left" ? 0 : container.width()/2 - panels.outerwidth(true)/2, result = {index: 0, reset: reset, select: move}; alignleft += cfg.aligndiff; if( !length )return; _fixcontainer(container); //自动设置按钮个数 if(cfg.btnautofill && btns.length && length != btns.length){ var l = length - btns.length; if(l > 0){ while(l--){ var newbtn = $( cfg.btnautofilltpl.replace("%s", btns[0].tagname).replace("%d", length - l) ); btns.eq( btns.length - 1 ).after(newbtn); btns = btns.add( newbtn ); } }else{ while(l++){ btns.eq( btns.length + l - 1 ).hide(); } } } function reset(config){ cfg = $.extend(cfg, config); props = $.extend({}, initial); movediv.css(initial) horizontal = cfg.direction == "left" || cfg.direction == "right"; usecss = horizontal ? "width": "height"; movediv.css(usecss, 99999); if( horizontal ){ panels.css("float", "left"); }else{ movediv.css("width", "auto"); panels.css("float", "none"); } css = horizontal ? "left" : "top"; add = cfg.direction == "bottom" || cfg.direction == 'right'; //单个的尺寸 var outer = panels["outer" + usecss.charat(0).touppercase() + usecss.slice(1)](true); containersize = container[usecss](); if( cfg.boxsize == "auto" ) { singlesize = math.max(outer, containersize); length = math.ceil(outer*length / singlesize); }else { singlesize = cfg.boxsize == "fix" ? outer : cfg.boxsize; } minposition = math.min(-singlesize * length + containersize, 0); lastindex = math.ceil( math.abs( minposition / singlesize ) ); clearinterval(timer); if( cfg.autoplay ) timer = setinterval(auto, cfg.interval); if( btns[curindex] ) btns.eq(curindex).addclass(cfg.btnactivecls); panels.eq(curindex).addclass(cfg.paneactivecls); } reset(); var pos = 0; function move(index){ if( btns[curindex] ) btns.eq(curindex).removeclass(cfg.btnactivecls); if( btns[index] ) btns.eq(index).addclass(cfg.btnactivecls); panels.eq(curindex).removeclass(cfg.paneactivecls); panels.eq(index).addclass(cfg.paneactivecls); curindex = index; result.index = index; cfg.onchange && cfg.onchange(index); pos = -index * singlesize; pos += alignleft; //对齐 pos -= cfg.focus == "left" ? 0 : cfg.focus == "center" ? (cfg.count*singlesize - containersize)/2 : cfg.count*singlesize - containersize; pos = pos < minposition ? minposition : math.min(pos, 0); props[css] = initial[css] + pos; movediv.stop().animate(props, null, cfg.easing, cfg.duration); } function movenext(dir){ var index; if( add && dir == 1 ){ if(curindex == lastindex || ( pos == minposition)) index = 0; else index = math.min(curindex + cfg.count, length - 1); }else { if(curindex == 0 || ( pos == 0)) index = lastindex; else index = math.max(curindex - cfg.count, 0); } move(index); }; function auto(){ var index; if( cfg.random ){ if(length > 1){ do{ index = math.floor(math.random() * length); }while(index == curindex) move(index); } } else{ movenext(1); } } btns.size() && btns.mouseover( function(){move(btns.index(this));} ); $(cfg.container || container).hover( function(){ cleartimeout(timer); }, function(){ if(cfg.autoplay) timer = setinterval(auto, cfg.interval); } ); $.each({"prev": -1, "next": 1}, function(k, v){ var arrow = $(cfg[k + "sel"], context); if(!arrow.size()) return; arrow.click(function(){ movenext(v); }) }); return result; }, /** @parm direction 0: 上下左右 1: 上下 2: 左右 */ hit: function (elem, direction, count, distance){ if(elem.length && elem[0]){ var i = 0; for(;i < elem.length; ++i) $.w.hit( elem[i], direction, count, distance ); return; } var a, b, c, t = 0, orig = {left: $.css(elem, "left", true), top: $.css(elem, "top", true)} ; if(!direction){ a = ['top', 'left']; b = 4; c = 2; } else if(direction == 1){ a = ['top']; b = 2; c = 1; } else{ a = ['left']; b = 2; c = 1; } count = count || 7; distance = distance || 3; if($.css(elem, "position") == 'static'){ $.style( elem, "position", 'relative'); } var tm = setinterval(function(){ $.style(elem, a[ t%c ], orig[ a[ t%c ] ] + ( t++%b < c ? -distance : distance) ); if(t > count){ clearinterval(tm); $(elem).css(orig); } }, 24) }, zoom: function(config){ var cfg = $.extend({ imgnodes: "img", //图片节点或jquery对象 bigimags: "rel", //大图路径 可以是数组, 默认读取原图rel和src属性 //指示器模版 recttpl: $("
").css({opacity: .2, border: "1px solid #00f", background: "#000"}), //大图模版 如果包含子div可以用data-attach-point标识容器 boxtpl: $("
"), width: 300, height: 300, /**大图显示方式 1. 1 ~ n 按倍数放大 2. [width, height]固定大小 */ method: 1, diff: [5, 0] //box和img之间间隔 }, config); var body = document.body, win = $(window), doc = $(document), imgnodes = $(cfg.imgnodes), rect = $(cfg.recttpl).css({zindex: 9999, position: "absolute"}).appendto(body).hide(), box = $(cfg.boxtpl).css({zindex: 9999, position: "absolute"}).width(cfg.width).height(cfg.height).appendto(body).hide(), boxcontainer = ( boxcontainer = box.find("[data-attach-point='container']") ) && boxcontainer.length && boxcontainer || box, img = $("").appendto(boxcontainer).css("position", "absolute"), offset, target, contentbox = {left: 0, top: 0, right: 0, bottom: 0}, x, y; if( boxcontainer.css("position") == "static" ) boxcontainer.css({position: "relative"}); boxcontainer.css({overflow: "hidden"}); if( $.isarray(cfg.method) ) img.width(cfg.method[0]).height(cfg.method[1]); function mouseover(e){ target = $(this); offset = target.offset(); rect.show(); box.show(); x = e.pagex; y = e.pagey; _resetcontentbox(); var src = $.isarray(cfg.bigimags) ? cfg.bigimags[ imgnodes.index(this) ] : $(this).attr(cfg.bigimags) || $(this).attr("src"); img[0].src = src; box.css(_getboxposition(offset, target.outerwidth(), target.outerheight())); $.w.loadimage(src, function(w, h){ if( !isnan(cfg.method) && !isnan(w) ){ w = w * cfg.method; h = h * cfg.method; img.width(w).height(h); var rw = math.min( cfg.width/w, 1 ) * target.width(); var rh = math.min( cfg.height/h, 1 ) * target.height(); rect.outerwidth( rw ).outerheight( rh ); _resetrectposition( rw, rh ); } }) doc.on("mousemove", mousemove); } function mousemove(e){ x = e.pagex; y = e.pagey; if( x < contentbox.left || x > contentbox.right || y < contentbox.top || y > contentbox.bottom){ mouseout(); } _resetrectposition( rect.outerwidth(), rect.outerheight() ); } function mouseout(e){ doc.off("mousemove", mousemove); box.hide(); rect.hide(); } function add(imgnodes){ imgnodes.mouseover(mouseover); } add(imgnodes); return { reset: function(){ if(target){ offset = target.offset(); _resetrectposition(); _resetcontentbox(); _getboxposition(); } } }; function _resetcontentbox(){ if(target){ contentbox.width = target.width(); contentbox.height = target.height(); contentbox.left = offset.left + $.css(target[0], "borderleftwidth", true) + $.css(target[0], "paddingleft", true); contentbox.top = offset.top + $.css(target[0], "bordertopwidth", true) + $.css(target[0], "paddingtop", true); contentbox.right = contentbox.left + contentbox.width; contentbox.bottom = contentbox.top + contentbox.height; } } function _resetrectposition(w, h){ var left = x - w/2, top = y - h/2; left = math.min(left, contentbox.right - w); left = math.max(left, contentbox.left); top = math.min(top, contentbox.bottom - h); top = math.max(top, contentbox.top); rect.css({left: left, top: top}); w = img.width(); h = img.height() left = (left - offset.left) / contentbox.width * w ; top = (top - offset.top) / contentbox.height * h; left = math.min(left, w - cfg.width); left = math.max(left, 0); top = math.min(top, h - cfg.height); top = math.max(top, 0); img.css({left: -left, top: -top}); } function _getboxposition(offset, w, h){ var scroll = {left: win.scrollleft(), top: win.scrolltop()}, width = box.outerwidth(), height = box.outerheight(), left = offset.left + w + cfg.diff[0], top = offset.top + cfg.diff[1], maxtop = scroll.top + win.height(), maxleft = scroll.left + win.width(); if( top + height > maxtop ){ top = maxtop - height; top = math.max(top, scroll.top); } if( left + width > maxleft ){ var l = offset.left - width - cfg.diff[0]; if( l > scroll.left || scroll.left - l < maxleft - left ){ left = l; } } return {top: top, left: left}; } }, calendar: function(cfg){ cfg = $.extend({ width: "", height: "", target: "", begin: "1950-1-2", end: 2030, onselect: null, showothermonth: true, weekendday: 0 }, cfg); var domnode = cfg.target ? $(cfg.target) : $("
"), table = $("").css({width: "100%"}).appendto(domnode), thead = $("").appendto(table), tbody = $("").appendto(table), tfoot = $("").appendto(table), daynames = ["日","一","二","三","四","五","六"], leapyear = function(y){ return ((y%4==0 && y%100!=0) || y%400==0) ? 1 : 0; }, getmonthdayscount = function(y){ return [31, 28 + leapyear(y),31,30,31,30,31,31,30,31,30,31]; }, saturday = 6 - cfg.weekendday, sunday = (7 - cfg.weekendday >= 7) ? 0 : (7 - cfg.weekendday), i = 0, j, tr, td, s, rowcount = 6 nowdate = new date(); domnode.css({width: cfg.width, height: cfg.height}).addclass("w-calendar"); //头部 tr = $("").appendto(thead); while(i < daynames.length){ s = (i == saturday || i == sunday) ? " class='w-calendar-weekend'" : "" tr.append(""); ++i; } function select(date){ var onow = new date(date), nowy = onow.getfullyear(), nowm = onow.getmonth(), nowd = onow.getdate(), odate = new date(date), y = odate.getfullyear(), m = odate.getmonth(), d = odate.getdate(), dayscount = getmonthdayscount(y)[m], olastmonth = new date(y, m - 1), lastmonthdayscount = getmonthdayscount( olastmonth.getfullyear() )[olastmonth.getmonth()], firstday = (new date(y, m)).getday(); tbody.empty(); i = 0; j = firstday; tr = $("").appendto(tbody); //上个月 while( j-- ){ td = $("").appendto(tbody); td = $("").appendto(tbody); td = $("
" + daynames[i] + "
").appendto(tr).addclass("w-calendar-othermonth"); if(cfg.showothermonth) td.html( lastmonthdayscount - j ); } //这个月 j = firstday; var counter = 1; for(; i < rowcount; ){ for(; j < 7; ){ if( j == 0 ) tr = $("
").appendto(tr); if( counter == d) td.addclass("w-calendar-selected"); td.html( counter++ ); ++j; if( counter > dayscount )break; } if( counter > dayscount )break; j = 0; ++i; } //下个月 counter = 1; while( i++ < rowcount ){ for(; j < 7; ++j){ if( j == 0 ) tr = $("
").appendto(tr).addclass("w-calendar-othermonth"); if(cfg.showothermonth) td.html( counter++ ); } j = 0; } } select("2013,7,5") }, datepicker: function(cfg){ cfg = $.extend({ date: "", //默认日期 begin: "1950-1-2", end: 2030, input: null, disabled: false, position: "", format: "y-m-d h:i:s", showtime: true || false, week: 0 }, cfg); if( !cfg.input )return false; var input = $(cfg.input), date = "" + (cfg.date || input.val()), odate; if( input.prop("disabled") ) cfg.disabled = true; if( date.length == 4 ) odate = new date(date, 0, 1); else odate = new date(date); var html = "
" + ""+ "
", domnode = $(html); input.addclass("w-datepicker-input").before(domnode); var hover = function(){$(this).toggleclass("w-datepicker-btns-hover")}; var btns = domnode.find(".w-datepicker-border").prepend(input).find(".w-datepicker-btns"); if( !cfg.disabled ) btns.hover(hover); return {disable: function(b){ input.prop("disabled", b); if( cfg.disabled == b )return; if( b ) btns.bind(hover); else btns.unbind(hover); cfg.disabled = b; }} }, /** * 和php一样的时间戳格式化函数 * @param {string} format 格式 * @param {int} timestamp 要格式化的时间 默认为当前时间 * @return {string} 格式化的时间字符串 */ dateformat: function (format, timestamp){ var a, jsdate=((timestamp) ? new date(timestamp) : new date()); var pad = function(n, c){ if((n = n + "").length < c){ return new array(++c - n.length).join("0") + n; } else { return n; } }; var txt_weekdays = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]; var txt_ordin = {1:"st", 2:"nd", 3:"rd", 21:"st", 22:"nd", 23:"rd", 31:"st"}; var txt_months = ["", "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]; var f = { // day d: function(){return pad(f.j(), 2)}, d: function(){return f.l().substr(0,3)}, j: function(){return jsdate.getdate()}, l: function(){return txt_weekdays[f.w()]}, n: function(){return f.w() + 1}, s: function(){return txt_ordin[f.j()] ? txt_ordin[f.j()] : 'th'}, w: function(){return jsdate.getday()}, z: function(){return (jsdate - new date(jsdate.getfullyear() + "/1/1")) / 864e5 >> 0}, // week w: function(){ var a = f.z(), b = 364 + f.l() - a; var nd2, nd = (new date(jsdate.getfullyear() + "/1/1").getday() || 7) - 1; if(b <= 2 && ((jsdate.getday() || 7) - 1) <= 2 - b){ return 1; } else{ if(a <= 2 && nd >= 4 && a >= (6 - nd)){ nd2 = new date(jsdate.getfullyear() - 1 + "/12/31"); return date("w", math.round(nd2.gettime()/1000)); } else{ return (1 + (nd <= 3 ? ((a + nd) / 7) : (a - (7 - nd)) / 7) >> 0); } } }, // month f: function(){return txt_months[f.n()]}, m: function(){return pad(f.n(), 2)}, m: function(){return f.f().substr(0,3)}, n: function(){return jsdate.getmonth() + 1}, t: function(){ var n; if( (n = jsdate.getmonth() + 1) == 2 ){ return 28 + f.l(); } else{ if( n & 1 && n < 8 || !(n & 1) && n > 7 ){ return 31; } else{ return 30; } } }, // year l: function(){var y = f.y();return (!(y & 3) && (y % 1e2 || !(y % 4e2))) ? 1 : 0}, //o not supported yet y: function(){return jsdate.getfullyear()}, y: function(){return (jsdate.getfullyear() + "").slice(2)}, // time a: function(){return jsdate.gethours() > 11 ? "pm" : "am"}, a: function(){return f.a().touppercase()}, b: function(){ // peter paul koch: var off = (jsdate.gettimezoneoffset() + 60)*60; var theseconds = (jsdate.gethours() * 3600) + (jsdate.getminutes() * 60) + jsdate.getseconds() + off; var beat = math.floor(theseconds/86.4); if (beat > 1000) beat -= 1000; if (beat < 0) beat += 1000; if ((string(beat)).length == 1) beat = "00"+beat; if ((string(beat)).length == 2) beat = "0"+beat; return beat; }, g: function(){return jsdate.gethours() % 12 || 12}, g: function(){return jsdate.gethours()}, h: function(){return pad(f.g(), 2)}, h: function(){return pad(jsdate.gethours(), 2)}, i: function(){return pad(jsdate.getminutes(), 2)}, s: function(){return pad(jsdate.getseconds(), 2)}, //u not supported yet // timezone //e not supported yet //i not supported yet o: function(){ var t = pad(math.abs(jsdate.gettimezoneoffset()/60*100), 4); if (jsdate.gettimezoneoffset() > 0) t = "-" + t; else t = "+" + t; return t; }, p: function(){var o = f.o();return (o.substr(0, 3) + ":" + o.substr(3, 2))}, //t not supported yet //z not supported yet // full date/time c: function(){return f.y() + "-" + f.m() + "-" + f.d() + "t" + f.h() + ":" + f.i() + ":" + f.s() + f.p()}, //r not supported yet u: function(){return math.round(jsdate.gettime()/1000)} }; return format.replace(/[\\]?([a-za-z])/g, function(t, s){ if( t!=s ){ // escaped ret = s; } else if( f[s] ){ // a date function exists ret = f[s](); } else{ // nothing special ret = s; } return ret; }); }, dateadd: function(interval, number, date) { var d = new date(date); var k={'y':'fullyear', 'q':'month', 'm':'month', 'w':'date', 'd':'date', 'h':'hours', 'n':'minutes', 's':'seconds', 'ms':'milliseconds'}; var n={'q':3, 'w':7}; eval('d.set'+k[interval]+'(d.get'+k[interval]+'()+'+((n[interval]||1)*number)+')'); return d; }, datediff: function(interval, date1, date2){ date1 = new date(date1); date2 = new date(date2); var i = {}, t = date1.gettime(), t2 = date2.gettime(); i['y'] = date2.getfullyear() - date1.getfullyear(); i['q'] = i['y']*4 + math.floor(date2.getmonth()/4) - math.floor(date1.getmonth()/4); i['m'] = i['y']*12 + date2.getmonth() - date1.getmonth(); i['ms'] = date2.gettime() - date1.gettime(); i['w'] = math.floor((t2+345600000)/(604800000))-math.floor((t+345600000)/(604800000)); i['d'] = math.floor(t2/86400000)-math.floor(t/86400000); i['h'] = math.floor(t2/3600000)-math.floor(t/3600000); i['n'] = math.floor(t2/60000)-math.floor(t/60000); i['s'] = math.floor(t2/1000)-math.floor(t/1000); return i[interval]; }, grid: function(cfg){ cfg = $.extend({ domnode: null, columns: null, subrows: null, data: null },cfg) var domnode = $(cfg.domnode || "
"), table = $("").css({ bordercollapse: "collapse", tablelayout: "fixed" }).appendto(domnode), rowindex, rowcount, label, formatter, field, i,j,tr,td,columns,fields,rowspan,colspan; if( cfg.subrows ){ $.each(cfg.data, function(key, data){ for(i = 0; i < cfg.subrows.length; ++i){ tr = $("").appendto(table); columns = cfg.subrows[i]; for(j = 0; j < columns.length; ++j) { fields = columns[j]; rowspan = colspan = 0; if( $.isplainobject(fields) ) { field = fields['field']; label = fields['label'] == undefined ? field : fields['label']; formatter = fields['formatter']; colspan = fields['colspan']; rowspan = fields['rowspan']; }else if( $.isarray(fields) ) { field = fields[0]; label = fields[1] == undefined ? field : fields[1]; formatter = fields[2]; }else{ field = fields; label = field; formatter = null; } td = $("
").appendto(tr).addclass("field-"+field); if( rowspan )td.attr("rowspan", rowspan); if( colspan )td.attr("colspan", colspan); if( formatter ) { td.html( formatter(data[field], data, td) ); }else td.html( data[field] ); } } }); } return domnode; } }); function _fixcontainer(container){ container = $(container); //获取高度 因为可能div未先设置高度 var height = container.height(); var cur = container; while(height == 0 && (cur = cur.parent())){ height = cur.height(); } while( container[0].nodename.tolowercase() in {"td": 1, "th": 1, "tr": 1} && container[0].parentnode){ container = container.parent(); } if( container.css("position") == 'static' ){ container.css("position", 'relative'); } container.height(height).css("overflow", "hidden"); } })(jquery);