我正在使用jquery的scrollTop函數,並無法弄清楚爲什麼它在同一項目上多次調用時跳轉。當在同一項目上多次調用scrollTop時跳轉
我已經把這個example放在一起來說明這個問題。單擊測試按鈕時,它會在指定的項目和列表中的其他位置之間切換。
$('#button').click(function() {
$('.items').scrollTop($('li.8').offset().top); // scroll to item 8
});
.items{
max-height: 80px;
overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="items large-8 columns end">
<ul class="list">
<li class="0">Almy, Joannie </li><li class="1">Auclair, Donnette </li><li class="2">Auerbach, Trinh </li><li class="3">Bement, Margeret </li><li class="4">Bryan, Willian </li><li class="5">Bullen, Tempie </li><li class="6">Coppa, Ozella </li><li class="7">Dahlin, Clementina</li><li class="8">Dahlman, Sasha </li><li class="9">Dorfman, Josef </li><li class="10">Heckert, Whitney </li><li class="11">Hendershott, Lida </li><li class="12">Holmes, Ellsworth </li><li class="13">Jaffee, Karlyn </li><li class="14">Joslyn, Nora </li><li class="15">Kiley, Patrica </li><li class="16">Lakes, Mickie </li><li class="17">Leiker, Enola</li><li class="18">Lemaire, Shanel, </li><li class="19">Lingerfelt, Graham </li><li class="20">Mangum, Maile </li><li class="21">Marcinkowski, Monica </li><li class="22">Mcraney, Allyson </li><li class="23">Meyerson, Lura </li><li class="24">Mole, Eboni </li><li class="25">Paulsen, Merrie </li><li class="26">Preble, Rivka </li><li class="27">Privette, Vonnie </li><li class="28">Quinones, Tilda </li><li class="29">Rojo, Eloy </li><li class="30">Semple, Dwana </li><li class="31">Sifuentes, Tyron </li><li class="32">Sloat, Dante </li><li class="33">Slocum, Shanna </li><li class="34">Sly, Ellamae </li><li class="35">Stolp, Courtney </li><li class="36">Till, Lucien </li><li class="37">Ulman, Jennifer </li><li class="38">Wadlow, Cassandra </li><li class="39">Zellars, Marty </li>
</ul>
</div>
<button id="button">test</button>
當我測試這一點,產生相同的結果 – Jess
@Jess你必須做出相對父UL位置。 – Burimi
@Jess檢查這個小提琴http://jsfiddle.net/wgqoh1b3/28/ – Burimi