【发布时间】:2018-01-24 23:19:04
【问题描述】:
我有一个ul 和一个max-height 和overflow-y: auto。
当用户输入足够多的li 元素时,ul 开始滚动,但我希望最后一个带有form 的li 始终存在并可供用户查看。
我尝试实现一个如下所示的scrollToBottom 函数:
scrollToBottom() {
this.formLi.scrollIntoView({ behavior: 'smooth' });
}
但这只会让ul 跳到屏幕顶部,并显示li 及其中的表单作为唯一可见的东西。
有没有更好的方法来做到这一点?我发现的答案往往有点老,并使用 ReactDOM。谢谢!
CSS:
.prompt-box .options-holder {
list-style: none;
padding: 0;
width: 95%;
margin: 12px auto;
max-height: 600px;
overflow-y: auto;
}
HTML:
<ul className='options-holder'>
{
this.state.items.map((item, index) => (
<li key={item.id} className={`option ${index === 0 ? 'first' : ''}`}>
<div className='circle' onClick={this.removeItem} />
<p className='item-text'>{ item.text }</p>
</li>
))
}
<li key={0} className={`option form ${this.state.items.length === 0 ? 'only' : ''}`} ref={el => (this.formLi = el)}>
<div className='circle form' />
<form className='new-item-form' onSubmit={this.handleSubmit}>
<input
autoFocus
className='new-item-input'
placeholder='Type something and press return...'
onChange={this.handleChange}
value={this.state.text}
ref={(input) => (this.formInput = input)} />
</form>
</li>
</ul>
【问题讨论】:
-
在这种情况下,也许你将最后一个li移出ul,并添加另一个包装器
-
我为什么要这样做而不是滚动 ul?
标签: javascript html css reactjs