简体   繁体   English

在jQuery中上下滚动到元素

[英]scroll to elements up and down in jquery

i want to do scroll to an element as whatsapp search 我想滚动到一个元素作为whatsapp搜索

when user clicked search (here it is bottom) should scroll to last element 当用户单击search (here it is bottom)应滚动到最后一个元素

ie; 即; element with id chat-7 . ID为chat-7元素。 and when click up button should scroll to chat-6 then chat-5 ..and so on. 然后单击up button应滚动到chat-6然后滚动到chat-5 ..依此类推。

if click down button it should scroll to down if it is not last item. 如果单击down button ,则它不是最后一项,应向下滚动。

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

full code here http://jsfiddle.net/p3kar5bb/231/ 完整代码在这里http://jsfiddle.net/p3kar5bb/231/

unfortunately this code is not working properly 不幸的是,这段代码无法正常工作

Perform the animate on the body instead of the .container 在身体上而不是.container上执行动画

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

}

Also set the 同时设置

var pointedPosition=0;

Fiddle http://jsfiddle.net/p3kar5bb/234/ 小提琴http://jsfiddle.net/p3kar5bb/234/

Because your div .container doesn't have scroll bar so you can do two things. 因为您的div .container没有滚动条,所以您可以做两件事。

1. Animate Body 1.动画身体

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

Demo 演示

2. Set max-height to div 2.将最大高度设置为div

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

Demo 演示

$('html, body').animate({
scrollTop: $("#"+id).offset().top
}, 500);

Code that is given in http://jsfiddle.net/p3kar5bb/231/ is fine, just change the animate() as above. http://jsfiddle.net/p3kar5bb/231/中给出的代码很好,只需如上所述更改animate()即可。

Just tried – $('html') does not work in Chrome and $('body') does not work in Firefox, so $('html, body') is needed. 刚刚尝试过-$('html')在Chrome中不起作用,而$('body')在Firefox中不起作用,因此需要$('html,body')。 And also calling .stop() is a good thing. 而且,调用.stop()也是一件好事。

Assuming you have the element id from buttons click event , try to change the scroll function code to this: 假设您具有按钮click event的元素ID,请尝试将滚动功能代码更改为此:

 function scroll(id){
      $('html, body').animate({ 
      scrollTop: $("#"+id).offset().top
        }, 2000);
}

And I have update your code here http://jsfiddle.net/p3kar5bb/231/ . 而且我在这里http://jsfiddle.net/p3kar5bb/231/更新了您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM