您的位置:

CSS3 Transform的rotate3d属性实现元素3D旋转效果

一、什么是CSS3 Transform的rotate3d属性

CSS3 Transform的rotate3d属性是CSS3中的一种3D旋转效果。其作用是将元素沿着x、y和/或z轴旋转一定的角度。其中rotate3d属性可以指定围绕一个自定义轴旋转元素。

在3D空间中,一个轴定义为一个三维向量(x,y,z),通过rotate3d()函数定义,它是用四个参数(x,y,z,θ)来定义一个包含旋转轴方向以及旋转角度的向量。

    transform: rotate3d(1, 1, 1, 45deg);

上述代码中,元素围绕向量(1,1,1)旋转45度,即在3D空间内按照45度旋转了一个直角三角形所在的平面。

二、如何使用CSS3 Transform的rotate3d属性实现侧翻效果

实现元素侧翻效果最常用的方法是使用CSS3中的rotateY属性。但是如果需要实现更加自定义的3D旋转效果,则可以使用rotate3d属性。

下面是一个实现元素侧翻效果的代码示例:

    .flip-container {
        -webkit-perspective: 1000px;
        perspective: 1000px; 
    }
    .flipper {
        -webkit-transition: 0.6s;
        -webkit-transform-style: preserve-3d;
        -ms-transform-style: preserve-3d;
        transform-style: preserve-3d;
        position: relative;
    }
    .flipper .front, .flipper .back {
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
        position: absolute;
        top: 0;
        left: 0;
    }
    .flipper .front {
        z-index: 2;
    }
    .flipper .back {
        -webkit-transform: rotate3d(0, 1, 0, 180deg);
        transform: rotate3d(0, 1, 0, 180deg);
    }
    .flip-container:hover .flipper {
        -webkit-transform: rotate3d(0, 1, 0, 180deg);
        transform: rotate3d(0, 1, 0, 180deg);
    }

上述代码中,首先给容器加上了透视,然后将 flipper 元素的 transform-style 设置为 preserve-3d,这样才可以在3D空间内进行旋转。接着使用 rotate3d() 函数来定义侧翻时围绕的自定义轴,这里的轴是 (0, 1, 0),表示沿着 Y 轴旋转。最后当鼠标 hover 到容器上时,触发元素翻转的效果。

三、如何使用CSS3 Transform的rotate3d属性实现元素翻页效果

实现元素翻页效果也是CSS3中常用的动效之一,同样可以使用rotate3d属性来实现。下面是一个示例代码:

    .flip {
        -webkit-perspective: 800px;
        perspective: 800px;
        position: relative;
        text-align: left;
        -webkit-transition: -webkit-transform 1s;
        transition: transform 1s;

    }
    .flip .front, .flip .back {
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
        position: absolute;
        top: 0;
        left: 0;
    }
    .flip .front {
        z-index: 2;
    }
    .flip .back {
        -webkit-transform: rotate3d(0, 1, 0, 180deg);
        transform: rotate3d(0, 1, 0, 180deg);
    }
    .flip.animate .card {
        -webkit-transform: rotate3d(0, 1, 0, -180deg);
        transform: rotate3d(0, 1, 0, -180deg);
    }
    .flip.animate .back {
        -webkit-transform: rotate3d(0, 1, 0, 0deg);
        transform: rotate3d(0, 1, 0, 0deg);
    }

上述代码中,我们使用了一个 flip 容器,front 和 back 元素以及添加了一个 animate 类作为动效的触发器。通过给容器制定透视效果,我们可以让 flip 和 back 在 3D 空间里面进行旋转。在 flip 元素中设置了过渡属性,以及 flip.animate .card 和 flip.animate .back 这两个选择器来设置翻转时的变化效果。

四、如何使用CSS3 Transform的rotate3d属性实现元素翻翻效果

元素翻翻效果是一种比较特殊的3D旋转效果,它可以让元素在翻转的同时进行另一种旋转。下面是一个示例代码:

    .flip {
        -webkit-perspective: 800px;
        perspective: 800px;
        position: relative;
        text-align: left;
        -webkit-transition: -webkit-transform 1s;
        transition: transform 1s;

    }
    .flip .front, .flip .back {
        -webkit-backface-visibility: hidden;
        backface-visibility: hidden;
        position: absolute;
        top: 0;
        left: 0;
    }
    .flip .front {
        z-index: 2;
    }
    .flip .back {
        -webkit-transform: rotate3d(0, 1, 0, 180deg);
        transform: rotate3d(0, 1, 0, 180deg);
    }
    .flip.animate .card {
        -webkit-transform: rotate3d(0, 1, 0, -180deg) rotate3d(1, 1, 1, 45deg);
        transform: rotate3d(0, 1, 0, -180deg) rotate3d(1, 1, 1, 45deg);
    }
    .flip.animate .back {
        -webkit-transform: rotate3d(0, 1, 0, 0deg) rotate3d(1, 1, 1, -45deg);
        transform: rotate3d(0, 1, 0, 0deg) rotate3d(1, 1, 1, -45deg);
    }

在上述代码中,我们依然使用了 flip 容器, front 和 back 元素以及添加了一个 animate 类作为动效的触发器。不同的是,这里在设置rotate3d属性的时候,我们传递了两个参数,第一个参数是 (0, 1, 0, -180deg) 用来控制翻转效果,第二个参数是 (1, 1, 1, 45deg) 用来控制“翻翻”效果。最终造成既翻转又翻翻的3D效果。

CSS3 Transform的rotate3d属性实现元素3

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

2023-05-12
如何使用rotate3d实现惊人的3D动画效果?

2023-05-17
优雅旋转元素的CSS技巧

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

2023-05-16
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)