作品相册

By  Ronin

 

案例链接地址:http://abel.aisuy.com/components/CaptionHoverEffects/

 

 

字幕悬停效果

 

HTML

 

网格的结构和图形将由无序列表组成,并且每个项目都将包含一个figure元素。在figure将包含一个图像和figcaption一些文本元素和链接:

 

<ul class="grid cs-style-1"> <li> <figure> <img src="images/1.png" alt="img01"> <figcaption> <h3>Camera</h3> <span>Jacob Cummings</span> <a href="http://dribbble.com/shots/1115632-Camera">Take a look</a> </figcaption> </figure> </li> <li> <figure> <!-- ... --> </figure> </li> <!-- ... --> </ul>

 

CSS

 

所有图的通用样式如下。首先,我们将定义网格的样式以及将用作图形容器的列表项:

 

.grid { padding: 20px 20px 100px 20px; max-width: 1300px; margin: 0 auto; list-style: none; text-align: center; }

.grid li { display: inline-block; width: 440px; margin: 0; padding: 20px; text-align: left; position: relative; }

 

使列表项显示为行内块将使我们能够将其居中,方法是将居中文本对齐方式应用于父项。

 

让我们重置figure元素的边距,并将位置设置为relative。我们figcaption将绝对定位,因此我们需要确保它将在以下位置进行figure:

 

.grid figure { margin: 0; position: relative; }

 

图片的最大宽度为100%,当我们定义媒体查询以调整列表项的大小时,该宽度会派上用场:

 

.grid figure img { max-width: 100%; display: block; position: relative; }

 

在figcaption将被绝对定位。默认情况下,它将位于左上角。我们不会在此处定义任何宽度或高度,因为我们将在所有单独的样式中进行定义:

 

.grid figcaption { position: absolute; top: 0; left: 0; padding: 20px; background: #2c3f52; color: #ed4e6e; }

 

最后,让我们为文本元素和链接定义一些样式:

 

.grid figcaption h3 { margin: 0; padding: 0; color: #fff; } .grid figcaption span:before { content: 'by '; }

.grid figcaption a { text-align: center; padding: 5px 10px; border-radius: 2px; display: inline-block; background: #ed4e6e; color: #fff; }

 

我们将使用伪类:before为包含作者名称的跨度添加“ by”。当然,您可以在HTML中添加它,但这将使您可以自由地将其轻松更改为类似“ made by”或“ Designer:”之类的内容。但是,请注意不要通过执行此类操作来从HTML中删除含义。

 

在CSS的最后,我们还将为较小的屏幕添加媒体查询:

 

@media screen and (max-width: 31.5em) { .grid { padding: 10px 10px 100px 10px; } .grid li { width: 100%; min-width: 300px; } }

 

现在让我们开始做一些不错的效果。

 

现在让我们开始做一些不错的效果。

 

效果1

 

 

让我们从一个非常简单的效果开始。我们希望标题淡入并向右和向下移动一点,以产生从图像中出来的3D层的错觉。

 

为此,我们首先需要设置的宽度和高度,figcaption并将初始不透明度设置为0:

 

.cs-style-1 figcaption { height: 100%; width: 100%; opacity: 0; text-align: center; backface-visibility: hidden; transition: transform 0.3s, opacity 0.3s; }

 

我们还添加了一个过渡,并将背面可见性设置为隐藏,以避免过渡结束时文本呈现出现跳跃。如果您不介意小故障,则不必使用它。

 

悬停时(或触摸时),我们将不透明度设置为1并平移figcaption一点:

 

.no-touch .cs-style-1 figure:hover figcaption, .cs-style-1 figure.cs-hover figcaption { opacity: 1; transform: translate(15px, 15px); }

 

此外,我们将放置文本元素:

 

.cs-style-1 figcaption h3 { margin-top: 70px; } .cs-style-1 figcaption span { display: block; } .cs-style-1 figcaption a { margin-top: 30px; }

 

Effect 2

 

 

 

这种效果将使图像向上移动,并从中汲取灵感,figcaption就像在Minimamente上看到的一样。

 

因此,我们为图像添加一个转换过渡,使其在悬停时向上移动:

 

.cs-style-2 figure img { z-index: 10; transition: transform 0.4s; } .no-touch .cs-style-2 figure:hover img, .cs-style-2 figure.cs-hover img { transform: translateY(-90px); }

 

我们将z-index设置为10,以便图像停留在标题的顶部。

 

在figcaption需要一个固定的高度为100%的宽度。我们将其粘贴在图的底部:

 

.cs-style-2 figcaption { height: 90px; width: 100%; top: auto; bottom: 0; }

 

让我们还将链接按钮放在右侧:

 

.cs-style-2 figcaption a { position: absolute; right: 20px; top: 30px; }

 

Effect 3

 

 

效果2的另一种方法是在移动图像时隐藏所有溢出。让我们这样做,使其看起来好像标题稍微将图像向上推。

 

首先,我们需要将的溢出设置figure为hidden。像这样,我们在走动时不会看到任何溢出的东西:

 

.cs-style-3 figure { overflow: hidden; }

 

图片需要进行转换,并且悬停时我们会将其向上平移50px:

 

.cs-style-3 figure img { transition: transform 0.4s; }

.no-touch .cs-style-3 figure:hover img, .cs-style-3 figure.cs-hover img { transform: translateY(-50px); }

 

该figcaption会比前面的例子中更高一点,我们将它翻译的角度出来figure。我们还为转换和不透明度添加过渡:

 

.cs-style-3 figcaption { height: 100px; width: 100%; top: auto; bottom: 0; opacity: 0; transform: translateY(100%); transition: transform 0.4s, opacity 0.1s 0.3s; }

 

悬停时,我们将不透明度设置为1并将其向上转换。看看我们如何添加两个过渡?一种用于正常状态,一种用于悬停?这是我们可以控制在悬停和悬停时会发生什么的方式。此处的过渡将应用于悬停:我们希望元素快速变为不透明,而转换需要0.4秒。当我们将鼠标悬停时,不透明度将再次设置为0,但仅在延迟0.3秒之后。这将使效果看起来一致且自然。

 

.no-touch .cs-style-3 figure:hover figcaption, .cs-style-3 figure.cs-hover figcaption { opacity: 1; transform: translateY(0px); transition: transform 0.4s, opacity 0.1s; }

 

让我们不要忘记我们的链接按钮:

 

.cs-style-3 figcaption a { position: absolute; bottom: 20px; right: 20px; }

 

Effect 4

 

 

第四示例将利用一些3D优点。目的是从左侧翻转标题并将图像推向右侧。

 

我们将使用列表项作为透视图容器,以便我们可以进行3D变换:

 

.cs-style-4 li { perspective: 1700px; perspective-origin: 0 50%; }

 

如果我们希望3D变换在其他元素中起作用,则子级需要具有以下变换样式:

 

.cs-style-4 figure { transform-style: preserve-3d; }

 

如前所述,该示例将为图像提供另一个包装,为什么需要此包装?好吧,我们需要将图像的父级设置为隐藏溢出,因为我们不希望在移动图像时看到图像从容器中溢出。我们可以将的溢出设置figure为隐藏,但随后看不到字幕的精美3D效果。因此,让我们添加另一个包装器并将其设置为隐藏溢出:

 

.cs-style-4 figure > div { overflow: hidden; }

 

让我们在悬停时移动图像:

 

.cs-style-4 figure img { transition: transform 0.4s; }

.no-touch .cs-style-4 figure:hover img, .cs-style-4 figure.cs-hover img { transform: translateX(25%); }

 

该figcaption会的宽度的一半figure,我们将设置其初始不透明度为0。现在,让我们将其旋转在Y轴这将使其朝着我们翻转在其左侧的原点90度。我们基本上不会那样看。让我们为“ hover out”设置一个过渡,该过渡将按照上一个示例中所述的相同原理进行工作:

 

.cs-style-4 figcaption { height: 100%; width: 50%; opacity: 0; backface-visibility: hidden; transform-origin: 0 0; transform: rotateY(-90deg); transition: transform 0.4s, opacity 0.1s 0.3s; }

 

悬停时,我们将其淡入并旋转到0度,使其从左侧像书​​的一页一样翻转:

 

.no-touch .cs-style-4 figure:hover figcaption,

.cs-style-4 figure.cs-hover figcaption { opacity: 1; transform: rotateY(0deg); transition: transform 0.4s, opacity 0.1s; }

 

最后但并非最不重要的一点是,我们的小链接按钮:

 

.cs-style-4 figcaption a { position: absolute; bottom: 20px; right: 20px; }

 

Effect 5

 

 

此效果将使图像缩小并从背面缩放标题。

 

让我们将图像放在标题上方,并为转换添加一个过渡:

 

.cs-style-5 figure img { z-index: 10; transition: transform 0.4s; }

 

悬停时,我们要按比例缩小图像:

 

.no-touch .cs-style-5 figure:hover img, .cs-style-5 figure.cs-hover img { transform: scale(0.4); }

 

字幕最初将缩放为0.7并逐渐消失:

 

.cs-style-5 figcaption { height: 100%; width: 100%; opacity: 0; transform: scale(0.7); backface-visibility: hidden; transition: transform 0.4s, opacity 0.4s; }

 

悬停时,我们将其放大并淡入:

 

.no-touch .cs-style-5 figure:hover figcaption, .cs-style-5 figure.cs-hover figcaption { transform: scale(1); opacity: 1; }

 

超级容易。哦,当然,小链接按钮希望位于右下角:

 

.cs-style-5 figure a { position: absolute; bottom: 20px; right: 20px; }

 

Effect 6

 

 

 

此效果是效果5的变体。可以使用这种效果进行多种操作,考虑将图像放置在其他位置并显示更多文本。此效果会将文本和图像放置在与以前不同的位置,并且我们不会淡入字幕或对其进行缩放,因为它已经存在,从而创建了一个不同的“环境”。

 

因此,让我们再次对图像执行相同的操作,只是在悬停时,我们还将对其进行一些转换:

 

.cs-style-6 figure img { z-index: 10; transition: transform 0.4s; }

.no-touch .cs-style-6 figure:hover img, .cs-style-6 figure.cs-hover img { transform: translateY(-50px) scale(0.5); }

 

因此,这次字幕没有过渡:

 

.cs-style-6 figcaption { height: 100%; width: 100%; }

 

然后放置文本元素:

 

.cs-style-6 figcaption h3 { margin-top: 60%; } .cs-style-6 figcaption a { position: absolute; bottom: 20px; right: 20px; }

 

Effect 7

 

 

由于第一个元素的框架将与其他列表项重叠,因此我们必须确保z索引正确(颠倒):

 

.cs-style-7 li:first-child { z-index: 6; }

.cs-style-7 li:nth-child(2) { z-index: 5; }

.cs-style-7 li:nth-child(3) { z-index: 4; }

.cs-style-7 li:nth-child(4) { z-index: 3; }

.cs-style-7 li:nth-child(5) { z-index: 2; }

.cs-style-7 li:nth-child(6) { z-index: 1; }

 

就像前面的示例一样,我们希望图像位于标题上方:

 

.cs-style-7 figure img { z-index: 10; }

 

标题将为图形的100%,我们将设置不透明度,高度和框阴影的过渡。为什么框阴影?我们可以轻松地使用盒子阴影在标题周围创建框架:

 

.cs-style-7 figcaption { height: 100%; width: 100%; opacity: 0; backface-visibility: hidden; box-shadow: 0 0 0 0px #2c3f52; transition: opacity 0.3s, height 0.3s, box-shadow 0.3s; }

 

悬停时,我们将不透明度设置为1,增加高度并将框阴影的散布设置为10像素:

 

.no-touch .cs-style-7 figure:hover figcaption,

.cs-style-7 figure.cs-hover figcaption { opacity: 1; height: 130%; box-shadow: 0 0 0 10px #2c3f52; }

 

让我们放置文本元素。我们希望元素在动画字幕的高度后立即出现,但是当我们“悬停”时,我们希望它们立即消失。因此,我们将正常状态下的过渡时间设置为0秒。

 

 

.cs-style-7 figcaption h3 { margin-top: 86%; }

.cs-style-7 figcaption h3,

.cs-style-7 figcaption span,

.cs-style-7 figcaption a { opacity: 0; transition: opacity 0s; }

.cs-style-7 figcaption a { position: absolute; bottom: 20px; right: 20px; }

 

悬停时,我们将使所有元素延迟出现:

 

.no-touch .cs-style-7 figure:hover figcaption h3,

.no-touch .cs-style-7 figure:hover figcaption span,

.no-touch .cs-style-7 figure:hover figcaption a,

.cs-style-7 figure.cs-hover figcaption h3,

.cs-style-7 figure.cs-hover figcaption span, .cs-style-7 figure.cs-hover figcaption a { transition: opacity 0.3s 0.2s; opacity: 1; }