-
-
Save Shobhit1/4379fd50d6504f0694b62c9f8a39d082 to your computer and use it in GitHub Desktop.
Slider using JS, HTML and CSS
This file contains 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="slider"> | |
<button id="previous-button">Previous</button> | |
<div class="item-container selected"></div> | |
<div class="item-container"></div> | |
<div class="item-container"></div> | |
<div class="item-container"></div> | |
<div class="item-container"></div> | |
<button id="next-button">Next</button> | |
</div> | |
</div> |
This file contains 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 rightCounter = 0; | |
var elements = document.getElementsByClassName('item-container'); | |
var numberOfElements = elements.length; | |
var leftCounter = numberOfElements; | |
document.getElementById("next-button").addEventListener('click', function() { | |
rightCounter++; | |
if(rightCounter >= numberOfElements) { | |
rightCounter = 0; | |
} | |
var selectedElement = document.getElementsByClassName('item-container selected'); | |
if (selectedElement) { | |
console.log(selectedElement[0].classList, rightCounter) | |
selectedElement[0].classList.remove('selected'); | |
} | |
elements[rightCounter].classList.add('selected'); | |
}) | |
document.getElementById("previous-button").addEventListener('click', function() { | |
leftCounter--; | |
if(leftCounter < 0) { | |
leftCounter = numberOfElements - 1; | |
} | |
var selectedElement = document.getElementsByClassName('item-container selected'); | |
if (selectedElement) { | |
console.log(selectedElement[0].classList, leftCounter) | |
selectedElement[0].classList.remove('selected'); | |
} | |
elements[leftCounter].classList.add('selected'); | |
}) |
This file contains 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
.slider { | |
height: 100%; | |
padding: 0; | |
margin: 0; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
} | |
.item-container { | |
border: 1px solid blue; | |
border-radius: 50%; | |
margin: 2px; | |
width: 10px; | |
height: 10px; | |
} | |
.item-container.selected { | |
background-color: black; | |
} | |
button { | |
margin: 0px 5px 0px 5px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment