简体   繁体   English

如何结合两个CSS动画

[英]How to Combine Two CSS Animations

I'm trying to replicate the menu design used by: 我正在尝试复制以下人员使用的菜单设计:

https://www.pandaexpress.com/menu/appetizers https://www.pandaexpress.com/menu/appetizers

I think I am at the point where I need to use javascript/jquery, but I'm very new and unsure of my next step. 我认为我正处于需要使用javascript / jquery的地步,但是我很新,不确定下一步。 When the user hovers over an image the image seems to slide into focus and display information below. 当用户将鼠标悬停在图像上方时,图像似乎会滑入焦点并在下面显示信息。 I have tried to isolate this into separate actions, modeled by the following jfiddles: 我试图将其隔离为单独的动作,由以下jfiddles建模:

https://jsfiddle.net/Lrqu8L0q/3/ (enlarges and focuses image on hover) https://jsfiddle.net/4ud7wnop/1/ (slides down to reveal text on hover) https://jsfiddle.net/Lrqu8L0q/3/ (放大图像并将其聚焦在悬停上) https://jsfiddle.net/4ud7wnop/1/ (向下滑动以显示悬停时的文本)

I tried combining them but when I add the 我尝试合并它们,但是当我添加

<p>This text is displayed on a downward slide after hover</p>

to the first jfiddle the paragraph isn't hidden. 到第一个小节,该段落没有被隐藏。

Now I'm having trouble trying to think of a way to combine the both. 现在,我很难尝试考虑将两者结合的方法。 I think I need to use javascript/jquery to create a function that applies the slide down to reveal text after the image enlarge has been completed. 我想我需要使用javascript / jquery创建一个函数,该函数在图像放大完成后将幻灯片向下应用以显示文本。 I'm very new to web design as a whole and am especially shaky with javascript and jquery. 我对整个Web设计是一个新手,对javascript和jquery尤其不满意。

I was wondering if I could write a function that checks if the image has expanded on enlarge yet, and after it's reached a desired height/width run the text slide function. 我想知道是否可以编写一个函数来检查图像是否在放大时扩展,并且在达到所需的高度/宽度后运行文本幻灯片功能。 Really not sure how to do this.. could someone point me in the correct direction? 真的不知道该怎么做..有人可以指出我正确的方向吗?

I merged them together here: https://jsfiddle.net/danbovey/53ya31gm/ 我在这里将它们合并在一起: https : //jsfiddle.net/danbovey/53ya31gm/

The main problem was, you need the image to be larger to cover over any detail text you may have. 主要问题是,您需要将图像放大以覆盖您可能拥有的任何详细文本。

I have commented on most changes I added in the fiddle. 我评论了我在小提琴中添加的大多数更改。 But here's the key parts: 但这是关键部分:

I renamed the bg element to tile , it seemed more appropriate. 我将bg元素重命名为tile ,这似乎更合适。 Instead of working with height for the .slide-down class, I created a transform on the details div when the tile is hovered. .slide-down贴悬停时,我没有在.slide-down类中使用height ,而是在.slide-down div上创建了一个转换。

.tile:hover .details {
    transform: translateY(150%);
}

You can learn about CSS transforms here: http://css3.bradshawenterprises.com/transforms/ 您可以在此处了解CSS转换: http//css3.bradshawenterprises.com/transforms/

The percentage of the transform can be calculated as the height of img / height of details - 300px / 200px = 150% 转换的百分比可以计算为img的高度/细节的高度300px / 200px = 150%

To create the growing effect, a pseudo :before element adds a white area the same size of the image before the tile, and when hovered, grows to 25px around each edge. 为了产生增长效果,伪:before元素在图块:before添加了一个与图像大小相同的白色区域,将其悬停时,在每个边缘周围增长到25px。 And an identical element is added to the details div. 并将相同的元素添加到详细信息div。

.tile:before {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: #FFF;
  transition: all 0.4s ease;
  content: '';
}

.tile:hover:before {
  top: -25px;
  left: -25px;
  width: calc(100% + 50px);
  height: calc(100% + 50px);
}

Is this what you want here? 这是您想要的吗? https://jsfiddle.net/Lrqu8L0q/9/ https://jsfiddle.net/Lrqu8L0q/9/

 .thumbnail{ width: 100px; height: 100px; display:block; z-index:999; cursor: pointer; -webkit-transition-property: all; -webkit-transition-duration: 0.3s; -webkit-transition-timing-function: ease; margin: 0 auto; position: relative; top: 50%; transform: translateY(-50%); border-radius: 2px; } .bg:hover { transform: scale(1.25) } .bg{ width: 150px; height: 110px; background-color: teal; border-radius: 2px; } .slide-down { border-radius: 3px; height: 60px; width: 150px; position: relative; transition: height 0.3s; -webkit-transition: height 0.3s; text-align: center; overflow: hidden; background-color: teal; } .bg:hover .slide-down { height: 140px; } .container{ position: relative; left: 150px; top: 50px; } 
 <div class="container"> <div class="bg"> <img src="https://static.pexels.com/photos/70497/pexels-photo-70497.jpeg" class="thumbnail"/> <div class="slide-down"> <h2>Title</h2> <p>This text is displayed after a downward slide on hover</p> </div> </div> </div> 

Basically what you need to do is put both your requirement into 1 combined object (in this I mean put both the Image & Text inside 1 container) like in the above example 基本上,您需要做的就是将您的两个需求都放入一个组合对象中(这意味着将图像和文本都放入1个容器中),如上例所示

<div class="bg">
    <img src="https://static.pexels.com/photos/70497/pexels-photo-70497.jpeg" class="thumbnail"/>
    <div class="slide-down">
        <h2>Title</h2> 
        <p>This text is displayed after a downward slide on hover</p>
    </div>
</div>

Both of them are put inside 1 container with class .bg , then what you want is to hover the container (not the thumbnail or description itself) and trigger both the scaling & slide-down the menu detail, you can do this by adding the CSS 将它们都放在一个类为.bg容器中,然后您要hover容器 (而不是缩略图或描述本身)并触发scalingslide-down菜单详细信息,您可以通过添加CSS

.bg:hover { ... }

For the scaling , you need to put it together with the container so all the elements inside will be scaled to, then for the description inside it, you need to use 对于scaling ,您需要将其与container放在一起,以便将其中的所有元素缩放到,然后对于其中的说明,您需要使用

.bg:hover slide-down { ... }

This is where you set the animation that will expand the description of the menu (for explanation, this CSS will trigger on .bg hover, and applied to the element .slide-down inside of it) 在此处设置将扩展菜单说明的动画(为便于说明,此CSS将在.bg悬停时触发,并应用于其中的元素.slide-down

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

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