简体   繁体   English

连续滚动元素到父元素的底部和顶部

[英]Continuously scroll an element to bottom and back to top of a parent element

I am trying to automatically .animate the .scrollTop of a <div> contained within another <div> . 我想自动.animate.scrollTop<div> 包含在另一个<div> I have done this so I could hide the scroll-bar thus only display the text. 我这样做是为了隐藏滚动条,因此仅显示文本。 Therefore, I know how to scroll that particular <div> with JQuery as such: 因此,我知道如何使用JQuery这样滚动特定的<div>

$("#div").animate({ scrollTop: "+=5" }, 200);

But what I am trying to do is to continuously .animate the content until it reaches the bottom of that particular <div> and then animates back to the top of the <div> ( .scrollTop: '0px' ). 但是我想做的是连续 .animate对内容进行动画处理,直到到达特定<div>的底部,然后再动画化回<div>的顶部( .scrollTop: '0px' )。 THere is where I am struggling with. 这是我在努力的地方。

  • I am currently pulling content into a <div> which the result of a query. 我目前正在将内容放入查询结果的<div>中。 Therefore, I do not know the full length of the content that will be placed of that <div> . 因此,我不知道该<div>将放置的内容的全长。
  • The <div> that contains the content is within another <div> and I don't think the method to detect the bottom of the <div> works correctly (or perhaps I am doing something wrong). <div>包含内容中的另一个<div>我不认为法检测底部的<div>正常工作(也许我做错了什么)。

     var div = $(this); if (div[0].scrollHeight - div.scrollTop() == div.height()) 

I was wondering if someone can give me a hand with this. 我想知道是否有人可以帮我这个忙。 Since I have not seen a particular approach like this in this forum or by Googleing for it. 由于我没有在本论坛或Googleing中看到类似的特定方法。

So pretty much what I have is this: 我几乎拥有的是:

if ($('#resultProviders').scrollTop() >= $('#resultProviders').innerHeight()) {
    $('#resultProviders').animate({ scrollTop: "0px" }, 800);
} else {
$('#resultProviders').animate({scrollTop: '+=30px'}, 500);
};

I have also provided a Fiddle for this. 我还为此提供了一个小提琴

https://jsfiddle.net/trinkermedia/ebhydbp3/5/ https://jsfiddle.net/trinkermedia/ebhydbp3/5/

Many thanks. 非常感谢。

I'd use a scroll function with a parameter, like shown in this fiddle . 我将使用带有参数的滚动功能,如该小提琴所示。

function _scroll(goDown) {
    goDown = goDown == undefined ? true: goDown;
    var frame = $('#resultProviders'),
        content = $('#resultProviders ul'),
        scrollTo;
    if (goDown) {
        scrollTo = content.outerHeight() - frame.outerHeight();
    } else {
        scrollTo = 0
    }
    frame.animate({
        scrollTop: scrollTo
    }, {
        duration: 2000,
        complete: function() {_scroll(!goDown)}
    });
}

Things are so simple with GSAP , here is your fiddle: jsFiddle . 使用GSAP ,事情是如此简单,这是您的小提琴: jsFiddle

JavaScript: JavaScript:

TweenMax.to('#resultProviders', 2, { scrollTo: { y: 'max' }, repeat: -1, yoyo: true, ease: Power2.easeInOut });

Apologies if this wasn't you were looking for or if you were not interested in solutions utilising anything else other than jQuery. 抱歉,如果不是您要找的东西,或者您对使用jQuery以外的其他解决方案不感兴趣。

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

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