代码之家  ›  专栏  ›  技术社区  ›  MePo

显示文本的剪辑路径备选方案

  •  2
  • MePo  · 技术社区  · 6 年前

    我正在努力实现这样的目标

    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>

    但问题是,它并没有像预期的那样在edge上工作,因为 clip-path

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

    有没有什么不同的方法可以让它在edge中工作?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Temani Afif    5 年前

    这是另一种你不必使用的方法 clip-path . 只需依靠一个背景色,将覆盖你的文字。你不会有透明度,但你会有更好的支持。

    body {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
      background-color: #8ce2ea;
      margin:0;
    }
    
    .reveal-text {
      position: relative;
      font-size: 10vw;
      color: #FFF;
    }
    
    .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: revealer-text 0.8s 2s cubic-bezier(0.0, 0.0, 0.2, 1) forwards;
    }
    
    @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>