javascript - How do I use jQueryJS to scroll a listbox to the top - Stack Overflow

JSFiddleIn the fiddle example I have several attempts to scroll a listbox to the top (or any point in t

JSFiddle

In the fiddle example I have several attempts to scroll a listbox to the top (or any point in the list) after programmatically selecting an item. I have not been able to find a way that works or an example on SO that shows how to successfully do this. Below is the code or you can see it on JSFiddle. Can someone point me in the right direction please.

<select id='focusScrollTest' multiple='multiple'>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

$(document).ready(function(){
//return the current scroll position before selecting the last option
var x = $("#focusScrollTest").scrollTop();

//Displays 0
//alert(x);

//Select the last option value
$("#focusScrollTest option[value='9']").attr('selected', 'selected');

//return the current scroll position after selecting the last option
var y = $("#focusScrollTest").scrollTop();

//Displays 85
//alert(y);

///
///   These do not change the scroll
///
//$("#focusScrollTest option[value='1']").scrollTo();
//$("#focusScrollTest option[value='1']").scrollTo(0);
//$("#focusScrollTest").scrollTo(x);
//$("#focusScrollTest").scrollTo(0);
//$("#focusScrollTest").scrollTo(1);
//$("#focusScrollTest").scrollTop = 0;
//$("#focusScrollTest option[value='1']").scrollTop = 0;
//$("#focusScrollTest").focus();
//$("#focusScrollTest option[value='1']").focus();
//$("#focusScrollTest").blur().focus();
//$("#focusScrollTest option[value='1']").blur().focus();

//This is very interesting, even though it is done last the box
//still scrolled to the end
//$("#focusScrollTest option[value='1']").attr('selected', 'selected');

});

JSFiddle

In the fiddle example I have several attempts to scroll a listbox to the top (or any point in the list) after programmatically selecting an item. I have not been able to find a way that works or an example on SO that shows how to successfully do this. Below is the code or you can see it on JSFiddle. Can someone point me in the right direction please.

<select id='focusScrollTest' multiple='multiple'>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
    <option value="9">9</option>
</select>

$(document).ready(function(){
//return the current scroll position before selecting the last option
var x = $("#focusScrollTest").scrollTop();

//Displays 0
//alert(x);

//Select the last option value
$("#focusScrollTest option[value='9']").attr('selected', 'selected');

//return the current scroll position after selecting the last option
var y = $("#focusScrollTest").scrollTop();

//Displays 85
//alert(y);

///
///   These do not change the scroll
///
//$("#focusScrollTest option[value='1']").scrollTo();
//$("#focusScrollTest option[value='1']").scrollTo(0);
//$("#focusScrollTest").scrollTo(x);
//$("#focusScrollTest").scrollTo(0);
//$("#focusScrollTest").scrollTo(1);
//$("#focusScrollTest").scrollTop = 0;
//$("#focusScrollTest option[value='1']").scrollTop = 0;
//$("#focusScrollTest").focus();
//$("#focusScrollTest option[value='1']").focus();
//$("#focusScrollTest").blur().focus();
//$("#focusScrollTest option[value='1']").blur().focus();

//This is very interesting, even though it is done last the box
//still scrolled to the end
//$("#focusScrollTest option[value='1']").attr('selected', 'selected');

});

Share Improve this question asked Oct 21, 2013 at 17:32 JabberwockyDepilerJabberwockyDepiler 3,4002 gold badges43 silver badges54 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 9

Try this:

http://jsfiddle/jLzpU/

$('#focusScrollTest').scrollTop(0); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744190012a4562390.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信