Created
April 23, 2018 11:56
-
-
Save tamimibrahim17/ed7d18a9404384ad7a78b723a9d56d33 to your computer and use it in GitHub Desktop.
input number custom style
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="quantity"> | |
<input type="number" min="1" max="9" step="1" value="1"> | |
</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
jQuery('<div class="quantity-nav"><div class="quantity-button quantity-up">+</div><div class="quantity-button quantity-down">-</div></div>').insertAfter('.quantity input'); | |
jQuery('.quantity').each(function() { | |
var spinner = jQuery(this), | |
input = spinner.find('input[type="number"]'), | |
btnUp = spinner.find('.quantity-up'), | |
btnDown = spinner.find('.quantity-down'), | |
min = input.attr('min'), | |
max = input.attr('max'); | |
btnUp.click(function() { | |
var oldValue = parseFloat(input.val()); | |
if (oldValue >= max) { | |
var newVal = oldValue; | |
} else { | |
var newVal = oldValue + 1; | |
} | |
spinner.find("input").val(newVal); | |
spinner.find("input").trigger("change"); | |
}); | |
btnDown.click(function() { | |
var oldValue = parseFloat(input.val()); | |
if (oldValue <= min) { | |
var newVal = oldValue; | |
} else { | |
var newVal = oldValue - 1; | |
} | |
spinner.find("input").val(newVal); | |
spinner.find("input").trigger("change"); | |
}); | |
}); |
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="//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
.quantity { | |
position: relative; | |
} | |
input[type=number]::-webkit-inner-spin-button, | |
input[type=number]::-webkit-outer-spin-button | |
{ | |
-webkit-appearance: none; | |
margin: 0; | |
} | |
input[type=number] | |
{ | |
-moz-appearance: textfield; | |
} | |
.quantity input { | |
width: 45px; | |
height: 42px; | |
line-height: 1.65; | |
float: left; | |
display: block; | |
padding: 0; | |
margin: 0; | |
padding-left: 20px; | |
border: 1px solid #eee; | |
} | |
.quantity input:focus { | |
outline: 0; | |
} | |
.quantity-nav { | |
float: left; | |
position: relative; | |
height: 42px; | |
} | |
.quantity-button { | |
position: relative; | |
cursor: pointer; | |
border-left: 1px solid #eee; | |
width: 20px; | |
text-align: center; | |
color: #333; | |
font-size: 13px; | |
font-family: "Trebuchet MS", Helvetica, sans-serif !important; | |
line-height: 1.7; | |
-webkit-transform: translateX(-100%); | |
transform: translateX(-100%); | |
-webkit-user-select: none; | |
-moz-user-select: none; | |
-ms-user-select: none; | |
-o-user-select: none; | |
user-select: none; | |
} | |
.quantity-button.quantity-up { | |
position: absolute; | |
height: 50%; | |
top: 0; | |
border-bottom: 1px solid #eee; | |
} | |
.quantity-button.quantity-down { | |
position: absolute; | |
bottom: -1px; | |
height: 50%; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment