css - 动画 div 颜色加悬停动画

标签 css css-animations

我有一条从 div 创建的线,现在我想做的是为 div 颜色设置动画,背景灰色,然后填充白色,然后白色像滑过一样填充回灰色。然后将鼠标悬停在线条上,文本向上滑动约 10 像素,然后在松开时返回到默认位置。

喜欢底部的这个example

.scroll-indicator {
    position: absolute;
    left: 50%;
    bottom: 0;
    z-index: 340;
    display: inline-block;
    width: 4.16667%;
    height: 6.66667%;
    min-height: 60px;
    font-family: 'rajdhani', 'Helvetica Neue', Helvetica, sans-serif;
    font-weight: bold;
    font-style: normal;
    color: #000;
    font-size: 16px;
}
.scroll-indicator .border-grey {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 100;
    width: 2px;
    height: 100%;
    background: #333;
}
.scroll-indicator .border {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 200;
    width: 2px;
    height: 100%;
    background: #000;
}
.scroll-indicator em {
    display: inline-block;
    font-style: normal;
    -webkit-transform: rotate(-90deg);
    transform: rotate(-90deg);
    -webkit-transform-origin: center center;
    transform-origin: center center;
    position: absolute;
    left: 0px;
    top: 12px;
}

@media screen and (max-width: 800px) {
.scroll-indicator {
    bottom: 0;
}
}
<a href="" class="scroll-indicator" style="opacity: 1; transform: matrix(1, 0, 0, 1, 0, 0);">
		<div class="border-grey"></div>
		<div class="border" style="transform: matrix(1, 0, 0, 1, 0, 0); transform-origin: 0% 0% 0px;"></div>
		<em>scroll</em>
	</a>

最佳答案

您可以使用 CSS3 animationstransitions实现该行为。

要实现动画,通常最好在尝试编写代码之前了解正在发生的事情。在这种情况下,我们可以通过 3 个简单的步骤来描述它:

  1. 元素从 top: 0 开始,height: 0
  2. 元素保持在 top: 0height: 100%
  3. 元素移动到顶部:100%高度:0

考虑到这一点,我们可以编写我们的 keyframe .

下面是一个关于如何做的小例子:

body {
  background: #555;
}

.scroll-indicator {
	position: absolute;
	bottom: 0;
	width: 30px;
	left: 50%;
	height: 60px;
	transition: height .25s linear;
	cursor: pointer;
}

.scroll-indicator:hover {
	height: 75px;
}

.scroll-indicator .border-grey {
	position: absolute;
	top: 0;
	bottom: 0;
	left: 0;
	width: 2px;
	background: #333;
}

.scroll-indicator .border-white {
	position: absolute;
	top: 0;
	left: 0;
	width: 2px;
	background: #fff;
	animation-name: animation;
    animation-duration: 3s;
	animation-iteration-count: infinite;
}

.scroll-indicator span {
	transform: rotate(-90deg);
	position: absolute;
	top: 10px;
  color: #fff;
}

@keyframes animation {
	0% {
		height: 0;
	}
	33% {
		top: 0;
		height: 100%;
	}
	66% {
		top: 100%;
		height: 0;
	}
	100% {}
}
<div class="scroll-indicator">
	<div class="border-grey"></div>
	<div class="border-white"></div>
	<span>scroll</span>
</div>

关于css - 动画 div 颜色加悬停动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48349873/

相关文章:

javascript - 我如何将 CSS 过渡(例如淡入淡出)应用于 React 中的元素列表,以便它们的动画在渲染时一个接一个地排序?

CSS 框图像 - 布局问题

jquery - 如何在可折叠内容面板内创建按钮并使内容可见并隐藏在其中?

CSS展开框动画

css - 如何让 CSS 动画在通过 CSS 关键帧循环一次后停止?

css - 如何并行启动 2 个动画

css - 为什么 @keyframes 动画不适用于 Vue.js?

css - New React with Material-UI Typography 和 Button 组件字体粗细显示不正确

html - 如果它们在同一页面上,如何在另一种形式下方显示一种形式的按钮

javascript - 如何在全屏 HTML5 视频播放器中显示我的 div 面板?