热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CSS如果溢出则滚动/翻译元素-CSSscroll/translateYanelementifitoverflows

Isitpossibletooptionallyscrollanelementifitgrowsbeyonditsbounding-boxsize?Thatis,

Is it possible to optionally "scroll" an element if it grows beyond its bounding-box size? That is, say I have a div with overflow: hidden and a list of items within, I'd like to have a looping animation which, over the course of the animation, displays all the items in the list, "scrolling" (translateY?) to the bottom and then back to the top. Here's my best attempt at an ascii art example:

如果元素超出其边界框大小,是否可以选择“滚动”元素?也就是说,假设我有一个带溢出的div:隐藏和其中的项目列表,我想要一个循环动画,在动画过程中,它会显示列表中的所有项目,“滚动”(translateY) ?)到底部然后回到顶部。这是我在ascii艺术示例中的最佳尝试:

                             AAAAAA                             
               AAAAAA        BBBBBB        AAAAAA               
+------+      +------+      +------+      +------+      +------+
|AAAAAA|      |BBBBBB|      |CCCCCC|      |BBBBBB|      |AAAAAA|
+------+  ->  +------+  ->  +------+  ->  +------+  ->  +------+
 BBBBBB        CCCCCC                      CCCCCC        BBBBBB 
 CCCCCC                                                  CCCCCC 

I don't know a lot about css, but I've been reading up on it and css animations. I'm wondering if there's a way to "conditionally" trigger such an animation? Or, would it be possible to write the animation in such a way that it could always be running and would just do the right thing even if the whole list was visible (that is, it was short enough that it was not overflowing), and then as the list grew, the effect of the animation would actually show up?

我不太了解css,但我一直在阅读它和css动画。我想知道是否有办法“有条件地”触发这样的动画?或者,是否可以以这样的方式编写动画,即它可以始终在运行,即使整个列表都可见(也就是说,它足够短以至于没有溢出),它也会做正确的事情,并且随着列表的增长,动画的效果实际上会显现出来?

EDIT: I should clarify I'd like to achieve a "scrolling"-style of animation, rather than just instantaneously replacing the elements that are visible.

编辑:我应该澄清我想要实现一种“滚动”式的动画,而不是只是瞬间替换可见的元素。

2 个解决方案

#1


1  

Here's a pure CSS animation that I think does what you're looking for. Basically it uses Keyframes to scroll the #inner div using translateY inside the #outer div. At 50% we stop at 100% of the height of #inner (-101px so we don't scroll past the last item and it's margin), then at 100% you're back to the start. You can change the speed in which it animates in the #inner declaration.

这是一个纯CSS动画,我认为你正在寻找。基本上它使用Keyframes在#outer div中使用translateY滚动#inner div。在50%时我们停在#inner的高度的100%(-101px,所以我们不滚过最后一个项目和它的边距),然后在100%你回到开始。您可以在#inner声明中更改其动画的速度。

By using calc and percents, this solution accommodates any number of slides without having to alter the CSS. Also by adding infinite to the animation declaration you can run the slide back and forth infinitely.

通过使用计算和百分比,此解决方案可以容纳任意数量的幻灯片,而无需更改CSS。此外,通过向动画声明添加无限,您可以无限地来回运行幻灯片。

#outer {
  overflow: hidden;
  width: 100px;
  min-height: 100px;
  height: 100px;
  }
  
#inner {
  height: auto;
  -webkit-animation: scrolly 10s infinite; /* Safari 4+ */
  -moz-animation: scrolly 10s infinite; /* Fx 5+ */
  -o-animation: scrolly 10s infinite; /* Opera 12+ */
  animation: scrolly 10s infinite; /* IE 10+, Fx 29+ */
}

#inner img {
 display: block;
 margin: 0 0 1px 0;
}


@-webkit-keyframes scrolly {
  0%   { transform: translateY(0px); }
  50%  { transform: translateY(calc(-100% + 101px)); }
  100% { transform: translateY(0px); }
}
@-moz-keyframes scrolly {
  0%   { transform: translateY(0px); }
  50%  { transform: translateY(calc(-100% + 101px)); }
  100% { transform: translateY(0px); }
}
@-o-keyframes scrolly {
  0%   { transform: translateY(0px); }
  50%  { transform: translateY(calc(-100% + 101px)); }
  100% { transform: translateY(0px); }
}
@keyframes scrolly {
  0%   { transform: translateY(0px); }
  50%  { transform: translateY(calc(-100% + 101px)); }
  100% { transform: translateY(0px); }
}

#2


0  

Yes, I am not sure exactly how you are wanting the animation to look, but here is a vanilla JS/CSS slideshow animation. If you want it to slideUp/down you can simply modify the CSS animation so rather than changing display, it will change it's position by adding/subtracting a margin to the element.

是的,我不确定你想要动画的样子,但这里有一个vanilla JS / CSS幻灯片动画。如果你想滑动上/下你可以简单地修改CSS动画,而不是改变显示,它会通过向元素添加/减去边距来改变它的位置。

//Changed index so 1 is actually first image, rather than starting at 0 index
var index = 1;
var paused = false;
var slideShow = [];

for (i=0; i slideShow.length) {
    index = 1;
  }
  else if (index+n <= 0) {
    index = slideShow.length;
  }
  else {
    index += n;
  }
  showDivs();
}

function showDivs() {
  //Hide all slideShow elements, and then show only the targeted element
  for (let i=1; i<=slideShow.length; i++) {
    slideShow[i-1].style.display = "none";
  }
  slideShow[index-1].style.display = "inline";
}

1

2

3

4

5


推荐阅读
author-avatar
choojo深呼吸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有