jeudi 30 janvier 2020

How to expand the width of images to the width of the window inside the carousel?

You can see my carousel on my Github page. https://ionianthales.github.io/playground.github.io/

I set .carousel to position: absolute; which is ul tag. and when I click the .right-btn, the slide effect works. But I use the dual monitor and when I expanded the web browser window horizontally over the full width of a monitor, I could see other images are shown in a slide too. ( To check this issue, I should decrease the size of web browser horizontally and then refresh the page and then expand the web browser horizontally )

I tried to fix it on my own. But It didn't work well.

also, I will include my codes here.

html code

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <link href="https://fonts.googleapis.com/css?family=Anton|Bowlby+One+SC|PT+Sans+Narrow&display=swap" rel="stylesheet">
</head>
<body>
    <nav class="nav-container">
        <div class="logo">
            LogoShop
        </div>
        <ul class="menu-container">
            <li class="menu-item"><a href="">shirt</a></li>
            <li class="menu-item"><a href="">pants</a></li>
            <li class="menu-item"><a href="">uniform</a></li>
            <li class="menu-item"><a href="">contact</a></li>
        </ul>
    </nav>
    <div class="carousel-container">
        <div class="carousel-wrapper">
            <ul class="carousel">
                <li class="slide active"><img src="images/freestocks-org-_3Q3tsJ01nc-unsplash.jpg" alt=""></li>
                <li class="slide"><img src="images/jonathan-francisca-HY-Nr7GQs3k-unsplash.jpg" alt=""></li>
                <li class="slide"><img src="images/tamara-bellis-0C2qrwkR1dI-unsplash.jpg" alt=""></li>
            </ul>
        </div>
        <button class="btn left-btn"><img src="images/left-arrow.svg" alt=""></button>
        <button class="btn right-btn"><img src="images/right-arrow.svg" alt=""></button>
        <div class="carousel-nav">
            <div class="dot active"></div>
            <div class="dot"></div>
            <div class="dot"></div>
        </div>
    </div>
    <script src="index.js"></script>

</body>
</html>

css

html{
    /* border: 1px red solid; */
    height: 100%;
}
body{
    height: 100%;
    margin: 0;
}
nav{
    /* border: 1px red solid; */
    width: 70%;
    margin: 0 auto;
    display: flex;
    justify-content: space-around;
    align-items: center;
}
ul{
    list-style: none;
}
.logo{
    /* border: 1px blue solid; */
    font-family: 'Anton', sans-serif;
    font-size: 30px;
}
.menu-container{
    /* border: 1px red solid; */
    display: flex;
    font-family: 'PT Sans Narrow', sans-serif;
    font-size: 25px;
    padding: 0;
}
.menu-item{
    /* border: 1px red solid; */
    margin: 0 15px;
}
li a{
    text-decoration: none;
    color: black;
}

/* carousel */
.carousel-container{
    position: relative;
    /* border: 1px red solid; */
    width: 100%;
    height: 500px;
}
.carousel-wrapper{
    /* border: 1px red solid; */
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
}
.carousel{
    /* border: 1px red solid; */
    height: 100%;
    margin: 0;
    padding: 0;
    list-style: none;
    transition: 300ms transform ease-in;
}
.slide{
    position: absolute;
    /* border: 1px red solid; */
    margin: 0;
    width: 100%;
    height: 100%;
}
.slide img{
    width: 100%;
    height: 100%;
    object-fit: cover;
    /* opacity: 0; */
}
.btn{
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    height: 100px;
    width: 40px;
    background-color: transparent;
    border: none;
    cursor: pointer;
}
.left-btn{
    left: 0;
}
.right-btn{
    right: 0;
}
.carousel-nav{
    position: absolute;
    /* background-color: dodgerblue; */
    width: 100%;
    height: 50px;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
}
.dot{
    border-radius: 50%;
    width: 20px;
    height: 20px;
    background-color: grey;
    opacity: 0.5;
    margin: 0 20px;
    cursor: pointer;
}
.dot.active{
    background-color: grey;
    opacity: 1;
}

javascript

const carousel = document.querySelector('.carousel');
const slides = [...carousel.children];
const nextBtn = document.querySelector('.right-btn');
const previousBtn = document.querySelector('.left-btn');

const slideWidth = slides[0].getBoundingClientRect().width;

console.log(slideWidth);

function positionSlides(slides){
    for (let i=0; i<slides.length; i++){
        slides[i].style.left = slideWidth * i + 'px';
    };
};

nextBtn.addEventListener('click', function(){
    const currentSlide = carousel.querySelector('.active');
    const nextSlide = currentSlide.nextElementSibling;
    const position = nextSlide.style.left;

    carousel.style.transform = `translateX(-${position})`;
    currentSlide.classList.remove('active');
    nextSlide.classList.add('active');

});

previousBtn.addEventListener('click', function(){
    const currentSlide = carousel.querySelector('.active');
    const previousSlide = currentSlide.previousElementSibling;
    const position = previousSlide.style.left;

    carousel.style.transform = `translateX(-${position})`;
    currentSlide.classList.remove('active');
    previousSlide.classList.add('active');
});

positionSlides(slides);

Thanks for reading my question. : )




Aucun commentaire:

Enregistrer un commentaire