Добавить случайное свойство объекту
Есть 9 картинок на странице, им нужно применить css свойство clip-path случайным образом. Правильно ли я мыслю, что исправить?
let cropImage = [ 'polygon(0 0, 100% 0%, 97% 100%, 0% 100%)', 'polygon(0 0, 100% 0%, 100% 100%, 0 94%)', 'polygon(0 0, 100% 0%, 100% 94%, 0 100%)', 'polygon(0 0, 94% 0, 100% 100%, 0 100%)', 'polygon(0 0, 100% 3%, 100% 100%, 0 100%)', 'polygon(1% 6%, 100% 0, 100% 100%, 0 100%)', 'polygon(5% 0, 100% 0, 100% 100%, 0 100%)', 'polygon(0 0, 100% 0, 100% 100%, 4% 100%)', 'polygon(0 0, 94% 0, 100% 100%, 0 100%)', ]; let images = document.querySelectorAll('img'); document.addEventListener("DOMContentLoaded", function () { if (images.length > 0) { for (let index = 0; index < images.length; index++) { images[index].style.clipPath = Math.floor(Math.random() * cropImage.length); } } }); UPD: Решила сама)) images[index].style.clipPath = cropImage[Math.floor(Math.random() * cropImage.length)]; |
Цитата:
|
Angelinasen,
<!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> .mySlider__wrapper { display: flex; flex-wrap: wrap; } </style> <script> let cropImage = [ 'polygon(0 0, 100% 0%, 97% 100%, 0% 100%)', 'polygon(0 0, 100% 0%, 100% 100%, 0 94%)', 'polygon(0 0, 100% 0%, 100% 94%, 0 100%)', 'polygon(0 0, 94% 0, 100% 100%, 0 100%)', 'polygon(0 0, 100% 3%, 100% 100%, 0 100%)', 'polygon(1% 6%, 100% 0, 100% 100%, 0 100%)', 'polygon(5% 0, 100% 0, 100% 100%, 0 100%)', 'polygon(0 0, 100% 0, 100% 100%, 4% 100%)', 'polygon(0 0, 94% 0, 100% 100%, 0 100%)', ]; document.addEventListener("DOMContentLoaded", function() { let images = document.querySelectorAll('img'); images.forEach(({ style }) => style.clipPath = cropImage[Math.random() * cropImage.length | 0]) }); </script> </head> <body> <div class="mySlider__wrapper"> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?1"></div> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?2"></div> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?3"></div> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?4"></div> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?5"></div> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?6"></div> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?7"></div> <div class="mySlider__slide"><img src="https://picsum.photos/200/145?8"></div> </div> </body> </html> |
случайные свойства объекту
Спасибо! ForEach надо бы подучить
|
Часовой пояс GMT +3, время: 02:13. |