3 回答
TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
只需添加您的 CSS。direction: rtl;input
const input = document.querySelector('input');
const button = document.querySelector('button');
button.addEventListener('click', function() {
input.value += Math.floor(Math.random() * 10)
});
html, body {
background: black;
}
input {
width: 15rem;
height: 3rem;
margin: 2rem .4rem 2rem .4rem;
font-size: 3rem;
text-align: right;
color: white;
background-color: black;
outline: none;
transition: all .1s ease;
direction: rtl;
}
<input type='text' readonly='true' value='0'>
<button>CLICK ME</button>
更新
根據(jù)您的最后一個(gè)鏈接 https://codepen.io/ssmkhrj/pen/ExPjJab
您應(yīng)該在代碼中更改此設(shè)置
<div class="display-cover">
<div class="display"></div>
</div>
和斷續(xù)器
.display-cover{
width: 19.2rem;
height: 3rem;
margin: 0 .4rem 1.5rem .4rem;
text-align: right;
font-size: 3rem;
white-space: nowrap;
padding-bottom: 0.5rem;
overflow-y: hidden;
overflow-x: scroll;
scroll-snap-type: x mandatory; /* this will do the magic for parent */
}
.display{
height: 3rem;
display: inline-block;
scroll-snap-align: end; /* this will do the magic for child */
}
更多關(guān)于這里的信息 在這里 https://css-tricks.com/practical-css-scroll-snapping/scroll-snap
TA貢獻(xiàn)2039條經(jīng)驗(yàn) 獲得超8個(gè)贊
使用 setInterval 會(huì)使輸入保持專(zhuān)注,因此它不會(huì)來(lái)回跳轉(zhuǎn)。雖然當(dāng)你在輸入或按鈕元素之外單擊時(shí),它仍然有點(diǎn)錯(cuò)誤,但這可以完成跳躍的工作。
const input = document.querySelector('input');
const button = document.querySelector('button');
button.addEventListener('click', function() {
// concatonate random number to input value
input.value += Math.floor(Math.random() * 10)
});
setInterval(function(){
input.focus();
});
html, body {
background: black;
}
input {
width: 15rem;
height: 3rem;
margin: 2rem .4rem 2rem .4rem;
font-size: 3rem;
text-align: right;
color: white;
background-color: black;
outline: none;
}
<input type='text' readonly='true' value='0' onfocus="this.value = this.value;" autofocus>
<button>CLICK ME</button>
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超5個(gè)贊
將屬性添加到輸入字段,它將從右向左流動(dòng)文本。W3學(xué)校dir="rtl"
const input = document.querySelector('input');
const button = document.querySelector('button');
button.addEventListener('click', function() {
// concatonate random number to input value
input.value += Math.floor(Math.random() * 10)
// focus input to scroll to end
input.focus();
});
html, body {
background: black;
}
input {
width: 15rem;
height: 3rem;
margin: 2rem .4rem 2rem .4rem;
font-size: 3rem;
text-align: right;
color: white;
background-color: black;
outline: none;
}
<input type='text' readonly='true' value='0' dir="rtl">
<button>CLICK ME</button>
添加回答
舉報(bào)
