[英]Scroll element to the top of the container onclick
我的部分包含了somes元素。
我需要像1rdiv一樣向上滾動數字5。 與典型的滾動頁面類似。
我也需要設置動畫 。 並將其向上滾動到更高位置 。 像第一個 我不在乎上層div是否隱藏
<section>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</section>
<button> Click and show Div 5 up</button>
JS
$("button").click(function() {
$('div:eq(5)').animate({scrollTop: $('div:ep(0)'}, 3000);
});
我把代碼留給你->
Javascript數組是從零開始的。 因此,在這種情況下,第5個元素實際上是4。(0-1-2-3-4)
嘗試這個:
$("button").click(function() {
var offset = $("div:eq(4)").offset().top;
$('section').scrollTop(offset, 2000);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.