您的位置:

让你的网页元素摇摆起来:使用CSS animation shake效果

一、什么是CSS animation shake效果

CSS animation shake效果是CSS3动画效果的一种,通过代码实现元素的抖动和摆动效果,为网页增添了动感和活力。

实现这种效果需要借助CSS3中的animation属性和关键帧(@keyframes)规则,使用相应的属性值设置元素的形态变化。其中animation属性有多个子属性,如animation-timing-function、animation-delay、animation-iteration-count等,可以实现不同的效果。

二、如何实现CSS animation shake效果

在CSS中使用CSS animation shake效果可以通过以下步骤实现:

1、定义一个需要应用CSS animation shake效果的元素,如下所示:

    <div class="animated-element"></div>

2、在CSS样式中,为这个元素添加animation属性,并设置animation-name、animation-duration等属性,如下所示:

    .animated-element {
        animation-name: shake;
        animation-duration: 1s;
        animation-delay: 0s;
        animation-iteration-count: infinite;
        animation-timing-function: ease-in-out;
    }

其中animation-name属性指定了元素要使用的动画名称,这里是shake;animation-duration属性指定了动画持续时间,这里是1秒;animation-delay属性指定了动画延迟时间,这里是0秒;animation-iteration-count属性指定了动画要执行的次数,这里是无限循环;animation-timing-function属性指定了动画的时间函数,这里是缓入缓出。

3、在样式中定义shake动画的关键帧,即元素的形态变化过程,如下所示:

    @keyframes shake {
        0% {
            transform: translateX(0);
        }
        25% {
            transform: translateX(-8px);
        }
        50% {
            transform: translateX(0);
        }
        75% {
            transform: translateX(8px);
        }
        100% {
            transform: translateX(0);
        }
    }

其中@keyframes关键帧规则用于定义动画的关键帧,包括动画开始和结束时元素的形态。每个关键帧使用百分比表示动画执行的时间节点,0%表示动画开始时元素的形态,100%表示动画结束时元素的形态。在这个样式中,每个关键帧定义了元素在执行过程中的形态变化,通过在transform属性中设置translateX属性值来实现元素的横向平移。

三、如何调整CSS animation shake效果

通过调整CSS animation shake效果的不同属性值,可以实现不同的动画效果。下面是一些常见的调整方法:

1、调整animation-duration属性值,可以改变动画执行时间的长短。

2、调整animation-delay属性值,可以改变动画执行前的延迟时间。

3、调整animation-iteration-count属性值,可以改变动画执行的次数。

4、调整animation-timing-function属性值,可以改变动画的时间函数,如ease-in、ease-out、ease-in-out、linear等。

5、在@keyframes关键帧规则中调整关键帧的百分比值和transform属性值,可以改变元素的形态变化。

四、完整CSS animation shake效果示例代码

    <!DOCTYPE html>
    <html>
    <head>
        <title>CSS animation shake效果示例代码</title>
        <style>
            .animated-element {
                width: 100px;
                height: 100px;
                background-color: #FF69B4;
                animation-name: shake;
                animation-duration: 1s;
                animation-delay: 0s;
                animation-iteration-count: infinite;
                animation-timing-function: ease-in-out;
            }
            @keyframes shake {
                0% {
                    transform: translateX(0);
                }
                25% {
                    transform: translateX(-8px);
                }
                50% {
                    transform: translateX(0);
                }
                75% {
                    transform: translateX(8px);
                }
                100% {
                    transform: translateX(0);
                }
            }
        </style>
    </head>
    <body>
        <div class="animated-element"></div>
    </body>
    </html>
让你的网页元素摇摆起来:使用CSS animation sh

2023-05-12
CSS动画的摇摆舞

2023-05-12
用CSS实现元素晃动动画效果,让网页更生动

2023-05-12
CSS动画震荡效果探究

2023-05-12
用CSS创建动态抖动动画,让你的网页更生动

2023-05-12
Animate淡入CSS动画效果:让你的页面更加优美和引人注

2023-05-12
利用CSS Animation实现元素跳动效果

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)