main.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Get all dynamic text objects which will change
  3. */
  4. const person_name = document.querySelector("#title--name");
  5. const person_job = document.querySelector("#title--job");
  6. const person_text = document.querySelector("#title--text");
  7. const person_img = document.querySelector("#title--img");
  8. /**
  9. * Get button objects
  10. */
  11. const btn_prev = document.querySelector("#btn--prev");
  12. const btn_next = document.querySelector("#btn--next");
  13. const btn_random = document.querySelector("#btn--rnd");
  14. /**
  15. * Setup event listners for buttons
  16. */
  17. btn_prev.addEventListener("click", prev_person);
  18. btn_next.addEventListener("click", next_person);
  19. btn_random.addEventListener("click", random_person);
  20. let current_person_index = 0;
  21. const persons = [
  22. {
  23. id: 1,
  24. name: 'Adam Smith',
  25. job: 'Web Developer',
  26. img: 'img/720x600.png',
  27. text: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta cupiditate molestias consequuntur doloremque quae est maxime assumenda at, voluptatem non?'
  28. },
  29. {
  30. id: 2,
  31. name: 'Какой-то чел #1',
  32. job: 'Без работный',
  33. img: 'img/720x600.png',
  34. text: 'Какой-то текст #1'
  35. },
  36. {
  37. id: 3,
  38. name: 'Какой-то чел #2',
  39. job: 'C++ developer',
  40. img: 'img/EZ.png',
  41. text: 'Оч крутой чел, короче EZ'
  42. },
  43. {
  44. id: 4,
  45. name: 'Какой-то чел #3',
  46. job: 'Криптовалютный инвестор',
  47. img: 'img/720x600.png',
  48. text: 'Ку всем! Я не могу сейчас говорить, я на алгебре'
  49. }
  50. ];
  51. function prev_person()
  52. {
  53. if (--current_person_index < 0)
  54. {
  55. current_person_index = persons.length - 1;
  56. }
  57. update_data();
  58. }
  59. function next_person()
  60. {
  61. if (++current_person_index > (persons.length - 1))
  62. {
  63. current_person_index = 0;
  64. }
  65. update_data();
  66. }
  67. function random_person()
  68. {
  69. current_person_index = Math.floor(Math.random() * (persons.length - 1));
  70. if (current_person_index > (persons.length - 1))
  71. {
  72. current_person_index = persons.length - 1;
  73. }
  74. else if (current_person_index < 0)
  75. {
  76. current_person_index = 0;
  77. }
  78. update_data();
  79. }
  80. function update_data()
  81. {
  82. person_name.textContent = persons[current_person_index].name;
  83. person_job.textContent = persons[current_person_index].job;
  84. person_text.textContent = persons[current_person_index].text;
  85. person_img.src = persons[current_person_index].img;
  86. }