您的位置:

CSS垂直动画:向上和向下运动的元素

CSS动画是实现页面交互效果的重要手段之一,本文将介绍如何使用CSS来实现垂直方向上下运动的元素动画效果。

一、CSS transform属性

CSS3中的transform属性可以实现元素的旋转、缩放、平移等效果,其中translateY()函数可以实现元素在垂直方向上的移动,其语法如下:

transform: translateY(<length>);

其中length可以为正值或负值,表示移动的距离。当length值为正数时,元素向下移动;当length值为负数时,元素向上移动。

我们可以将上述代码应用于需要垂直方向上下移动的元素上,然后使用动画属性(如animation或transition)来实现平滑的过渡效果。

/* CSS */
.container {
  position: relative;
}

.box {
  position: absolute;
  top: 0;
  left: 0;
  width: 100px;
  height: 100px;
  background-color: #f00;
  animation: move-up-and-down 2s linear infinite;
}

@keyframes move-up-and-down {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(100px);
  }
}

在上述代码中,我们将.box元素的position属性设置为absolute,然后将其置于.container元素中。接着,我们使用animation属性来实现元素的垂直上下移动,其关键帧动画定义如下:

  • 0%和100%时,元素不进行上下移动;
  • 50%时,元素向下移动100px。

通过这样的方式,我们就可以实现一个简单的元素垂直上下运动的动画效果。

二、CSS transition属性

CSS3中的transition属性可以实现过渡效果,即在元素属性发生变化时,平滑地改变元素属性所对应的样式。我们可以利用这种特性实现元素的垂直上下运动效果。代码如下:

/* CSS */
.box {
  position: relative;
  top: 0;
  transition: top 1s ease-in-out;
}

.box.move-down {
  top: 100px;
}

在上述代码中,.box元素的position属性为relative,之后我们只需要在需要进行上下运动的时候,给.box元素添加一个名为“move-down”的class,这个class定义了box元素的top属性从0过渡到100px的动画,其执行过程为1秒,并使用了缓动效果(ease-in-out)使页面更加流畅。这样就可以实现一个简单的垂直动画的效果。

三、CSS animation属性

在CSS3中,animation属性可以实现更加复杂的动画效果,其关键帧动画可以定义多个关键帧,并且可以使用easing函数实现不同的动画效果。代码如下:

/* CSS */
.box {
  position: relative;
  top: 0;
  animation: move-up-and-down 2s ease-in-out infinite;
}

@keyframes move-up-and-down {
  0%, 100% {
    top: 0;
    transform: translateY(0);
  }
  25% {
    top: 50px;
    transform: translateY(50px);
  }
  75% {
    top: -50px;
    transform: translateY(-50px);
  }
}

在上述代码中,.box元素的位置使用了相对定位(position:relative),接着我们使用animation属性来实现元素的垂直上下移动,同时还加入了关键帧,其关键帧动画定义如下:

  • 0%和100%时,元素不进行上下移动;
  • 25%时,元素向下移动50px;
  • 75%时,元素向上移动50px。

通过这样的方式,我们就可以实现一个稍微复杂一点的元素垂直上下运动的动画效果。不同的关键帧可以定义不同的移动路径、速度等信息,页面动画效果更加出彩。

四、总结

通过CSS transform属性、transition属性和animation属性,我们可以实现页面元素的垂直上下运动效果。这些属性都是CSS3中新增的特性,已经得到了广泛的支持,同时具有不错的浏览器兼容性。在实现动画效果时,我们需要结合关键帧动画和相应的动画属性,以达到更加丰富的动画效果。

CSS垂直动画:向上和向下运动的元素

2023-05-12
when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: ${item.id} [in template "article/detail/index.ftl" at line 48, column 106] ---- Java stack trace (for programmers): ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) at freemarker.core.EvalUtil.coerceModelToTextualCommon(EvalUtil.java:481) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:401) at freemarker.core.EvalUtil.coerceModelToStringOrMarkup(EvalUtil.java:370) at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:104) at freemarker.core.DollarVariable.accept(DollarVariable.java:63) at freemarker.core.Environment.visit(Environment.java:371) at freemarker.core.IteratorBlock$IterationContext.executedNestedContentForCollOrSeqListing(IteratorBlock.java:321) at freemarker.core.IteratorBlock$IterationContext.executeNestedContent(IteratorBlock.java:271) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:244) at freemarker.core.Environment.visitIteratorBlock(Environment.java:645) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:108) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at freemarker.core.Environment.visit(Environment.java:335) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.visit(Environment.java:341) at freemarker.core.Environment.process(Environment.java:314) at freemarker.template.Template.process(Template.java:383) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:332) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:266) at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:220) at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:181) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:314) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1431) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1106) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:205) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at com.software.filter.HttpSpiderIdentifyFilter.doFilter(HttpSpiderIdentifyFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:174) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:149) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:340) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) at java.base/java.lang.VirtualThread.run(VirtualThread.java:309)