zoukankan      html  css  js  c++  java
  • CSS实战笔记(四) 抖动效果

    1、悬浮抖动

    (1)效果演示

    (2)完整代码

    <!DOCTYPE html>
    <html>
    
    <head>
    	<style>
            .shape {
                margin: 50px;
                 200px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                border: 1px solid black;
            }
            .shake:hover {
                animation: shake 800ms ease-in-out;
            }
            @keyframes shake { /* 水平抖动,核心代码 */
                10%, 90% { transform: translate3d(-1px, 0, 0); }
                20%, 80% { transform: translate3d(+2px, 0, 0); }
                30%, 70% { transform: translate3d(-4px, 0, 0); }
                40%, 60% { transform: translate3d(+4px, 0, 0); }
                50% { transform: translate3d(-4px, 0, 0); }
            }
    	</style>
    </head>
    
    <body>
    	<div class="shape shake">horizontal shake</div>
    </body>
    
    </html>
    

    2、点击抖动

    (1)效果演示

    (2)完整代码

    <!DOCTYPE html>
    <html>
    
    <head>
    	<style>
            .shape {
                margin: 50px;
                 200px;
                height: 50px;
                line-height: 50px;
                text-align: center;
                border: 1px solid black;
            }
            .shake {
                animation: shake 800ms ease-in-out;
            }
            @keyframes shake { /* 垂直抖动,核心代码 */
                10%, 90% { transform: translate3d(0, -1px, 0); }
                20%, 80% { transform: translate3d(0, +2px, 0); }
                30%, 70% { transform: translate3d(0, -4px, 0); }
                40%, 60% { transform: translate3d(0, +4px, 0); }
                50% { transform: translate3d(0, -4px, 0); }
            }
    	</style>
        <script>
            function shake(elemId) {
                let elem = document.getElementById(elemId)
    			if (elem) {
    				elem.classList.add('shake')
    				setTimeout(()=>{ elem.classList.remove('shake') }, 800)
    			}
            }
        </script>
    </head>
    
    <body>
    	<div class="shape" id="shake" onclick="shake(this.id)">vertical shake</div>
    </body>
    
    </html>
    

    3、实战

    抖动效果在实际场景中有重要的应用,比如说当表单输入错误时,让错误的输入框抖动以给用户一个友好的提示

    (1)效果演示

    (2)完整代码

    <!DOCTYPE html>
    <html>
    
    <head>
        <style>
            .login-box {
                 300px;
                min- 300px;
                height: 300px;
                min-height: 300px;
                background-color: white;
                border: 1px solid black;
            }
            .title {
                text-align: center;
                color: cornflowerblue;
            }
            .form-item-wrap {
                 100%;
                margin-top: 20px;
                margin-bottom: 20px;
            }
            .form-label {
                 100%;
                margin-top: 10px;
                margin-bottom: 10px;
                margin-left: 20px;
            }
            .form-input{
                 80%;
                margin-top: 10px;
                margin-bottom: 10px;
                margin-left: 20px;
                padding: 5px;
            }
            .form-submit {
                display: block;
                float: right;
                margin-right: 20px;
                color: cornflowerblue;
                background-color: white;
                text-shadow: 1px 1px 2px cornflowerblue;
                border: none;
                outline: none;
                font-size: 20px;
                font-weight: 500;
            }
            .form-submit:hover {
                cursor: pointer;
                font-size: 22px;
            }
            .shake {
                animation: shake 800ms ease-in-out;
            }
            @keyframes shake {
                10%, 90% { transform: translate3d(-1px, 0, 0); }
                20%, 80% { transform: translate3d(+2px, 0, 0); }
                30%, 70% { transform: translate3d(-4px, 0, 0); }
                40%, 60% { transform: translate3d(+4px, 0, 0); }
                50% { transform: translate3d(-4px, 0, 0); }
            }
        </style>
        <script>
            function shake(elemId) {
                let elem = document.getElementById(elemId)
    			if (elem) {
    				elem.classList.add('shake')
    				setTimeout(()=>{ elem.classList.remove('shake') }, 800)
    			}
            }
            function check() {
                // validate form data
                if (login_form.username.value.length === 0) {
                    // the length of username must be greater than 0
                    shake('username')
                    return false
                }
                if (login_form.password.value.length < 6) {
                    // the length of password must be greater than 6
                    shake('password')
                    return false
                }
                return true
            }
        </script>
    </head>
    
    <body>
        <div class="login-box">
            <h3 class="title">Login</h3>
            <form name="login_form" id="login_form" onsubmit="return check()">
                <div class="form-item-wrap">
                    <label for="username" class="form-label">Username</label><br/>
                    <input type="text" name="username" id="username" class="form-input" placeholder="Please Enter Username">
                </div>
                <div class="form-item-wrap">
                    <label for="password" class="form-label">Password</label><br/>
                    <input type="password" name="password" id="password" class="form-input" placeholder="Please Enter Password">
                </div>
                <input type="submit" value="Submit" class="form-submit">
            </form>
        </div>
    </body>
    
    </html>
    

    【 阅读更多 CSS 系列文章,请看 CSS学习笔记

    版权声明:本博客属于个人维护博客,未经博主允许不得转载其中文章。
  • 相关阅读:
    小程序开发日志-1、小程序自带的日志功能
    java判断List里面的值是否存在重复元素
    java给List<String>批量赋值方法
    (转)post请求携带cookie时配置跨域问题(withCredentials设置)
    redis远程连接不上,配置redis远程连接
    Velocity判断是否为空(Velocity基本语法)
    mysql设置权限,添加远程访问用户
    java 接收邮件时附件中文乱码问题
    JAVA AES加解密问题(解密时出错)
    om.baomidou.mybatisplus.core.exceptions.MybatisPlusException: 该模式不能应用于非数据库字段!
  • 原文地址:https://www.cnblogs.com/wsmrzx/p/11779632.html
Copyright © 2011-2022 走看看