动画div颜色加上悬停动画

时间:2018-01-19 21:35:07

标签: css3 css-animations

我有一条用div创建的线现在我要做的是为div颜色设置动画,背景为灰色,然后填充白色,然后白色填充为灰色,就像它滑过一样。然后悬停线和文本向上滑动大约10px,然后在释放时它回到默认位置。

就像这一个在底部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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

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

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

  1. 元素在top: 0 height: 0
  2. 开头
  3. 元素与top: 0
  4. 一起留在height: 100%
  5. 元素随top: 100%
  6. 移至height: 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>