简体   繁体   English

如何使用jQuery在DIV中连续滚动内容?

[英]How to continuously scroll content within a DIV using jQuery?

Aim 目标

The aim is to a have a container DIV with a fixed height and width and have the HTML content within that DIV automatically scroll vertically continuously. 目标是拥有一个具有固定高度和宽度的容器DIV,并且该内容中的HTML内容会自动连续垂直滚动。

Question Basically I've created the code below using jQuery to scroll (move) the child DIV vertically upwards until its outside the bounding parent box where the animation then completes which triggers an event handler which resets the position of the child DIV and starts the process again. 问题基本上我使用jQuery创建了下面的代码,将子DIV垂直向上滚动(移动),直到它在动画然后完成的边界父框之外,触发了一个事件处理程序,它重置子DIV的位置并启动进程再次。

This works fine, so the content scrolls up leaving a blank space and then starts from the bottom again and scrolls up. 这样工作正常,因此内容向上滚动留下一个空白区域,然后再从底部开始向上滚动。

The problem I have is that the requirements for this is for the content to appear as if it was continuously repeating, see below diagram to better explain this, is there a way to do this? 我遇到的问题是,对此内容的要求就是内容看起来好像在不断重复,请参见下图以更好地解释这一点,有没有办法做到这一点? (I don't want to use 3rd party plug ins or libraries other than jQuery): (我不想使用第三方插件或jQuery以外的库):

替代文字
(source: cameroncooke.com ) (来源: cameroncooke.com

What I have so far 到目前为止我有什么

The HTML: HTML:

<div id="scrollingContainer">

  <div class="scroller">

    <h1>This is a title</h1>

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at orci mi, id gravida tellus. Integer malesuada ante sit amet enim pulvinar congue. Donec pulvinar dolor et arcu posuere feugiat id et felis.</p>

    <p>More content....</p>   

  </div>

</div>

The CSS: CSS:

#scrollingContainer{
  height: 300px;
  width: 300px;
  overflow: hidden;
}

#scrollingContainer DIV.scroller{
  position: relative;
}

The JavaScript: JavaScript:

/**
* Scrolls the content DIV
*/
function scroll() {

  if($('DIV.scroller').height() >  $('#scrollingContainer').height()) {

    var t = $('DIV.scroller').position().top + $('DIV.scroller').height();

    /* Animate */
    $('DIV.scroller').animate(
    {
      top: '-=' + t + 'px'
    }
    , 4000, 'linear', animationComplete);
  }
}

function animationComplete() {
  $(this).css('top', $('#scrollingContainer').height());
  scroll();
}

You'll need to duplicate your content element and align them so that they're vertically next to each other, and scroll them in tandem. 您需要复制内容元素并对齐它们,使它们彼此垂直相邻,并将它们串联滚动。 Your current scrolling should work, the jump will be invisible because it should jump from the top of the bottom element to the top of the top element, that is, to a same-looking part. 您当前的滚动应该有效,跳转将是不可见的,因为它应该从底部元素的顶部跳到顶部元素的顶部,也就是说,跳到相同的部分。 I'd put both copies of the content (you can just .clone it to get the other copy) in a container and scroll that, that way you don't have to worry about moving two elements. 我将内容的两个副本(你可以只是.clone来获取另一个副本)放在一个容器中并滚动它,这样你就不用担心移动两个元素了。

If you want to really optimize it, you could only display the top part (enough to hide the jump) of the content in the bottom element, but unless your content is really complex and heavy, it's not worth the effort. 如果你想真正优化它,你只能在底部元素中显示内容的顶部(足以隐藏跳转),但除非你的内容非常复杂和沉重,否则不值得努力。

You want the content to "auto-repeat" and scroll forever? 您希望内容“自动重复”并永久滚动? You should be able to add a new DIV below the text, and copy that text into the new DIV. 您应该能够在文本下方添加新的DIV,并将该文本复制到新的DIV中。 Do this based on scroll position, removing the DIV above when it goes out of view. 根据滚动位置执行此操作,当它离开视图时移除上面的DIV。 Basically you're just copying the text, pushing a new DIV to the bottom of the "stack" and popping it off the top when it's out of view. 基本上你只是复制文本,将新的DIV推到“堆栈”的底部,当它不在视野时从顶部弹出。

Simply put you'll need two divs which are larger than the scroll box and you'll need to move move the one that's not visible below the one that is. 简单地说,你需要两个大于滚动框的div,你需要移动一个在那个下面看不到的那个。 If the two are exactly the same it shouldn't be noticeable. 如果两者完全相同,那就不应该引人注意了。

Try this: 试试这个:

$('.upBut').bind('click',function(){
    $('.articleWrapper').prepend($('.article:last')).children('.article:first').css({display:'none'}).show('slow');
});
$('.downBut').bind('click',function(){
    //$('.articleWrapper').append($('.article:first')).children('.article:last').css({display:'none'}).show('slow');
    $('.article:first').hide('slow',function(){$(this).appendTo('.articleWrapper').show('slow');});
});
$('.upBut').hover(function(){$(this).css("background-image", "url(images/up_green.png)");},function(){$(this).css("background-image", "url(images/up_red.png)");});
$('.downBut').hover(function(){$(this).css("background-image", "url(images/down_green.png)");},function(){$(this).css("background-image", "url(images/down_red.png)");});

Working example can be seen here: http://www.timeswellness.com/index.aspx?page=others&rightnav=false&do=CancerDay 在这里可以看到工作示例: http//www.timeswellness.com/index.aspx?page =他们&rightnav = false& do = CancerDay

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

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