Как расширить ширину изображений до ширины окна внутри карусели? - PullRequest
0 голосов
/ 31 января 2020

Вы можете увидеть мою карусель на моей странице Github. https://ionianthales.github.io/playground.github.io/

Я установил .carousel на position: absolute;, что является тегом ul. и когда я нажимаю .right-btn, эффект слайда работает. Но я использую двойной монитор, и когда я развернул окно веб-браузера по горизонтали по всей ширине монитора, я увидел, что другие изображения также отображаются на слайде. (Чтобы проверить эту проблему, я должен уменьшить размер веб-браузера по горизонтали, а затем обновить sh страницу, а затем развернуть веб-браузер по горизонтали)

Я попытался исправить это самостоятельно. Но это не сработало.

также я включу сюда свои коды.

html код

<!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);

Спасибо за чтение моего вопроса. :)

1 Ответ

0 голосов
/ 31 января 2020

Вы можете изменить правило CSS, как показано ниже:

   .slide {
      position: absolute;
      border: 1px solid red;
      margin: 0;
      width: 100%;
      height: 100%;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
    }

или

    .slide {
      width: 100vw;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...