使用 CSS 自定义输入类型数字的增量箭头
Customizing Increment Arrows on Input of Type Number Using CSS
我有一个使用以下代码呈现的数字类型的输入:
<input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number">
看起来像这样:
我想把它变成这样:
第二个视图是使用两个单独的按钮模拟的。
我如何按照描述设置箭头样式?
tl;博士:
私下被问及以下设置很多次后,我决定为其添加一个演示(Bootstrap 4 + jQuery + Font Awesome 输入组设置):
$('.btn-plus, .btn-minus').on('click', function(e) {
const isNegative = $(e.target).closest('.btn-minus').is('.btn-minus');
const input = $(e.target).closest('.input-group').find('input');
if (input.is('input')) {
input[0][isNegative ? 'stepDown' : 'stepUp']()
}
})
.inline-group {
max-width: 9rem;
padding: .5rem;
}
.inline-group .form-control {
text-align: right;
}
.form-control[type="number"]::-webkit-inner-spin-button,
.form-control[type="number"]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div class="input-group inline-group">
<div class="input-group-prepend">
<button class="btn btn-outline-secondary btn-minus">
<i class="fa fa-minus"></i>
</button>
</div>
<input class="form-control quantity" min="0" name="quantity" value="1" type="number">
<div class="input-group-append">
<button class="btn btn-outline-secondary btn-plus">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
长(初始)答案:
原生 input[type=number]
控件无法跨浏览器设置样式。实现您想要的效果的最简单和最安全的方法 cross-browser/cross-device 是使用以下方法隐藏它们:
input[type="number"] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
}
...这允许您使用您的自定义按钮,这些按钮可以链接以执行微调器(箭头)将执行的功能(.stepUp()
和 .stepDown()
),前提是您保持输入的type="number"
.
例如:
input[type="number"] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
}
.number-input {
border: 2px solid #ddd;
display: inline-flex;
}
.number-input,
.number-input * {
box-sizing: border-box;
}
.number-input button {
outline:none;
-webkit-appearance: none;
background-color: transparent;
border: none;
align-items: center;
justify-content: center;
width: 3rem;
height: 3rem;
cursor: pointer;
margin: 0;
position: relative;
}
.number-input button:before,
.number-input button:after {
display: inline-block;
position: absolute;
content: '';
width: 1rem;
height: 2px;
background-color: #212121;
transform: translate(-50%, -50%);
}
.number-input button.plus:after {
transform: translate(-50%, -50%) rotate(90deg);
}
.number-input input[type=number] {
font-family: sans-serif;
max-width: 5rem;
padding: .5rem;
border: solid #ddd;
border-width: 0 2px;
font-size: 2rem;
height: 3rem;
font-weight: bold;
text-align: center;
}
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown()" ></button>
<input class="quantity" min="0" name="quantity" value="1" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class="plus"></button>
</div>
注意:为了改变输入的值,需要找到它。为了提供灵活性,在上面的示例中,我将按钮和 <input>
分组在一个公共父项下,并使用该父项找到 <input>
(选择不依赖它们的接近度或 [=134= 中的特定顺序) ]).上面的方法将改变任何input[type=number]
同级按钮。如果这不方便,可以使用任何其他方法从按钮中找到输入:
- 通过 id:
.querySelector('#some-id')
:
<button onclick="this.parentNode.querySelector('#some-id').stepUp()"></button>
- 来自 类名:
.querySelector('.some-class')
:
<button onclick="this.parentNode.querySelector('.some-class').stepUp()"></button>
还要注意上面的例子只在.parentNode
里面搜索,而不是在整个document
里面搜索,这也是可以的:
即:onclick="document.getElementById('#some-id').stepUp()"
- 距离 (
previousElementSibling
| nextElementSibling
)
<button onclick="this.previousElementSibling.stepUp()"></button>
- 在 DOM 结构中确定和查找特定输入元素的任何其他方式。例如,可以使用第三方库,例如 jQuery:
<button onclick="$(this).prev()[0].stepUp()"></button>
使用 jQuery 时的一个重要注意事项是 stepUp()
和 stepDown()
方法放在 DOM 元素上,而不是 jQuery 包装器上. DOM 元素位于 jQuery
包装器的 0
属性 内。
注意 preventDefault()
。单击 <form>
中的 <button>
将触发表单提交。因此,如果像上面那样使用,在表格内部,onclick
也应该包含 preventDefault();
。示例:
<button onclick="$(this).prev()[0].stepUp();preventDefault()"></button>
但是,如果要使用 <a>
标签而不是 <button>
s,则没有必要。此外,可以使用一个小 JavaScript 代码段为所有表单按钮全局设置预防:
var buttons = document.querySelectorAll('form button:not([type="submit"])');
for (i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function(e) {
e.preventDefault();
});
}
... 或者,使用 jQuery:
$('form').on('click', 'button:not([type="submit"])', function(e){
e.preventDefault();
})
您可以像这样轻松地将第一个设计转换为第二个设计:
HTML
<div class="quantity">
<button class="btn minus1">-</button>
<input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number">
<button class="btn add1">+</button>
CSS
.quantity{
display:flex;
width:160px;
}
/* it will support chrome and firefox */
.quantity input[type=number]::-webkit-inner-spin-button,
.quantity input[type=number]::-webkit-outer-spin-button{
-webkit-appearance:none;
}
.quantity input,.quantity button{
width:50px;
padding:.5em;
font-size:1.2rem;
text-align:center;
font-weight:900;
background:white;
border:1px solid #aaa;
}
.quantity input{
border-left:none;
border-right:none;
}
我找到了一个很好的解决方案。只需旋转箭头键并将不透明度设置为 0。(它们现在位于正确的位置,不可见但可单击)然后在这些不可见按钮上设置 :after 和 :before 元素。然后可以根据需要设置这些元素的样式。
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
transform: rotate(90deg);
height: 80px;
opacity: 0;
}
.quantity-wrapper {
position: relative;
}
.quantity-wrapper:after {
content: "+";
position: absolute;
right: 5px;
height: 100%;
top: 8px;
pointer-events: none;
}
.quantity-wrapper:before {
content: "-";
position: absolute;
left: 5px;
height: 100%;
top: 8px;
}
<div class="quantity-wrapper">
<input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number">
</div>
可以使用我最近发现的以下脚本以这种方式对数字输入进行样式化:
Demo.
这个脚本是一个super-light-weight(1.49kb 未压缩,0.71kb 压缩,0.35kb gzipped),可用且可靠的数量输入,它取代了内置在数字输入中的可怕、笨拙的小输入按钮浏览器。它是使用 ES6 模块编写的,因此需要为旧版浏览器进行转换。
作者的存储库是 here。
希望这会有所帮助 ;)
编辑:
或者,如果您想要 up/down 箭头按钮而不是 plus/minus 箭头按钮,可以使用 another jQuery-based solution.
$(document).ready(function () {
jQuery('<div class="quantity-nav"><button class="quantity-button quantity-up"></button><button class="quantity-button quantity-down"></button></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");
});
});
});
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
min-width: 100vw;
background: #34495E;
font-size: 1rem;
}
.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: none;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08);
font-size: 1rem;
border-radius: 4px;
}
.quantity input:focus {
outline: 0;
}
.quantity-nav {
float: left;
position: relative;
height: 42px;
}
.quantity-button {
position: relative;
cursor: pointer;
border: none;
border-left: 1px solid rgba(0, 0, 0, 0.08);
width: 21px;
text-align: center;
color: #333;
font-size: 13px;
font-family: "FontAwesome" !important;
line-height: 1.5;
padding: 0;
background: #FAFAFA;
-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:active {
background: #EAEAEA;
}
.quantity-button.quantity-up {
position: absolute;
height: 50%;
top: 0;
border-bottom: 1px solid rgba(0, 0, 0, 0.08);
font-family: "FontAwesome";
border-radius: 0 4px 0 0;
line-height: 1.6
}
.quantity-button.quantity-down {
position: absolute;
bottom: 0;
height: 50%;
font-family: "FontAwesome";
border-radius: 0 0 4px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="quantity">
<input type="number" min="1" max="9" step="1" value="1">
</div>
这是另一个版本,基于@tao 的回答,使用了超棒的字体:
input[type="number"] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
}
.number-input {
border: 2px solid #ddd;
display: inline-flex;
}
.number-input,
.number-input * {
box-sizing: border-box;
}
.number-input button {
outline:none;
-webkit-appearance: none;
background-color: transparent;
border: none;
align-items: center;
justify-content: center;
width: 3rem;
height: 3rem;
cursor: pointer;
margin: 0;
position: relative;
}
.number-input button:after {
display: inline-block;
position: absolute;
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: '\f077';
transform: translate(-50%, -50%) rotate(180deg);
}
.number-input button.plus:after {
transform: translate(-50%, -50%) rotate(0deg);
}
.number-input input[type=number] {
font-family: sans-serif;
max-width: 5rem;
padding: .5rem;
border: solid #ddd;
border-width: 0 2px;
font-size: 2rem;
height: 3rem;
font-weight: bold;
text-align: center;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown()" class="minus"></button>
<input class="quantity" min="0" name="quantity" value="1" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class="plus"></button>
</div>
此代码可以帮助您。也可以在[这里]找到(Detecting number input spinner click
$.fn.spinInput = function (options) {
var settings = $.extend({
maximum: 1000,
minimum: 0,
value: 1,
onChange: null
}, options);
return this.each(function (index, item) {
var min = $(item).find('>*:first-child').first();
var max = $(item).find('>*:last-child').first();
var v_span = $(item).find('>*:nth-child(2)').find('span');
var v_input = $(item).find('>*:nth-child(2)').find('input');
var value = settings.value;
$(v_input).val(value);
$(v_span).text(value);
async function increment() {
value = Number.parseInt($(v_input).val());
if ((value - 1) > settings.maximum) return;
value++;
$(v_input).val(value);
$(v_span).text(value);
if (settings.onChange) settings.onChange(value);
}
async function desincrement() {
value = Number.parseInt($(v_input).val());
if ((value - 1) < settings.minimum) return;
value--
$(v_input).val(value);
$(v_span).text(value);
if (settings.onChange) settings.onChange(value);
}
var pressTimer;
function actionHandler(btn, fct, time = 100, ...args) {
function longHandler() {
pressTimer = window.setTimeout(function () {
fct(...args);
clearTimeout(pressTimer);
longHandler()
}, time);
}
$(btn).mouseup(function () {
clearTimeout(pressTimer);
}).mousedown(function () {
longHandler();
});
$(btn).click(function () {
fct(...args);
});
}
actionHandler(min, desincrement, 100);
actionHandler(max, increment, 100)
})
}
$('body').ready(function () {
$('.spin-input').spinInput({ value: 1, minimum: 1 });
});
:root {
--primary-dark-color: #F3283C;
--primary-light-color: #FF6978;
--success-dark-color: #32A071;
--sucess-light-color: #06E775;
--alert-light-color: #a42a23;
--alert-dark-color: #7a1f1a;
--secondary-dark-color: #666666;
--secondary-light-color: #A6A6A6;
--gold-dark-color: #FFA500;
--gold-light-color: #FFBD00;
--default-dark-color: #1E2C31;
--default-light-color: #E5E5E5;
}
.fx-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.fx-colum {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.fx-colum.nowrap,
.fx-row.nowrap {
flex-wrap: nowrap;
}
.fx-row.fx-fill>*,
.fx-colum.fx-fill>* {
flex-grow: 1;
}
.spin-input {
border: 1px solid var(--secondary-light-color);
}
.spin-input>div:first-child {
cursor: pointer;
border-right: 1px solid var(--secondary-light-color);
}
.spin-input>div:first-child:active {
transform: translate3d(1px, 0px, 1px)
}
.spin-input>div:last-child {
flex: none;
border-left: 1px solid var(--secondary-light-color);
cursor: pointer;
}
.spin-input>div:last-child:active {
transform: translate3d(1px, 0px, 1px)
}
.icon {
font-weight: bold;
text-align: center;
vertical-align: middle;
padding: 12px;
font-size: 28px;
}
.icon.primary,
.icon.primary .ci {
color: var(--primary-dark-color);
}
.icon.reactive:hover .ci {
color: var(--primary-light-color);
}
.hidden {
display: none;
}
<script src="https://releases.jquery.com/git/jquery-3.x-git.min.js"></script>
<div class="spin-input nowrap fx-row fx-fill" >
<div class="icon reactive">
<span class="ci ci-minus">-</span>
</div>
<div class="icon">
<span>0</span>
<input type="text" class="hidden" value="0">
</div>
<div class="icon reactive">
<span class="ci ci-plus">+</span>
</div>
</div>
259)
我有一个使用以下代码呈现的数字类型的输入:
<input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number">
看起来像这样:
我想把它变成这样:
第二个视图是使用两个单独的按钮模拟的。
我如何按照描述设置箭头样式?
tl;博士:
私下被问及以下设置很多次后,我决定为其添加一个演示(Bootstrap 4 + jQuery + Font Awesome 输入组设置):
$('.btn-plus, .btn-minus').on('click', function(e) {
const isNegative = $(e.target).closest('.btn-minus').is('.btn-minus');
const input = $(e.target).closest('.input-group').find('input');
if (input.is('input')) {
input[0][isNegative ? 'stepDown' : 'stepUp']()
}
})
.inline-group {
max-width: 9rem;
padding: .5rem;
}
.inline-group .form-control {
text-align: right;
}
.form-control[type="number"]::-webkit-inner-spin-button,
.form-control[type="number"]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<div class="input-group inline-group">
<div class="input-group-prepend">
<button class="btn btn-outline-secondary btn-minus">
<i class="fa fa-minus"></i>
</button>
</div>
<input class="form-control quantity" min="0" name="quantity" value="1" type="number">
<div class="input-group-append">
<button class="btn btn-outline-secondary btn-plus">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
长(初始)答案:
原生 input[type=number]
控件无法跨浏览器设置样式。实现您想要的效果的最简单和最安全的方法 cross-browser/cross-device 是使用以下方法隐藏它们:
input[type="number"] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
}
...这允许您使用您的自定义按钮,这些按钮可以链接以执行微调器(箭头)将执行的功能(.stepUp()
和 .stepDown()
),前提是您保持输入的type="number"
.
例如:
input[type="number"] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
}
.number-input {
border: 2px solid #ddd;
display: inline-flex;
}
.number-input,
.number-input * {
box-sizing: border-box;
}
.number-input button {
outline:none;
-webkit-appearance: none;
background-color: transparent;
border: none;
align-items: center;
justify-content: center;
width: 3rem;
height: 3rem;
cursor: pointer;
margin: 0;
position: relative;
}
.number-input button:before,
.number-input button:after {
display: inline-block;
position: absolute;
content: '';
width: 1rem;
height: 2px;
background-color: #212121;
transform: translate(-50%, -50%);
}
.number-input button.plus:after {
transform: translate(-50%, -50%) rotate(90deg);
}
.number-input input[type=number] {
font-family: sans-serif;
max-width: 5rem;
padding: .5rem;
border: solid #ddd;
border-width: 0 2px;
font-size: 2rem;
height: 3rem;
font-weight: bold;
text-align: center;
}
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown()" ></button>
<input class="quantity" min="0" name="quantity" value="1" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class="plus"></button>
</div>
注意:为了改变输入的值,需要找到它。为了提供灵活性,在上面的示例中,我将按钮和 <input>
分组在一个公共父项下,并使用该父项找到 <input>
(选择不依赖它们的接近度或 [=134= 中的特定顺序) ]).上面的方法将改变任何input[type=number]
同级按钮。如果这不方便,可以使用任何其他方法从按钮中找到输入:
- 通过 id:
.querySelector('#some-id')
:
<button onclick="this.parentNode.querySelector('#some-id').stepUp()"></button>
- 来自 类名:
.querySelector('.some-class')
:
<button onclick="this.parentNode.querySelector('.some-class').stepUp()"></button>
还要注意上面的例子只在.parentNode
里面搜索,而不是在整个document
里面搜索,这也是可以的:
即:onclick="document.getElementById('#some-id').stepUp()"
- 距离 (
previousElementSibling
|nextElementSibling
)
<button onclick="this.previousElementSibling.stepUp()"></button>
- 在 DOM 结构中确定和查找特定输入元素的任何其他方式。例如,可以使用第三方库,例如 jQuery:
<button onclick="$(this).prev()[0].stepUp()"></button>
使用 jQuery 时的一个重要注意事项是 stepUp()
和 stepDown()
方法放在 DOM 元素上,而不是 jQuery 包装器上. DOM 元素位于 jQuery
包装器的 0
属性 内。
注意 preventDefault()
。单击 <form>
中的 <button>
将触发表单提交。因此,如果像上面那样使用,在表格内部,onclick
也应该包含 preventDefault();
。示例:
<button onclick="$(this).prev()[0].stepUp();preventDefault()"></button>
但是,如果要使用 <a>
标签而不是 <button>
s,则没有必要。此外,可以使用一个小 JavaScript 代码段为所有表单按钮全局设置预防:
var buttons = document.querySelectorAll('form button:not([type="submit"])');
for (i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', function(e) {
e.preventDefault();
});
}
... 或者,使用 jQuery:
$('form').on('click', 'button:not([type="submit"])', function(e){
e.preventDefault();
})
您可以像这样轻松地将第一个设计转换为第二个设计:
HTML
<div class="quantity">
<button class="btn minus1">-</button>
<input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number">
<button class="btn add1">+</button>
CSS
.quantity{
display:flex;
width:160px;
}
/* it will support chrome and firefox */
.quantity input[type=number]::-webkit-inner-spin-button,
.quantity input[type=number]::-webkit-outer-spin-button{
-webkit-appearance:none;
}
.quantity input,.quantity button{
width:50px;
padding:.5em;
font-size:1.2rem;
text-align:center;
font-weight:900;
background:white;
border:1px solid #aaa;
}
.quantity input{
border-left:none;
border-right:none;
}
我找到了一个很好的解决方案。只需旋转箭头键并将不透明度设置为 0。(它们现在位于正确的位置,不可见但可单击)然后在这些不可见按钮上设置 :after 和 :before 元素。然后可以根据需要设置这些元素的样式。
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
transform: rotate(90deg);
height: 80px;
opacity: 0;
}
.quantity-wrapper {
position: relative;
}
.quantity-wrapper:after {
content: "+";
position: absolute;
right: 5px;
height: 100%;
top: 8px;
pointer-events: none;
}
.quantity-wrapper:before {
content: "-";
position: absolute;
left: 5px;
height: 100%;
top: 8px;
}
<div class="quantity-wrapper">
<input class="quantity" id="id_form-0-quantity" min="0" name="form-0-quantity" value="1" type="number">
</div>
可以使用我最近发现的以下脚本以这种方式对数字输入进行样式化:
Demo.
这个脚本是一个super-light-weight(1.49kb 未压缩,0.71kb 压缩,0.35kb gzipped),可用且可靠的数量输入,它取代了内置在数字输入中的可怕、笨拙的小输入按钮浏览器。它是使用 ES6 模块编写的,因此需要为旧版浏览器进行转换。
作者的存储库是 here。 希望这会有所帮助 ;)
编辑: 或者,如果您想要 up/down 箭头按钮而不是 plus/minus 箭头按钮,可以使用 another jQuery-based solution.
$(document).ready(function () {
jQuery('<div class="quantity-nav"><button class="quantity-button quantity-up"></button><button class="quantity-button quantity-down"></button></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");
});
});
});
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
min-width: 100vw;
background: #34495E;
font-size: 1rem;
}
.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: none;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08);
font-size: 1rem;
border-radius: 4px;
}
.quantity input:focus {
outline: 0;
}
.quantity-nav {
float: left;
position: relative;
height: 42px;
}
.quantity-button {
position: relative;
cursor: pointer;
border: none;
border-left: 1px solid rgba(0, 0, 0, 0.08);
width: 21px;
text-align: center;
color: #333;
font-size: 13px;
font-family: "FontAwesome" !important;
line-height: 1.5;
padding: 0;
background: #FAFAFA;
-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:active {
background: #EAEAEA;
}
.quantity-button.quantity-up {
position: absolute;
height: 50%;
top: 0;
border-bottom: 1px solid rgba(0, 0, 0, 0.08);
font-family: "FontAwesome";
border-radius: 0 4px 0 0;
line-height: 1.6
}
.quantity-button.quantity-down {
position: absolute;
bottom: 0;
height: 50%;
font-family: "FontAwesome";
border-radius: 0 0 4px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="quantity">
<input type="number" min="1" max="9" step="1" value="1">
</div>
这是另一个版本,基于@tao 的回答,使用了超棒的字体:
input[type="number"] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
}
.number-input {
border: 2px solid #ddd;
display: inline-flex;
}
.number-input,
.number-input * {
box-sizing: border-box;
}
.number-input button {
outline:none;
-webkit-appearance: none;
background-color: transparent;
border: none;
align-items: center;
justify-content: center;
width: 3rem;
height: 3rem;
cursor: pointer;
margin: 0;
position: relative;
}
.number-input button:after {
display: inline-block;
position: absolute;
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: '\f077';
transform: translate(-50%, -50%) rotate(180deg);
}
.number-input button.plus:after {
transform: translate(-50%, -50%) rotate(0deg);
}
.number-input input[type=number] {
font-family: sans-serif;
max-width: 5rem;
padding: .5rem;
border: solid #ddd;
border-width: 0 2px;
font-size: 2rem;
height: 3rem;
font-weight: bold;
text-align: center;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" />
<div class="number-input">
<button onclick="this.parentNode.querySelector('input[type=number]').stepDown()" class="minus"></button>
<input class="quantity" min="0" name="quantity" value="1" type="number">
<button onclick="this.parentNode.querySelector('input[type=number]').stepUp()" class="plus"></button>
</div>
此代码可以帮助您。也可以在[这里]找到(Detecting number input spinner click
$.fn.spinInput = function (options) {
var settings = $.extend({
maximum: 1000,
minimum: 0,
value: 1,
onChange: null
}, options);
return this.each(function (index, item) {
var min = $(item).find('>*:first-child').first();
var max = $(item).find('>*:last-child').first();
var v_span = $(item).find('>*:nth-child(2)').find('span');
var v_input = $(item).find('>*:nth-child(2)').find('input');
var value = settings.value;
$(v_input).val(value);
$(v_span).text(value);
async function increment() {
value = Number.parseInt($(v_input).val());
if ((value - 1) > settings.maximum) return;
value++;
$(v_input).val(value);
$(v_span).text(value);
if (settings.onChange) settings.onChange(value);
}
async function desincrement() {
value = Number.parseInt($(v_input).val());
if ((value - 1) < settings.minimum) return;
value--
$(v_input).val(value);
$(v_span).text(value);
if (settings.onChange) settings.onChange(value);
}
var pressTimer;
function actionHandler(btn, fct, time = 100, ...args) {
function longHandler() {
pressTimer = window.setTimeout(function () {
fct(...args);
clearTimeout(pressTimer);
longHandler()
}, time);
}
$(btn).mouseup(function () {
clearTimeout(pressTimer);
}).mousedown(function () {
longHandler();
});
$(btn).click(function () {
fct(...args);
});
}
actionHandler(min, desincrement, 100);
actionHandler(max, increment, 100)
})
}
$('body').ready(function () {
$('.spin-input').spinInput({ value: 1, minimum: 1 });
});
:root {
--primary-dark-color: #F3283C;
--primary-light-color: #FF6978;
--success-dark-color: #32A071;
--sucess-light-color: #06E775;
--alert-light-color: #a42a23;
--alert-dark-color: #7a1f1a;
--secondary-dark-color: #666666;
--secondary-light-color: #A6A6A6;
--gold-dark-color: #FFA500;
--gold-light-color: #FFBD00;
--default-dark-color: #1E2C31;
--default-light-color: #E5E5E5;
}
.fx-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.fx-colum {
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
.fx-colum.nowrap,
.fx-row.nowrap {
flex-wrap: nowrap;
}
.fx-row.fx-fill>*,
.fx-colum.fx-fill>* {
flex-grow: 1;
}
.spin-input {
border: 1px solid var(--secondary-light-color);
}
.spin-input>div:first-child {
cursor: pointer;
border-right: 1px solid var(--secondary-light-color);
}
.spin-input>div:first-child:active {
transform: translate3d(1px, 0px, 1px)
}
.spin-input>div:last-child {
flex: none;
border-left: 1px solid var(--secondary-light-color);
cursor: pointer;
}
.spin-input>div:last-child:active {
transform: translate3d(1px, 0px, 1px)
}
.icon {
font-weight: bold;
text-align: center;
vertical-align: middle;
padding: 12px;
font-size: 28px;
}
.icon.primary,
.icon.primary .ci {
color: var(--primary-dark-color);
}
.icon.reactive:hover .ci {
color: var(--primary-light-color);
}
.hidden {
display: none;
}
<script src="https://releases.jquery.com/git/jquery-3.x-git.min.js"></script>
<div class="spin-input nowrap fx-row fx-fill" >
<div class="icon reactive">
<span class="ci ci-minus">-</span>
</div>
<div class="icon">
<span>0</span>
<input type="text" class="hidden" value="0">
</div>
<div class="icon reactive">
<span class="ci ci-plus">+</span>
</div>
</div>
259)