1 /** 2 * 3 * dom 4 * @fileOverView core/dom 5 * @author <a href="mailto:zhang.gd@foxmail.com">Zhang Guangda</a> 6 * @date 2012-10-25 7 */ 8 define("core/dom", function() { 9 /** 10 * @class we dom方法 11 */ 12 $we.dom = { 13 /** 14 * 获取当前事件的event对象 15 * @return {event} event对象 16 */ 17 getEvent: function() { 18 if (typeof event != "undefined") 19 return event; 20 21 var func = $we.dom.getEvent.caller; 22 while(func != null) { 23 var e = func.arguments[0]; 24 if(e && (e + "").indexOf("Event") >= 0) return e; 25 func = func.caller; 26 } 27 return null; 28 }, 29 30 /** 31 * 获取当前事件的触发对象 32 * @param {event} e event对象,可不传 33 * @return {dom} dom 对象 34 */ 35 getTarget: function(e) { 36 e = e || this.getEvent(); 37 return e.srcElement || e.target; 38 }, 39 40 /** 41 * 屏幕滑动至某位置 42 * @param {dom} to 滑动到的对象 43 * @param {integer} time 时间 44 * @param {Function} cb 回调函数 45 */ 46 scrollTo: function(to, time, cb) { 47 if (!to) return; 48 49 if (typeof to == "number" || typeof to == "string") to = to; 50 else if (typeof to == "object") 51 to = $(to).offset().top; 52 53 if (!to) return; 54 55 var $body = (window.opera) ? (document.compatMode == "CSS1Compat" ? $('html') : $('body')) : $('html,body'); 56 time = time || 300; 57 cb = cb || $we.emptyFunction; 58 59 $($body).animate({scrollTop: to}, time, cb); 60 }, 61 62 /** 63 * 判断图片是否已经加载完成,完成后执行回调 64 * @param {dom} el 元素 65 * @param {Function} cb 回调 66 */ 67 imageLoaded: function(el, cb) { 68 if (arguments.length < 2 && typeof arguments[0] === "function") { 69 cb = el; 70 el = $("body"); 71 } 72 73 cb = cb || $we.emptyFunction; 74 75 var imgs = $(el).find("img"), 76 total = imgs.length, 77 cur = 0; 78 79 if (!total) cb(); 80 81 var judgeAllLoaded = function() { 82 if (++cur >= total - 1) cb(); 83 }; 84 85 imgs.each(function(index) { 86 if (this.complete || this.readyStatus === "complete") { 87 judgeAllLoaded(); 88 } else { 89 $(this).on("load", judgeAllLoaded); 90 } 91 }); 92 } 93 }; 94 95 return $we.dom; 96 });