显示文本的剪切路径替代方法

时间:2018-11-07 09:54:37

标签: css css3 css-animations clip-path

我正在尝试实现类似的东西

body {
	display: flex;
	align-items: center;
	justify-content: center;
	height: 100vh;
	background-color: #8ce2ea;
	flex-direction: column;
}

.reveal-text,
.reveal-text::after {
	-webkit-animation-delay: 2s;
	        animation-delay: 2s;
	-webkit-animation-iteration-count: 1;
	        animation-iteration-count: 1;
	-webkit-animation-duration: 800ms;
	        animation-duration: 800ms;
	-webkit-animation-fill-mode: both;
	        animation-fill-mode: both;
	-webkit-animation-timing-function: cubic-bezier(0.0, 0.0, 0.2, 1);
	        animation-timing-function: cubic-bezier(0.0, 0.0, 0.2, 1);
}

.reveal-text {
	position: relative;
	font-size: 10vw;
	display: block;
	-webkit-user-select: none;
	   -moz-user-select: none;
	    -ms-user-select: none;
	        user-select: none;
	-webkit-animation-name: reveal-text;
	        animation-name: reveal-text;
	color: #FFF;
	white-space: nowrap;
	cursor: default
	
}

.reveal-text::after {
	content: "";
	position: absolute;
	z-index: 999;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: #f2f98b;
	-webkit-transform: scaleX(0);
	        transform: scaleX(0);
	-webkit-transform-origin: 0 50%;
	        transform-origin: 0 50%;
	pointer-events: none;
	-webkit-animation-name: revealer-text;
	        animation-name: revealer-text;
}


@-webkit-keyframes reveal-text {
	from {
		-webkit-clip-path: inset(0 100% 0 0);
		        clip-path: inset(0 100% 0 0);
	}
	to {
		-webkit-clip-path: inset(0 0 0 0);
		        clip-path: inset(0 0 0 0);
	}
}


@keyframes reveal-text {
	from {
		-webkit-clip-path: inset(0 100% 0 0);
		        clip-path: inset(0 100% 0 0);
	}
	to {
		-webkit-clip-path: inset(0 0 0 0);
		        clip-path: inset(0 0 0 0);
	}
}


@-webkit-keyframes revealer-text {
	
	0%, 50% {
		-webkit-transform-origin: 0 50%;
		        transform-origin: 0 50%;
	}
	
	60%, 100% {
		-webkit-transform-origin: 100% 50%;
		        transform-origin: 100% 50%;		
	}

	
	60% {
		-webkit-transform: scaleX(1);
		        transform: scaleX(1);
	}
	
	100% {
		-webkit-transform: scaleX(0);
		        transform: scaleX(0);
	}
}


@keyframes revealer-text {
	
	0%, 50% {
		-webkit-transform-origin: 0 50%;
		        transform-origin: 0 50%;
	}
	
	60%, 100% {
		-webkit-transform-origin: 100% 50%;
		        transform-origin: 100% 50%;		
	}

	
	60% {
		-webkit-transform: scaleX(1);
		        transform: scaleX(1);
	}
	
	100% {
		-webkit-transform: scaleX(0);
		        transform: scaleX(0);
	}
}
<h1 class="reveal-text">
	I'm here.
</h1>

但是问题在于,由于clip-path(文本从头开始显示),它无法按预期运行在边缘上

@keyframes reveal-text {
  from {
     clip-path: inset(0 100% 0 0);
  }
  to {
     clip-path: inset(0 0 0 0);
  }
}

有什么其他方法可以使这项工作在边缘吗?

(我已经了解到剪切路径可以在svg的边缘工作,我应该用文本创建svg吗?

1 个答案:

答案 0 :(得分:2)

这是您不必使用clip-path的另一种方法。只需依靠将覆盖您的文本的背景色即可。您将没有透明度,但会获得更好的支持。

body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #8ce2ea;
  flex-direction: column;
}

.reveal-text {
  position: relative;
  font-size: 10vw;
  display: block;
  color: #FFF;
  cursor: default
}

.reveal-text::after {
  content: "";
  position: absolute;
  z-index: 999;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: linear-gradient(#f2f98b, #f2f98b), linear-gradient(#8ce2ea, #8ce2ea);
  background-size: 0% 100%, 100% 100%;
  background-repeat: no-repeat;
  background-position: left, right;
  animation-name: revealer-text;
  animation-delay: 2s;
  animation-iteration-count: 1;
  animation-duration: 800ms;
  animation-fill-mode: both;
  animation-timing-function: cubic-bezier(0.0, 0.0, 0.2, 1);
}

@keyframes revealer-text {
  0% {
    background-size: 0% 100%, 100% 100%;
    background-position: left, right;
  }
  50% {
    background-size: 100% 100%, 0% 100%;
    background-position: left, right;
  }
  51% {
    background-size: 100% 100%, 0% 100%;
    background-position: right;
  }
  100% {
    background-size: 0% 100%, 0% 100%;
    background-position: right;
  }
}
<h1 class="reveal-text">
  I'm here.
</h1>