beam.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. $(function () {
  2. 'use strict';
  3. var container = $('container');
  4. var contentContainer0 = $('#content0');
  5. var contentContainer1 = $('#content1');
  6. var curContentContainer = contentContainer0;
  7. var nextContentContainer = contentContainer1;
  8. var content = [];
  9. function preload(imageUrl, callback) {
  10. var img = new Image();
  11. img.onload = function () {
  12. callback();
  13. };
  14. img.src = imageUrl;
  15. }
  16. function random(values) {
  17. return values[Number.parseInt(Math.random()*values.length)];
  18. }
  19. function showNext() {
  20. var img = random(content);
  21. preload(img, function () {
  22. nextContentContainer.css('background-image', 'url("' + img + '")');
  23. nextContentContainer.css('background-repeat', random(['no-repeat', 'space', 'repeat']));
  24. nextContentContainer.css('background-size', random(['cover', 'contain', 'unset', '50%', '120%', '30%']));
  25. if (random(['fade', 'slide']) === 'fade') {
  26. curContentContainer.fadeOut();
  27. nextContentContainer.fadeIn();
  28. } else {
  29. curContentContainer.slideToggle();
  30. nextContentContainer.slideToggle();
  31. }
  32. var tmp = nextContentContainer;
  33. nextContentContainer = curContentContainer;
  34. curContentContainer = tmp;
  35. setTimeout(showNext, 3000);
  36. });
  37. }
  38. $.get('/content.json', function (data) {
  39. content = data.content.map(function (img) { return img.replace('http://', 'https://'); }).map(function (img) { return img.replace('_75sq', '_500'); });
  40. showNext();
  41. });
  42. });