[英]Scroll to top of div when it is not displayed
我的div充满了预搜索结果。 当必须提前输入的输入失去焦点时,我希望将具有提前输入结果的div滚动到最高位置。
我正在使用typeahead.js ,所以我目前有:
$('input[name="customer"]').on('typeahead:closed', function(){
$('div.tt-dataset-customers').scrollTop(0);
});
但是,div永远不会滚动。 考虑到这是由于div被隐藏而引起的,我也尝试了:
$('input[name="customer"]').on('typeahead:open', function(){
$('div.tt-dataset-customers').scrollTop(0);
});
这也不起作用。
有没有一种方法可以在未显示的div上设置滚动位置?
小提琴展示正在发生的事情: http : //jsfiddle.net/tTdF4/
编辑v3
因此,我为此添加了console.log
,但从未触发。
$('input[name="customer"]').on('typeahead:closed', function(){
console.log('test');
});
因此,我没有使用on()
拆分typeahead()
,而是使用jQuery链,现在它触发了typeahead:closed
。 不要问我为什么它没有触发,仍然没有弄清楚:(。
无论如何,当您输入a
,向下滚动,单击外部然后在输入上单击返回时,这将起作用(在chrome v29上进行了测试),列表将被备份。
$('input[name="customers"]').typeahead({
local: ['a','ab','ac','ad','ae','af',
'ag','ah','ai','aj','ak','al',
'am','an','ao','ap','aq','ar',
'as','at','au','av','aw','ax',
'ay','az'
],
limit: 20,
name: 'customers',
rateLimitWait: 100,
ttl: 300
}).on('typeahead:closed keypress', function(){
console.log('test');
$('div.tt-dataset-customers').animate({
scrollTop: 0
}, 900);
});
ps:至少在小提琴上,jQuery 2.X和typeahead会在IE9的两个脚本上产生错误(请参阅控制台),如果使用1.9 (edge)
则它将起作用。
V3:在监听的事件中添加了keypress
,因此每当有人键入a
,它就会尝试向上滚动(例如,输入a
,后退空格和b
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.