您的位置:

CSS旋转过渡效果

一、CSS旋转过渡基础

CSS旋转过渡效果是一种视觉效果,它通过CSS3的transform属性来实现。transform属性可以进行旋转、缩放、平移等操作。其中,旋转操作可以通过rotate()函数来完成。该函数接受一个角度作为参数,单位可以是度(deg)、弧度(rad)或者梯度(grad)。

    <div class="box">
      <p>Hello World</p>
    </div>
    
    .box {
      width: 200px;
      height: 200px;
      background-color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      transition: transform 1s;
    }

    .box:hover {
      transform: rotate(180deg);
    }

上面的代码实现了一个简单的旋转效果。当鼠标悬停在box元素上时,它将会在1秒钟内旋转180度。

二、通过CSS关键帧动画实现旋转过渡效果

除了使用CSS属性transition来实现 CSS 过渡效果之外,还可以通过 CSS 关键帧动画来实现旋转过渡效果。

    @keyframes spin {
      0% {
        transform: rotate(0deg);
      }
      100% {
        transform: rotate(360deg);
      }
    }

    .box {
      width: 200px;
      height: 200px;
      background-color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      animation-name: spin;
      animation-duration: 1s;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }

上面的代码中,我们定义了一个名为spin的关键帧动画。在0%时,元素的旋转角度为0度,在100%时旋转角度为360度,这样就可以实现完整的旋转动画。然后我们将动画应用到.box元素上的animation-name,设置动画时长为1秒,动画时间函数为linear,动画循环播放无限次。

三、通过CSS变量实现更灵活的旋转过渡效果

为了让 CSS 过渡效果更加灵活,我们可以使用 CSS 变量来定义旋转角度。

    :root {
      --angle: 0deg;
    }

    .box {
      width: 200px;
      height: 200px;
      background-color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      transform: rotate(var(--angle));
      transition: transform 1s;
    }

    .box:hover {
      --angle: 180deg;
    }

上面的代码中,我们定义了一个名为 --angle 的 CSS 变量,初始值为 0deg。然后我们将这个变量应用到 .box 元素的 transform 属性中的 rotate() 函数中。当鼠标悬停在.box元素上时,我们通过设置 --angle 变量的值为 180deg,让元素完成一个旋转过渡效果。

四、通过CSS transform-origin属性实现旋转中心点设置

CSS transform-origin 属性可以用来设置元素变换(例如:旋转、缩放、倾斜或平移)的起点。默认情况下,变换起点为元素的中心点。但是通过transform-origin属性可以实现旋转中心点的任意设置。

    .box {
      width: 200px;
      height: 200px;
      background-color: #333;
      display: flex;
      align-items: center;
      justify-content: center;
      transform: rotate(var(--angle));
      transform-origin: top left;
      transition: transform 1s;
    }

    .box:hover {
      --angle: 180deg;
    }

上面的代码中,我们通过设置transform-origin属性的值,将旋转中心点从默认的中心点移动到了左上角。

五、通过CSS filter属性实现图片旋转效果

除了可以对元素进行旋转之外,我们还可以使用filter属性对图片进行旋转。

    <img src="image.jpg" alt="image" class="img">

    .img {
      filter: grayscale(100%) hue-rotate(var(--angle));
      transition: filter 1s;
    }

    .img:hover {
      --angle: 180deg;
    }

上面的代码中,我们通过设置filter属性的值,将进行图片的灰度处理和色相旋转。当鼠标悬停在图片上时,我们通过设置--angle变量的值为180deg,实现了一个图片旋转效果。

六、总结

通过本文的学习,我们了解了如何使用CSS实现旋转过渡效果。不仅可以通过transition属性来实现元素的旋转过渡,还可以通过CSS关键帧动画来实现更加灵活的效果。而通过使用CSS变量和transform-origin属性,我们可以让旋转效果更加的精准。另外,我们还学习到了如何通过CSS filter属性实现图片旋转效果。

CSS旋转过渡效果

2023-05-12
CSS 旋转动画

2023-05-12
CSS过渡动画全面解析

2023-05-20
如何运用CSS创建旋转动画效果

2023-05-12
使用CSS旋转动画为网页添加引人注目的动态元素

2023-05-12
CSS动画效果详解

2023-05-17
使用transform: rotation CSS属性实现网

2023-05-12
CSS移动效果

2023-05-12
CSS Fade Up效果

2023-05-12
使用CSS制作旋转动画效果

2023-05-12
如何创建CSS旋转动画效果

2023-05-12
使用CSS transform属性制作炫酷动画效果

2023-05-16
如何使用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)