Created
January 6, 2022 12:18
-
-
Save mbisht/56bbebea13f86ab75cf8903fcd6f2cd1 to your computer and use it in GitHub Desktop.
Single 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
| <section> | |
| <button class="next">Next</button> | |
| <button class="prev">Previous</button> | |
| <div class="container"> | |
| <div style="display:inline-block;"> | |
| <img src="http://placeimg.com/400/200/any"> | |
| </div> | |
| <div > | |
| <img src="http://placeimg.com/400/200/tech"> | |
| </div> | |
| <div> | |
| <img src="http://placeimg.com/400/200/nature"> | |
| </div> | |
| </div> | |
| </section> |
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
| var currentIndex = 0, | |
| items = $(".container div"), | |
| itemAmt = items.length; | |
| function cycleItems(){ | |
| var item = $(".container div").eq(currentIndex); | |
| items.hide(); | |
| item.css("display","inline-block"); | |
| } | |
| var autoSlide = setInterval(function(){ | |
| currentIndex += 1; | |
| if(currentIndex > itemAmt -1){ | |
| currentIndex = 0; | |
| } | |
| cycleItems(); | |
| }, 3000); | |
| $(".next").on("click", function(){ | |
| clearInterval(autoSlide) | |
| currentIndex += 1; | |
| if(currentIndex > itemAmt -1){ | |
| currentIndex = 0; | |
| } | |
| cycleItems(); | |
| }) | |
| $(".prev").on("click", function(){ | |
| clearInterval(autoSlide) | |
| currentIndex += 1; | |
| if(currentIndex > itemAmt -1){ | |
| currentIndex = 0; | |
| } | |
| cycleItems(); | |
| }) | |
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
| <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> |
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
| .container { | |
| max-width: 400px; | |
| background-color: black; | |
| margin: 0 auto; | |
| text-align: center; | |
| position: relative; | |
| } | |
| .container div { | |
| background-color: white; | |
| width: 100%; | |
| display: inline-block; | |
| display: none; | |
| } | |
| .container img { | |
| width: 100%; | |
| height: auto; | |
| } | |
| button { | |
| position: absolute; | |
| } | |
| .next { | |
| right: 5px; | |
| } | |
| .prev { | |
| left: 5px; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment