使两个 div 朝同一方向一个接一个地进行动画处理

发布于 2024-12-27 05:19:49 字数 2552 浏览 2 评论 0原文

我已经搜索过这个,但给出的最佳答案是“在您的项目周围包裹一个 div,然后将您的动画/滚动添加到其中”。我同意,这将是解决我的问题的一个快速简单的解决方案,但是,不幸的是,我不能在我的 div 周围包裹一个 div,因为我有一个特定的问题,需要我为每个 div 单独设置动画。我会详细说明...

我有一个名为“highlight”的 div,其中有一些分两行的图块,每行 5 个图块形成一个网格。这就是我所说的“功能区”,它是页面底部的一个面板,它在所有突出显示图块周围有一个容器 div,具有指定的宽度并隐藏了溢出,允许我的内容在以下情况下加载和隐藏并滑动。使用寻呼机上的“上一个”或“下一个”按钮。我的代码如下所示:

    <div class="ribbon-display">
      <div class="display-container clearfix">
       <div id="highlight">
         <div class="top-row clearfix">                                     
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
         </div>
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
       </div>
       <div id="highlight" class="new-highlight">
         <div class="top-row clearfix">                                     
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
         </div>
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
       </div>
      </div>            
    </div>

我正在做的是一次加载 10 个图块。然后,当单击“下一步”按钮时,将进行 ajax 调用以拉入接下来的 10 个图块。然后将它们作为新的“突出显示”div 附加到我的容器 div 中的内容。因此,我现在有两个“突出显示”div,其中包含 10 个彼此相邻的图块。然后,我可以使用 jquery 'animate' 函数将它们滚动到左侧,以更改元素的“左侧”位置。然而,当我这样做时,第一个突出显示面板比它旁边的下一个突出显示面板向左滑动得更快。这意味着滑动中有间隙,而不是彼此相邻的滑动毛绒。如果所有幻灯片内容图块都已预加载并且只是随幻灯片一起显示/隐藏,那么您该怎么办...

所以我的问题是,如何让这两个 div 同时向左滑动,一个直接在其之后另一个以同样的速度?正如我所说,我已经探索过使用 animate 函数并更改左侧位置,就像这样......

$('#highlight').animate(
  {left:'-=971'},
  30000
);
$('.new-highlight').animate(
  {left:'-=971'},
  200
);

并且还尝试更改 animate 函数中的 leftScroll 参数,并更改每个突出显示 div 的速度以尝试减慢第一个 div 的速度假装动画看起来很光滑。有人有尝试这样做的经验吗?我可以使用“scrollLeft”功能吗?

任何帮助将不胜感激!预先感谢大家!

克雷格

I have searched for this, but the best answer that has been given is 'Wrap a div around your items and then add your animate/scroll to that'. I agree, that would be a quick easy solution to my problem, however, i can't, unfortunately, wrap a div around my div's as i have a specific problem which requires me to animate each of my div's separate. I'll elaborate...

I have a div called 'highlight' which has some tiles in it in two rows, with 5 tiles in each row to form a grid. This is in what i call a 'Ribbon' which is a panel at the bottom of my page which has a container div around all the highlight tiles with a specified width with overflow hidden, allowing my content to be loaded and hidden and slide across when using the 'previous' or 'next' buttons on my pager. My code looks something like the following:

    <div class="ribbon-display">
      <div class="display-container clearfix">
       <div id="highlight">
         <div class="top-row clearfix">                                     
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
         </div>
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
       </div>
       <div id="highlight" class="new-highlight">
         <div class="top-row clearfix">                                     
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
         </div>
      <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
      <div class="ribbon-tile"></div>
          <div class="ribbon-tile"></div>
       </div>
      </div>            
    </div>

What i am doing is loading 10 tiles at a time. Then, when the 'next' button is clicked, an ajax call is made to pull in the next 10 tiles. These are then appended to my content within my container div as a new 'highlight' div. Thus meaning, i now have two 'highlight' divs containing 10 tiles adjacent to each other. I can then scroll these both across to the left using the jquery 'animate' function to alter the 'left' position of the elements. When i do that however, the first highlight panel slides faster to the left than the next highlight panel next to it. Meaning that there is a gap in the slide, rather than sliding plush next each other. Much how can you do if all your slide content tiles are preloaded and just shown/hidden with the slide....

So my question is, how can i get these two div's to slide to the left at the same time, one directly after the other at the same speed? As i stated, i have explored using the animate function and altering the left position, like so....

$('#highlight').animate(
  {left:'-=971'},
  30000
);
$('.new-highlight').animate(
  {left:'-=971'},
  200
);

And also tried altering the leftScroll paramater in the animate function, and altering the speeds of each highlight div to try and slow the first one down to fake the animation looking slick. Anyone had experience with trying to do this? Can i use the 'scrollLeft' function?

Any help would be greatly appreciated here! thanks in advance guys!

Craig

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蔚蓝源自深海 2025-01-03 05:19:49

您可以使用延迟功能进行菊花链连接。

$('#highlight').animate({left:'-=971'},30000);
$('.new-highlight').delay(30000).animate({left:'-=971'},200);

请注意,这是毫秒...您制作动画需要 30 秒! :)

“我怎样才能让这两个div同时向左滑动,一个以相同的速度紧接着另一个?”

同时还是先后?这是两件不同的事情:)

这是一个小提琴,因为我不确定你的动画意图:)
http://jsfiddle.net/NZkf8/

You can daisy chain by using the delay function.

$('#highlight').animate({left:'-=971'},30000);
$('.new-highlight').delay(30000).animate({left:'-=971'},200);

Please note that it's milliseconds... you're making the animation taking 30 seconds! :)

"how can i get these two div's to slide to the left at the same time, one directly after the other at the same speed?"

At the same time or after each other? these are two different things :)

Here's a fiddle as i'm unsure about your animation intentions :)
http://jsfiddle.net/NZkf8/

赠意 2025-01-03 05:19:49

您可以将“完成”函数传递给 animate 方法,该函数将在第一个完成时调用。

$('#highlight').animate({left:'-=971'},30000, function(){
  $('.new-highlight').animate(
    {left:'-=971'},
    200
  );
});

You can pass a 'finished' function to the animate method, which will be called when the first finishes.

$('#highlight').animate({left:'-=971'},30000, function(){
  $('.new-highlight').animate(
    {left:'-=971'},
    200
  );
});
高速公鹿 2025-01-03 05:19:49

我很难准确理解您正在寻找的内容...但是您可以通过设置一个间隔来实现“跟随”的效果,使第二个 div 动画到第一个 div 的位置。 O_o

像这样:

var follow = setInterval(function(){
  $('.new-highlight').animate(
    {left : $('#highlight').position().left},
    1000
  );
},1000)

$('#highlight').animate(
  {left:'-=971'},
  30000,
  function(){
    window.clearInterval(follow)
  }
);

您可以尝试使用两个 1000。如果你将第二个移动到 500,那么每一秒,第二个移动都会移动两个移动距离的一半。

我给出了自己的答案,因为我的策略非常不同,而且我的
之前的帖子对于寻求帮助的人来说仍然有效
关于这个话题。

I'm having a hard time understanding exactly what you're looking for... but its possible that you can achieve this effect of 'following' by setting an interval that causes the second div to animate to the location of the first div.. O_o

like this:

var follow = setInterval(function(){
  $('.new-highlight').animate(
    {left : $('#highlight').position().left},
    1000
  );
},1000)

$('#highlight').animate(
  {left:'-=971'},
  30000,
  function(){
    window.clearInterval(follow)
  }
);

You might try playing with both 1000's. if you make the second one 500, then every second, the second one will travel half the distance between the two wen it starts to move.

I made this its own answer because my tactic is very different, and my
previous post could still be valid for people who are looking for help
on this topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文