A Pen by Brad Traversy on CodePen.
Created
July 19, 2018 14:28
-
-
Save MorenoMdz/0c7013e9814cd797e65d46dc2425a569 to your computer and use it in GitHub Desktop.
Fullscreen Image Slider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="wrap"> | |
| <div id="arrow-left" class="arrow"></div> | |
| <div id="slider"> | |
| <div class="slide slide1"> | |
| <div class="slide-content"> | |
| <span>Image One</span> | |
| </div> | |
| </div> | |
| <div class="slide slide2"> | |
| <div class="slide-content"> | |
| <span>Image Two</span> | |
| </div> | |
| </div> | |
| <div class="slide slide3"> | |
| <div class="slide-content"> | |
| <span>Image Three</span> | |
| </div> | |
| </div> | |
| </div> | |
| <div id="arrow-right" class="arrow"></div> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let sliderImages = document.querySelectorAll(".slide"), | |
| arrowLeft = document.querySelector("#arrow-left"), | |
| arrowRight = document.querySelector("#arrow-right"), | |
| current = 0; | |
| // Clear all images | |
| function reset() { | |
| for (let i = 0; i < sliderImages.length; i++) { | |
| sliderImages[i].style.display = "none"; | |
| } | |
| } | |
| // Init slider | |
| function startSlide() { | |
| reset(); | |
| sliderImages[0].style.display = "block"; | |
| } | |
| // Show prev | |
| function slideLeft() { | |
| reset(); | |
| sliderImages[current - 1].style.display = "block"; | |
| current--; | |
| } | |
| // Show next | |
| function slideRight() { | |
| reset(); | |
| sliderImages[current + 1].style.display = "block"; | |
| current++; | |
| } | |
| // Left arrow click | |
| arrowLeft.addEventListener("click", function() { | |
| if (current === 0) { | |
| current = sliderImages.length; | |
| } | |
| slideLeft(); | |
| }); | |
| // Right arrow click | |
| arrowRight.addEventListener("click", function() { | |
| if (current === sliderImages.length - 1) { | |
| current = -1; | |
| } | |
| slideRight(); | |
| }); | |
| startSlide(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| body, | |
| #slider, | |
| .wrap, | |
| .slide-content { | |
| margin: 0; | |
| padding: 0; | |
| font-family: Arial, Helvetica, sans-serif; | |
| width: 100%; | |
| height: 100vh; | |
| overflow-x: hidden; | |
| } | |
| .wrap { | |
| position: relative; | |
| } | |
| .slide { | |
| background-size: cover; | |
| background-position: center; | |
| background-repeat: no-repeat; | |
| } | |
| .slide1 { | |
| background-image: url("https://static.pexels.com/photos/302889/pexels-photo-302889.jpeg"); | |
| } | |
| .slide2 { | |
| background-image: url("https://static.pexels.com/photos/302892/pexels-photo-302892.jpeg"); | |
| } | |
| .slide3 { | |
| background-image: url("https://static.pexels.com/photos/226633/pexels-photo-226633.jpeg"); | |
| } | |
| .slide-content { | |
| display: flex; | |
| flex-direction: column; | |
| justify-content: center; | |
| align-items: center; | |
| text-align: center; | |
| } | |
| .slide-content span { | |
| font-size: 5rem; | |
| color: #fff; | |
| } | |
| .arrow { | |
| cursor: pointer; | |
| position: absolute; | |
| top: 50%; | |
| margin-top: -35px; | |
| width: 0; | |
| height: 0; | |
| border-style: solid; | |
| } | |
| #arrow-left { | |
| border-width: 30px 40px 30px 0; | |
| border-color: transparent #fff transparent transparent; | |
| left: 0; | |
| margin-left: 30px; | |
| } | |
| #arrow-right { | |
| border-width: 30px 0 30px 40px; | |
| border-color: transparent transparent transparent #fff; | |
| right: 0; | |
| margin-right: 30px; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment