Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wall.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. function wall(parent, children, style, min) {
  2. var min = +min || 2
  3. var b = $(parent+':not(.hidden)').raw()
  4. var bs = $(parent+':not(.hidden) '+children).toArray()
  5. if (!window.getComputedStyle(b).height) {return}
  6. bs.forEach(function(el){el.style.width='';el.style.height=''})
  7. var rows = []
  8. if (typeof(style) === 'string') { style = +style }
  9. if (typeof(style) === 'number') {
  10. for (var i=0; i<bs.length; i+=style)
  11. rows.push(bs.slice(i,i+style))
  12. } else {
  13. var a = 0
  14. for (i in style) {
  15. rows.push(bs.slice(a, a+style[i]))
  16. a += style[i]
  17. }
  18. }
  19. if (rows.length >= 2 && rows[rows.length-1].length < min) {
  20. var needed = min - rows[rows.length-1].length
  21. if (rows[rows.length-2].length - needed >= min) {
  22. for(i=0; i<needed; i++) {
  23. rows[rows.length-1]=[rows[rows.length-2].pop()].concat(rows[rows.length-1])
  24. }
  25. } else {
  26. rows[rows.length-2] = rows[rows.length-2].concat(rows[rows.length-1])
  27. rows.splice(rows.length-1, 1)
  28. }
  29. }
  30. function getW(e) {
  31. var a = window.getComputedStyle(e).width.match(/[0-9.]+/)
  32. return a ? parseFloat(a[0]) : 0
  33. }
  34. function getH(e) {
  35. var a = window.getComputedStyle(e).height.match(/[0-9.]+/)
  36. return a ? parseFloat(a[0]) : 0
  37. }
  38. for (i in rows) {
  39. for (j in rows[i]) {
  40. rows[i][j].style.width = (getW(rows[i][j]) / getH(rows[i][j]) * 100) + 'px'
  41. rows[i][j].style.height = 100 + 'px'
  42. }
  43. var w = rows[i].reduce(function(x, y) { return x + getW(y) }, 0)
  44. for (j in rows[i]) {
  45. rows[i][j].style.height=(getH(rows[i][j])*(getW(b)-(4*rows[i].length))/w)+'px'
  46. rows[i][j].style.width=(getW(rows[i][j])*(getW(b)-(4*rows[i].length))/w)+'px'
  47. rows[i][j].style.display = 'inline-block'
  48. rows[i][j].style.margin = '1px'
  49. }
  50. }
  51. }
  52. $(function() {
  53. $('[data-wall-child]').each(function(i, el) {
  54. wall('#'+el.id, el.attributes['data-wall-child'].value, el.attributes['data-wall-size'].value, el.attributes['data-wall-min']?el.attributes['data-wall-min']:false)
  55. var sel = '#'+el.id+' '+el.attributes['data-wall-child'].value+' img'
  56. $(sel).on('load', function() {
  57. var test = true;
  58. $(sel).each(function(j, img) {
  59. if (!img.complete) test = false
  60. })
  61. if (test) wall('#'+el.id, el.attributes['data-wall-child'].value, el.attributes['data-wall-size'].value, el.attributes['data-wall-min']?el.attributes['data-wall-min']:false)
  62. })
  63. })
  64. })