我想滚动到一个元素作为whatsapp搜索

用户单击search (here it is bottom)时应滚动到最后一个元素

即ID为chat-7的元素。
当单击up button时,应滚动到chat-6,然后滚动到chat-5 ..依此类推。

如果单击down button,如果不是最后一项,则应向下滚动。



function scroll(id){
    console.log(id);
    $(".container").animate(
        {
            scrollTop: $("#"+id).offset().top
        },
        "fast"
    );
}





完整的代码在这里
http://jsfiddle.net/p3kar5bb/231/

不幸的是,这段代码无法正常工作

最佳答案

因为您的div .container没有滚动条,所以您可以做两件事。

1.动画身体

$("body").animate({ scrollTop: $("#"+id).parent('.item').offset().top}, "fast");


Demo

2.将最大高度设置为div

.container{
   height: 100%;
   overflow-y: scroll;
   max-height:200px;
  }


Demo

07-24 17:03
查看更多