easy_background.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!-----------------------------------------------------------------------------
  2. * easy_background
  3. * v2.0 - built 2017-10-30
  4. * Licensed under the MIT License.
  5. * http://www.testersite.it/github/easy_background/v3/
  6. * ----------------------------------------------------------------------------
  7. * Copyright (C) 2017 Eugenio Segala
  8. * --------------------------------------------------------------------------*/
  9. function easy_background(selector, sld_args) {
  10. function empty_img(x) {
  11. if (x) {
  12. return "<img src='" + x + "'>";
  13. } else {
  14. return "";
  15. }
  16. }
  17. //use object same as arrays in php {nameofindex:variable} inside object you can use arrays [value1,val2] (variable in object can be as array
  18. //var sld_args={i:["img/555.jpg","img/44.jpg","img/33.jpg","img/22.jpg","img/11.jpg","img/1.jpg","img/2.jpg","img/3.jpg","img/4.jpg","img/5.jpg"],d:[3000,3000,3000,3000,3000] };
  19. //if delay is empty or forgotten then use this default value
  20. var def_del = 3000;
  21. var p = document.createElement("div");
  22. p.innerHTML = " ";
  23. p.classList.add("easy_slider");
  24. document.body.insertBefore(p, document.body.firstChild);
  25. //switch all values in object -- objectname.index in you case sld_args is object and i is index of array which keep images (i). We use this function for fill div with img tags
  26. //and for insert delays into empty or forgotten places in object
  27. sld_args.slide.forEach(function(v, i) {
  28. if (v) {
  29. document.querySelector(".easy_slider").innerHTML += empty_img(v);
  30. if (typeof sld_args.delay[i] == 'undefined' || typeof sld_args.delay[i] == '' || sld_args.delay[i] == 0) {
  31. sld_args.delay[i] = def_del;
  32. }
  33. }
  34. });
  35. //add various style on selector
  36. document.querySelector(".easy_slider").style.display = "none";
  37. //add various style on selector
  38. document.querySelector(selector).style.backgroundSize = "cover";
  39. document.querySelector(selector).style.backgroundRepeat = "no-repeat";
  40. document.querySelector(selector).style.backgroundPosition = "center center";
  41. setTimeout(function() {
  42. //add various style on selector
  43. if (typeof sld_args.transition_timing === 'undefined') {
  44. sld_args.transition_timing = "ease-in";
  45. }
  46. if (typeof sld_args.transition_duration === 'undefined') {
  47. sld_args.transition_duration = 500;
  48. }
  49. var transition = "all " + sld_args.transition_duration + 'ms ' + sld_args.transition_timing;
  50. document.querySelector(selector).style.WebkitTransition = transition;
  51. document.querySelector(selector).style.MozTransition = transition;
  52. document.querySelector(selector).style.MsTransition = transition;
  53. document.querySelector(selector).style.OTransition = transition;
  54. document.querySelector(selector).style.transition = transition;
  55. }, 100);
  56. //this n is number of row in object - if first row one function if more than 1 then other
  57. var n = 1;
  58. //li collection previous delays from previous slides
  59. var li = 0;
  60. function slider() {
  61. //switching all images one by one
  62. sld_args.slide.forEach(function(vvv, iii) {
  63. //here go all slides except first
  64. if (n > 1) {
  65. //set delay from collected number from previous slides
  66. var delay = li;
  67. setTimeout(function() {
  68. document.querySelector(selector).style.backgroundImage = "url('" + vvv + "')";
  69. }, delay); // >1
  70. //collecting delays from curent
  71. li = li + sld_args.delay[iii];
  72. } else { //this function for only first slide
  73. //next row
  74. n++;
  75. //collect delay first time
  76. li = sld_args.delay[iii];
  77. document.querySelector(selector).style.backgroundImage = "url('" + vvv + "')";
  78. }
  79. });
  80. };
  81. slider();
  82. setInterval(function() { // REPEAT
  83. slider();
  84. //here used length of array of delays in object instead you tot_time variable
  85. }, sld_args.delay.length);
  86. }