Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

карусель компонент #16

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions 6-module/3-task/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,100 @@
import createElement from '../../assets/lib/create-element.js';

export default class Carousel {

constructor(slides) {
this.slides = slides;

this.currentSlideNumber = 0;
this.render();
this.addEventListeners();
}

render() {
this.elem = createElement(`
<div class="carousel">
<div class="carousel__arrow carousel__arrow_right">
<img src="/assets/images/icons/angle-icon.svg" alt="icon" />
</div>
<div class="carousel__arrow carousel__arrow_left">
<img src="/assets/images/icons/angle-left-icon.svg" alt="icon" />
</div>
<div class="carousel__inner"></div>
</div>
`);

let slides = this.slides.map(item => createElement(`
<div class="carousel__slide" data-id="${item.id}">
<img
src="/assets/images/carousel/${item.image}"
class="carousel__img"
alt="slide"
/>
<div class="carousel__caption">
<span class="carousel__price">€${item.price.toFixed(2)}</span>
<div class="carousel__title">${item.name}</div>
<button type="button" class="carousel__button">
<img src="/assets/images/icons/plus-icon.svg" alt="icon" />
</button>
</div>
</div>`));

this.sub('inner').append(...slides);

this.update();
}

addEventListeners() {
this.elem.onclick = ({target}) => {
let button = target.closest('.carousel__button');
if (button) {
let id = target.closest('[data-id]').dataset.id;

this.elem.dispatchEvent(new CustomEvent('product-add', {
detail: id,
bubbles: true
}));
}

if (target.closest('.carousel__arrow_right')) {
this.next();
}

if (target.closest('.carousel__arrow_left')) {
this.prev();
}
};
}

sub(ref) {
return this.elem.querySelector(`.carousel__${ref}`);
}

next() {
this.currentSlideNumber++;
this.update();
}

prev() {
this.currentSlideNumber--;
this.update();
}

update() {
let offset = -this.elem.offsetWidth * this.currentSlideNumber;
this.sub('inner').style.transform = `translateX(${offset}px)`;

if (this.currentSlideNumber == this.slides.length - 1) {
this.sub('arrow_right').style.display = 'none';
} else {
this.sub('arrow_right').style.display = '';
}

if (this.currentSlideNumber == 0) {
this.sub('arrow_left').style.display = 'none';
} else {
this.sub('arrow_left').style.display = '';
}
}

}