动态图片乱布局--初步定型
1 function luan(luand,limgevent,loadimgevent,tagdata) { 2 var windowW = $(window).width(); 3 var windowH = $(window).height(); 4 var gld; 5 6 function init(d){ 7 gld = { 8 boxid: d == undefined || d == null ? "body" : d.id == undefined || d.id == null ? "body" : d.id, 9 type: d == undefined || d == null ? "cect" : d.type == undefined || d.type == null ? "cect" : d.type, 10 count: d == undefined || d == null ? 4 : typeof d.count == "number" ? d.count : 5, 11 height: d == undefined || d == null ? windowH : d.height == undefined || d.height == null ? windowH : d.height, 12 width: d == undefined || d == null ? windowW : d.width == undefined || d.width == null ? windowW : d.width, 13 inlinewidth: d == undefined || d == null ? Math.floor(windowW / 4) : d.inlinewidth == undefined || d.inlinewidth == null ? Math.floor(windowW / 4) : d.inlinewidth, 14 inlineheight: d == undefined || d == null ? 300 : d.inlinewidth == undefined || d.inlinewidth == null ? 300 : d.inlineheight, 15 scale: d == undefined || d == null ? 1 : d.scale == undefined || d.scale == null ? 1 : d.scale, 16 outercls: d == undefined || d == null ? null : d.outercls == undefined ? null : d.outercls, 17 outercss: d == undefined || d == null ? null : d.outercss == undefined ? null : d.outercss, 18 inner: d == undefined || d == null ? "div" : d.inner == undefined || d.inner == null ? "div" : d.inner, 19 innercls: d == undefined || d == null ? null : d.innercls == undefined ? null : d.innercls, 20 innercss: d == undefined || d == null ? null : d.innercss == undefined ? null : d.innercss, 21 imgcls: d == undefined || d == null ? null : d.imgcls == undefined ? null : d.imgcls, 22 imgcss: d == undefined || d == null ? null : d.imgcss == undefined ? null : d.imgcss, 23 errosrc: d == undefined || d == null ? null : d.errosrc == undefined ? null : d.errosrc, 24 dely: d == undefined || d == null ? "css" : d.dely == undefined || d.dely == null ? "css" : d.dely, 25 //sort: d == undefined || d == null ? "quence" : d.sort == undefined || d.sort == null ? "quence" : d.sort, 26 tag: d == undefined || d == null ? null : d.tag == undefined ? null : d.tag, 27 28 } 29 } 30 var glimg; 31 function initglimg() { 32 var imgd = { 33 //id: (typeof d != "object") ? null : (typeof d.id == "string" ? d.id : null), 34 css: gld.imgcss,//css 35 cls: gld.imgcls,//className 36 scale: gld.scale, 37 outer: gld.inner, 38 outercss: gld.innercss, 39 outercls: gld.innercls, 40 errosrc: gld.errosrc, 41 42 } 43 glimg = new limg(imgd); 44 } 45 init(luand); 46 initglimg(); 47 luan.prototype.sort = function (ar) { 48 49 //集合 50 if (ar instanceof Array) { 51 //sortarray(); 52 function sortarray() { 53 function createDiv() { 54 var v = document.createElement("div"); 55 $(v).attr("luan-be-out-box", gld.type); 56 if (gld.outercss != null) { $(v).css(gld.outercss); } 57 if (gld.outercls != null) { $(v).attr("class", gld.outercls) } 58 $(v).appendTo($("#" + gld.boxid)); 59 return v; 60 61 } 62 function createDivs(c) { 63 var aray = new Array(c); 64 for (var i = 0; i < c; i++) { 65 aray[i] = createDiv(); 66 } 67 return aray; 68 } 69 var boxes = createDivs(gld.count); 70 71 var imgloadEvent = { 72 load: function (e, n, b, h, w, tag) { 73 $(b).appendTo(tag.element); 74 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.load !=undefined && loadimgevent.load!=null) { 75 loadimgevent.load(e, n,b, h, w, tag); 76 } 77 }, 78 complete: function (e) { 79 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.complete != undefined && loadimgevent.complete != null) { 80 loadimgevent.complete(e); 81 } 82 }, 83 error: function (e) { 84 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.error != undefined && loadimgevent.error != null) { 85 loadimgevent.error(e); 86 } 87 }, 88 readystatechange: function (e) { 89 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.readystatechange != undefined && loadimgevent.readystatechange != null) { 90 loadimgevent.readystatechange(e); 91 } 92 } 93 } 94 for (var i = 0; i < ar.length; i++) { 95 var tag = { 96 element:boxes[i%(gld.count+1)], 97 index:i, 98 tagdata:tagdata, 99 } 100 glimg.load(ar[i], limgevent, imgloadEvent,tag); 101 } 102 } 103 104 //type=="align-height" 105 function sortTypeAlignHeight() { 106 107 //创建所需div 108 function createDiv() { 109 var v = document.createElement("div"); 110 $(v).attr("luan-be-out-box", gld.type);//栓选标志 111 if (gld.outercss != null) { $(v).css(gld.outercss); } 112 if (gld.outercls != null) { $(v).attr("class", gld.outercls) } 113 $(v).css({ "width": "100%" }); 114 //添加到外容器中 115 116 $(v).appendTo($("#" + gld.boxid)); 117 118 return v; 119 120 } 121 var box; 122 box = createDiv(); 123 var imgloadEvent = { 124 load: function (e, n, b, h, w, tag) { 125 var allwidth = 0; 126 var v = $("#" + gld.boxid).find("div[luan-be-out-box=" + gld.type + "]"); 127 var box = $(v).eq(v.length - 1); 128 //$(box).attr("luan-limg-be-box", "outer"); 129 $(box).find(gld.inner + "[luan-limg-be-box=outer]").each(function () { allwidth += $(this).width() }); 130 allwidth += $(b).width(); 131 if (allwidth > $(box).width()) { 132 box = createDiv(); 133 } 134 $(b).appendTo(box); 135 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.load != undefined && loadimgevent.load != null) { 136 loadimgevent.load(e, n, b, h, w, tag); 137 } 138 }, 139 complete: function (e) { 140 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.complete != undefined && loadimgevent.complete != null) { 141 loadimgevent.complete(e); 142 } 143 }, 144 error: function (e) { 145 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.error != undefined && loadimgevent.error != null) { 146 loadimgevent.error(e); 147 } 148 }, 149 readystatechange: function (e) { 150 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.readystatechange != undefined && loadimgevent.readystatechange != null) { 151 loadimgevent.readystatechange(e); 152 } 153 } 154 } 155 for (var i = 0; i < ar.length; i++) { 156 //var v = $("#"+gld.boxid).find("div[luan-be-out-box="+gld.type+"]"); 157 //var tag = { element:$(v).eq(v.length-1), index: i }; 158 glimg.load(ar[i], limgevent, imgloadEvent); 159 } 160 } 161 162 //实现 163 if (gld.type == "align-width") { sortarray(); } else if (gld.type == "align-height") { sortTypeAlignHeight(); } 164 165 166 } 167 168 } 169 170 luan.prototype.add = function (obj) { 171 var box; 172 var imgloadEvent; 173 174 if (gld.type == "align-height") { 175 imgloadEvent = { 176 load: function (e, n, b, h, w, tag) { 177 var allwidth = 0; 178 var v = $("#" + gld.boxid).find("div[luan-be-out-box=" + gld.type + "]"); 179 var box = $(v).eq(v.length - 1); 180 //$(box).attr("luan-limg-be-box", "outer"); 181 $(box).find(gld.inner + "[luan-limg-be-box=outer]").each(function () { allwidth += $(this).width() }); 182 allwidth += $(b).width(); 183 if (allwidth > $(box).width()) { 184 box = createDiv(); 185 } 186 $(b).appendTo(box); 187 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.load != undefined && loadimgevent.load != null) { 188 loadimgevent.load(e, n, b, h, w, tag); 189 } 190 }, 191 complete: function (e) { 192 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.complete != undefined && loadimgevent.complete != null) { 193 loadimgevent.complete(e); 194 } 195 }, 196 error: function (e) { 197 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.error != undefined && loadimgevent.error != null) { 198 loadimgevent.error(e); 199 } 200 }, 201 readystatechange: function (e) { 202 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.readystatechange != undefined && loadimgevent.readystatechange != null) { 203 loadimgevent.readystatechange(e); 204 } 205 } 206 } 207 var v = $("#" + gld.type).find("div[luan-be-out-box=align-height]"); 208 box = $(v).eq(v.length - 1); 209 //var tag = { element: box, index: length }; 210 glimg.load(obj, limgevent, imgloadEvent); 211 function createDiv() { 212 var v = document.createElement("div"); 213 $(v).attr("luan-be-out-box", gld.type);//栓选标志 214 if (gld.outercss != null) { $(v).css(gld.outercss); } 215 if (gld.outercls != null) { $(v).attr("class", gld.outercls) } 216 $(v).css({ "width": "100%" }); 217 //添加到外容器中 218 219 $(v).appendTo($("#" + gld.boxid)); 220 221 return v; 222 223 } 224 } else if (gld.type == "align-width") { 225 imgloadEvent = { 226 load: function (e, n, b, h, w, tag) { 227 $(b).appendTo(tag.element); 228 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.load != undefined && loadimgevent.load != null) { 229 loadimgevent.load(e, n, b, h, w, tag); 230 } 231 }, 232 complete: function (e) { 233 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.complete != undefined && loadimgevent.complete != null) { 234 loadimgevent.complete(e); 235 } 236 }, 237 error: function (e) { 238 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.error != undefined && loadimgevent.error != null) { 239 loadimgevent.error(e); 240 } 241 }, 242 readystatechange: function (e) { 243 if (loadimgevent != undefined && loadimgevent != null && loadimgevent.readystatechange != undefined && loadimgevent.readystatechange != null) { 244 loadimgevent.readystatechange(e); 245 } 246 } 247 } 248 var v = $("#" + gld.boxid).find("div[luan-be-out-box=align-width]"); 249 var length = v[0].children.length; 250 box = v[0]; 251 for (var i = 0; i < v.length; i++) { 252 var vl = v[i].children.length; 253 //length = this.children.length; 254 if (length > vl) { 255 length = vl; 256 box = v[i]; 257 break; 258 } 259 } 260 var tag = { element: box, index: length,tagdata:tagdata }; 261 glimg.load(obj, limgevent, imgloadEvent, tag); 262 } 263 264 265 } 266 267 268 luan.prototype.img = function (d) { 269 return new limg(d); 270 } 271 function limg(d) { 272 var gd; 273 function init(d) { 274 var initgd = { 275 //id: (typeof d != "object") ? null : (typeof d.id == "string" ? d.id : null), 276 css: (d == undefined || d == null) ? null : (d.css == undefined || d.css == null ? null : d.css),//css 277 cls: (d == undefined || d == null) ? null : (d.cls == undefined || d.cls == null ? null : d.cls),//className 278 scale: (d == undefined || d == null) ? 1 : (typeof d.scale == "number" ? d.scale : 1), 279 outer: (d == undefined || d == null) ? "div" : (typeof d.out == "string" ? d.out : "div"), 280 outercss: (d == undefined || d == null) ? null : (d.outercss == undefined || d.outercss == null ? null : d.outercss), 281 outercls: (d == undefined || d == null) ? null : (d.outercls == undefined || d.outercls == null ? null : d.outercls), 282 errosrc: (d == undefined || d == null) ? null : (d.errosrc == undefined || d.errosrc == null ? null : d.errosrc), 283 284 } 285 gd = initgd; 286 } 287 init(d); 288 var baseHeight; 289 var baseWidth; 290 limg.prototype.baseHeight = function () { 291 return baseHeight; 292 } 293 limg.prototype.baseWidth = function () { 294 return baseWidth; 295 } 296 limg.prototype.load = function (d, event, loadevent,tag) { 297 //事件装饰器 298 function eventBind(n, b) { 299 var e = { 300 name: event == undefined || event == null || event.name == undefined ? "inner" : event.name, 301 mouseover: event == undefined || event == null || event.mouseover == undefined ? null : event.mouseover, 302 mousemove: event == undefined || event == null || event.mousemove == undefined ? null : event.mousemove, 303 mouseout: event == undefined || event == null || event.mouseout == undefined ? null : event.mouseout, 304 click: event == undefined || event == null || event.click == undefined ? null : event.click, 305 } 306 307 if (e.name == "inner") { 308 if (e.mouseover != null) { n.onmouseover = function (eve) { e.mouseover(eve, n) } } 309 if (e.mouseout != null) { n.onmouseout = function (eve) { e.mouseout(eve, n) } } 310 if (e.mousemove != null) { n.onmousemove = function (eve) { e.mousemove(eve, n) } } 311 if (e.click != null) { n.onclick = function (eve) { e.click(eve, n) } } 312 } else { 313 if (e.mouseover != null) { b.onmouseover = function (eve) { e.mouseover(eve, b) } } 314 if (e.mouseout != null) { b.onmouseout = function (eve) { e.mouseout(eve, b) } } 315 if (e.mousemove != null) { b.onmousemove = function (eve) { e.mousemove(eve, b) } } 316 if (e.click != null) { b.onclick = function (eve) { e.click(eve, b) } } 317 } 318 } 319 320 function load_src() { 321 //加载框 322 var box = document.createElement(gd.outer); 323 324 if (gd.outercls != null) { $(box).attr("class", gd.outercls) } 325 if (gd.outercss != null) { $(box).css(gd.outercss) } 326 //加载内容 327 var nr = new Image(); 328 nr.onload = function () { 329 baseHeight = $(nr).height(); 330 baseWidth = $(nr).width(); 331 332 } 333 $(nr).attr("src", d); 334 if (gd.css != null) { $(nr).css(gd.css) } 335 if (gd.cls != null) { $(nr).attr("class", gd.cls) } 336 //装饰 --事件装饰 337 eventBind(nr, box); 338 //luan 标志装饰 339 $(box).attr("luan-limg-be-box", "outer"); 340 $(nr).attr("luan-limg-be-img", "inner"); 341 //打包 342 $(nr).appendTo(box); 343 return box; 344 } 345 function load_src_loadevent() { 346 //加载框 347 var box = document.createElement(gd.outer); 348 if (gd.outercls != null) { $(box).attr("class", gd.outercls) } 349 if (gd.outercss != null) { $(box).css(gd.outercss) } 350 //加载内容 351 var nr = new Image(); 352 nr.onload = function (e) { 353 baseHeight = $(nr).height(); 354 baseWidth = $(nr).width(); 355 if (gd.css != null) { $(nr).css(gd.css) } 356 if (gd.cls != null) { $(nr).attr("class", gd.cls) } 357 if (loadevent.load != undefined && loadevent.load != null) { loadevent.load(e, nr,box,baseHeight,baseWidth,tag) }; 358 } 359 //事件装饰 img加载 360 nr.onreadystatechange = function (e) { if (loadevent.readystatechange!=undefined && loadevent.readystatechange != null) { loadevent.readystatechange(e, nr); } } 361 nr.onerror = function (e) { if (loadevent.error!=undefined && loadevent.error != null) { loadevent.error(e, nr); } } 362 nr.complete = loadevent.complete==undefined || loadevent.complete == null ? null : loadevent.complete; 363 364 $(nr).attr("src", d); 365 366 //装饰 --事件装饰 367 eventBind(nr, box); 368 369 //luan 标志装饰 370 $(box).attr("luan-limg-be-box", "outer"); 371 $(nr).attr("luan-limg-be-img", "inner"); 372 //打包 373 $(nr).appendTo(box); 374 return box; 375 376 } 377 function load(loadmd) { 378 //加载框 379 var box = document.createElement(loadmd.outer); 380 if (loadmd.outercls != null) { $(box).attr("class", loadmd.outercls) } 381 if (loadmd.outercss != null) { $(box).css(loadmd.outercss) } 382 //加载内容 383 var nr = new Image(); 384 nr.onload = function () { 385 baseHeight = $(nr).height(); 386 baseWidth = $(nr).width(); 387 388 } 389 $(nr).attr("src", loadmd.src); 390 $(nr).attr("alt", loadmd.alt) 391 if (loadmd.id != null) { $(nr).attr("id", loadmd.id); } 392 393 if (loadmd.css != null) { $(nr).css(loadmd.css) } 394 if (loadmd.cls != null) { $(nr).attr("class", loadmd.cls) } 395 //装饰 --事件装饰 396 eventBind(nr, box); 397 398 //luan 标志装饰 399 $(box).attr("luan-limg-be-box", "outer"); 400 $(nr).attr("luan-limg-be-img", "inner"); 401 //打包 402 $(nr).appendTo(box); 403 return box; 404 } 405 function load_loadevent(loadmd) { 406 //加载框 407 var box = document.createElement(loadmd.outer); 408 if (loadmd.outercls != null) { $(box).attr("class", loadmd.outercls) } 409 if (loadmd.outercss != null) { $(box).css(loadmd.outercss) } 410 //加载内容 411 var nr = new Image(); 412 nr.onload = function (e) { 413 baseHeight = $(nr).height(); 414 baseWidth = $(nr).width(); 415 416 if (loadmd.id != null) { $(nr).attr("id", loadmd.id); } 417 418 if (loadmd.css != null) { $(nr).css(loadmd.css) } 419 if (loadmd.cls != null) { $(nr).attr("class", loadmd.cls) } 420 421 if (loadevent.load != undefined && loadevent.load != null) { loadevent.load(e, nr, box, baseHeight, baseWidth,tag) }; 422 } 423 //事件装饰 img加载 424 nr.onreadystatechange = function (e) { if (loadevent.readystatechange != undefined && loadevent.readystatechange != null) { loadevent.readystatechange(e, nr); } } 425 nr.onerror = function (e) { if (loadevent.error != undefined && loadevent.error != null) { loadevent.error(e, nr); } } 426 nr.complete = loadevent.complete == undefined || loadevent.complete == null ? null : loadevent.complete; 427 428 $(nr).attr("src", loadmd.src); 429 $(nr).attr("alt", loadmd.alt) 430 //装饰 --事件装饰 431 eventBind(nr, box); 432 433 //luan 标志装饰 434 $(box).attr("luan-limg-be-box", "outer"); 435 $(nr).attr("luan-limg-be-img", "inner"); 436 //打包 437 $(nr).appendTo(box); 438 return box; 439 } 440 441 var box; 442 if (typeof d == "string") { 443 box = loadevent == undefined || loadevent == null ? load_src() : load_src_loadevent(); 444 445 } else if (typeof d == "object") { 446 var lmd = { 447 id: d.id == undefined ? null : d.id, 448 src: d.src == undefined || d.src == null ? gd.errosrc : d.src, 449 alt: d.alt == undefined ? null : d.alt, 450 outer: d.outer == undefined || d.outer == null ? gd.outer : d.outer, 451 outercss: d.outercss == undefined || d.outercss - null ? gd.outercss : d.outercss, 452 outercls: d.outercls == undefined || d.outercls == null ? gd.outercls : g.outercls, 453 css: d.css == undefined || d.css == null ? gd.css : d.css, 454 cls: d.cls == undefined || d.cls == null ? gd.cls : d.cls, 455 tag: d.tag == undefined ? null : d.tag, 456 sort: d.sort == undefined ? null : d.sort, 457 } 458 box = loadevent==undefined || loadevent==null ?load(lmd):load_loadevent(lmd); 459 } 460 461 return box; 462 } 463 } 464 }
上面是主要的代码,这编写了两种模式,一种稳定宽度,一种稳定高度的,只有这么两种布局模型!
1 function addOneImage(src, le) { 2 3 //加载布局事件 4 5 var l = new luan(ld, null, le); 6 7 l.add(src); 8 //refreshEvent(); 9 } 10 11 function addImages(a) { 12 var index = 0; 13 var le = { 14 load: function (e, n, b, h, w, t) { 15 var picscang = $("<li />").attr("class", "picScang").click(function () { SoucangAjax(this);}); 16 var piczan = $("<li/>").attr("class", "piczan").click(function () { zanAjax(this);}); 17 var outbox = $("<div />").addClass("picStyle").attr("rtid", $(tag).find("ID").eq(index).text()); 18 $(picscang).appendTo(outbox); 19 $(piczan).appendTo(outbox); 20 $(b).remove(); 21 $(b).appendTo(outbox); 22 23 //C:\Users\sekeys\Desktop\Morgen\Morgen\js\dylayout.js 24 //alert($(t.element).html()); 25 $(outbox).appendTo(t.element); 26 27 //$(picscang).appendTo(outbox); 28 $(createFooter(index)).appendTo(outbox); 29 $(createComent(index)).appendTo(outbox); 30 31 $(outbox).mouseover(function () { 32 $(this).find(".picScang").toggle(); 33 $(this).find(".piczan").toggle(); 34 }); 35 $(outbox).mouseout(function () { 36 $(this).find(".picScang").toggle(); 37 $(this).find(".piczan").toggle(); 38 }); 39 index++; 40 if (index == a.length) { 41 if (typeof callback == "function") { 42 callback(); 43 } 44 return; 45 } else { 46 addOneImage(a[index], le); 47 } 48 } 49 } 50 if (a instanceof Array) { 51 addOneImage(a[index], le); 52 } 53 } 54 function sort(a) { 55 56 var le = { 57 load: function (e, n, b, h, w, t) { 58 var picscang = $("<li/>").attr("class", "picScang").click(function () { SoucangAjax(this);}); 59 var piczan = $("<li/>").attr("class", "piczan").click(function () { zanAjax(this); }); 60 var outbox = $("<div />").addClass("picStyle").attr("rtid",$(tag).find("ID").eq(t.index).text()); 61 $(picscang).appendTo(outbox); 62 $(piczan).appendTo(outbox); 63 $(b).remove(); 64 $(b).appendTo(outbox); 65 66 67 //alert($(t.element).html()); 68 $(outbox).appendTo(t.element); 69 70 //$(picscang).prependTo(b); 71 $(createFooter(t.index)).appendTo(outbox); 72 $(createComent(t.index)).appendTo(outbox); 73 74 $(outbox).mouseover(function () { 75 $(this).find(".picScang").toggle(); 76 $(this).find(".piczan").toggle(); 77 }); 78 $(outbox).mouseout(function () { 79 $(this).find(".picScang").toggle(); 80 $(this).find(".piczan").toggle(); 81 82 }); 83 84 } 85 } 86 var l = new luan(ld, null,le); 87 l.sort(a); 88 if (typeof callback == "function") { 89 callback(); 90 } 91 //$(".picLine1").each(function () { if ($(".picLine1").index(this) > 0) $(this).attr("class", "picLine1 picLine2") }); 92 } 93 var ele = document.getElementById(eleid); 94 if(ele.childElementCount== 0){ 95 sort(srcs); 96 $("#" + eleid).find(".picLine1").each(function () { 97 if ($("#"+eleid+" .picLine1").index(this) > 0) 98 $(this).attr("class", "picLine1 picLine2"); 99 }); 100 }else{ 101 addImages(srcs); 102 } 103 104 }
这是主要显示的地方。代码!
主要效果!

浙公网安备 33010602011771号