热门标签 | 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


推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • CSS3 animation动画属性详解及用法
    本文详细介绍了CSS3 animation动画的各种属性及用法,包括关键帧动画、动画名称、动画时间、动画曲线、动画延迟、动画播放次数、动画状态和动画前后的状态等。通过本文的学习,读者可以深入了解CSS3 animation动画的使用方法。 ... [详细]
  • 目录浏览漏洞与目录遍历漏洞的危害及修复方法
    本文讨论了目录浏览漏洞与目录遍历漏洞的危害,包括网站结构暴露、隐秘文件访问等。同时介绍了检测方法,如使用漏洞扫描器和搜索关键词。最后提供了针对常见中间件的修复方式,包括关闭目录浏览功能。对于保护网站安全具有一定的参考价值。 ... [详细]
  • 本文整理了常用的CSS属性及用法,包括背景属性、边框属性、尺寸属性、可伸缩框属性、字体属性和文本属性等,方便开发者查阅和使用。 ... [详细]
  • 开发笔记:对称加密详解,以及JAVA简单实现
     (原)常用的加密有3种1、正向加密,如MD5,加密后密文固定,目前还没办法破解,但是可以能过数据库撞库有一定概率找到,不过现 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了css回到顶部按钮相关的知识,希望对你有一定的参考价值。 ... [详细]
  • html结构 ... [详细]
  • 转自:http:www.phpweblog.netfuyongjiearchive200903116374.html一直对字符的各种编码方式懵懵懂懂,什 ... [详细]
  • 刚开始crousera上学习<algorithmspart1>但对JAVA实在是不熟。******************************************** ... [详细]
  • pyecharts 介绍
    一、pyecharts介绍ECharts,一个使用JavaScript实现的开源可视化库,可以流畅的运行在PC和移动设备上,兼容当前绝大部 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • java io换行符_Java IO:为什么从stdin读取时,换行符的数字表示出现在控制台上?...
    只是为了更好地理解我在讲座中听到的内容(关于Java输入和输出流),我自己做了这个小程序:publicstaticvoidmain(String[]args)thro ... [详细]
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社区 版权所有