I currently have a list of items which I am allowing users to re-arrange; please check out the snippet.
The re-arrange is working well but I have a fixed height on the list box and I would like for the highlighted item to stay in sight while it gets moved up and down in the list.
To re-create my issue simply hit the Move Down button a few times and Item 2 will go out of view. You must scroll in order to see it again. This behavior is not desired.
Any guidance will be greatly appreciated!
$('.move-up').on('click', function(){
var $active = $('.active');
$active.insertBefore($active.prev('div'));
scroll_to_element();
});
$('.move-down').on('click', function(){
var $active = $('.active');
$active.insertAfter($active.next('div'));
scroll_to_element();
});
function scroll_to_element(){
$('.list').animate({
scrollTop: $(".active").offset().top
}, 0);
}
.list{
overflow:scroll;
height:200px;
}
.list div{
padding:10px;
}
.list div.active {
background-color: #ffff7f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="list">
<div>Item 1</div>
<div class="active">Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
<div>Item 6</div>
<div>Item 7</div>
<div>Item 8</div>
<div>Item 9</div>
<div>Item 10</div>
</div>
<button type="button" class="move-up">Move Up</button>
<button type="button" class="move-down">Move Down</button>