From baf278036c96c1db95028a8fa5098941a45e3e0c Mon Sep 17 00:00:00 2001 From: YNazymko12 Date: Fri, 15 Dec 2023 17:12:23 +0100 Subject: [PATCH] Feat(JS) Update and add stars --- src/css/layout/05-modal-exercise.css | 21 ++++++--- src/js/05-modal-exercises.js | 64 +++++++++++++++++----------- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/src/css/layout/05-modal-exercise.css b/src/css/layout/05-modal-exercise.css index 8e19177..7d756da 100644 --- a/src/css/layout/05-modal-exercise.css +++ b/src/css/layout/05-modal-exercise.css @@ -41,7 +41,6 @@ .modal-exercises { position: relative; z-index: 100; - /* overflow-y: auto; */ overflow: auto; max-width: 100%; @@ -141,20 +140,32 @@ } } -.icon-star { +/* .icon-star { padding: 2px; width: 18px; height: 18px; - fill: var(--color-yellow); -} + fill: rgba(244, 244, 244, 0.2); +} */ -.icon-star-empty { +/* .icon-star-empty { padding: 2px; width: 18px; height: 18px; fill: rgba(244, 244, 244, 0.2); +} */ + +/* .icon-star:nth-child(3n + 1) { + fill: var(--color-yellow); } +.icon-star:nth-child(3n + 2) { + fill: var(--color-yellow); +} + +.icon-star:nth-child(3n) { + fill: var(--color-yellow); +} */ + .modal-exercises__block { margin-bottom: 40px; } diff --git a/src/js/05-modal-exercises.js b/src/js/05-modal-exercises.js index 20fc713..41a06a4 100644 --- a/src/js/05-modal-exercises.js +++ b/src/js/05-modal-exercises.js @@ -8,13 +8,13 @@ const listItem = document.querySelector('.js-list'); listItem.addEventListener('click', onExercisesCardClick); async function onExercisesCardClick(event) { - if (!event.target.closest('.filters__btn-second')) { + if (!event.target.closest('.card__btn')) { return; } try { const exerciseID = event.target - .closest('.filters__btn-second') + .closest('.card__btn') .getAttribute('data-id'); const exerciseData = await apiService.getExercisesById(exerciseID); @@ -43,34 +43,48 @@ function updateModal(markup) { } function createRating(rating) { - const maxRating = 5; - const filledStars = Math.floor(rating); - const remainder = rating - filledStars; - - const filledStarsHTML = - ''.repeat( - filledStars - ); - - const partialStarHTML = ''; - if (remainder > 0) { - const roundedRemainder = Math.round(remainder * 10) / 10; - partialStarHTML = ``; + const starColor = '#EEA10C'; + const emptyStarColor = '#F4F4F4'; + const totalStars = 5; + + let stars = ''; + for (let i = 0; i < totalStars; i++) { + const gradientId = `starGradient${i}`; + const fillPercentage = + i + 1 <= rating ? 100 : i < rating ? (rating % 1) * 100 : 0; + + const gradientStops = [ + { offset: '0%', color: starColor, opacity: '1' }, + { offset: `${fillPercentage}%`, color: starColor, opacity: '1' }, + { + offset: `${fillPercentage + 1}%`, + color: emptyStarColor, + opacity: '0.20', + }, + ]; + + // Генерація linearGradient + const linearGradient = ` + + ${gradientStops + .map( + stop => + `` + ) + .join('')} + + `; + + const fill = `url(#${gradientId})`; + + stars += `${linearGradient}`; } - const emptyStarsHTML = - ''.repeat( - maxRating - filledStars - ); - const ratingText = Number.isInteger(rating) ? `${rating}.0` - : `${rating}.toFixed(1)`; - const starsHTML = `${filledStarsHTML}${partialStarHTML}${emptyStarsHTML}`; + : rating.toFixed(1); - const ratingWithStars = `${ratingText} ${starsHTML}`; + const ratingWithStars = `${ratingText} ${stars}`; return ratingWithStars; }