04 - jQuery 动画

常见的一些动画方法
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        div {
            height: 100px;
            width: 200px;
            background-color: green;
        }
    </style>
    <script src="../jquery-1.12.2.js"></script>
    <script>
        $(function () {
            // 案例1
            $('#btnHide').click(function () {
                // 隐藏 参数类型2种  1. 数字  2. 字符串
                // 1. 数字  单位毫秒
                // 2. "slow" "normal" "fast"
                $('#div').hide('fast');
            });
            $('#btnShow').click(function () {
                // 显示 参数类型2种  1. 数字  2. 字符串
                // 1. 数字  单位毫秒
                // 2. "slow" "normal" "fast"
                $('#div').show('fast');
            });

            // 案例2
            $('#btn1').click(function () {
                // 参数与上面一致
                // 滑入 .slideUp()
                $('#dv').slideUp('slow');
            });
            $('#btn2').click(function () {
                // 滑出 .slideDown()
                $('#dv').slideDown('normal');
            });
            $('#btn3').click(function () {
                // 滑入滑出之间的切换 .slideToggle()
                $('#dv').slideToggle('fast');
            });

            // 案例3
            $('#btn4').click(function () {
                // 淡入  参数与上方一致
                $('#d').fadeIn('slow');
            });
            $('#btn5').click(function () {
                // 淡出  可以理解为淡出视线 
                $('#d').fadeOut('normal');
            });
            $('#btn6').click(function () {
                // 淡入,淡出之间切换
                $('#d').fadeToggle('fast');
            });
            
            // 案例4
            $('#btn7').click(function () {
                // 两个参数 透明度
                // 1. 事件  ms
                // 2. 透明度
                $('#d').fadeTo(1000, 0.3)
            });
        });
    </script>
</head>
<body>
    <input type="button" value="隐藏" id="btnHide">
    <input type="button" value="显示" id="btnShow">
    <div id="div"></div>

    <input type="button" value="滑入" id="btn1">
    <input type="button" value="滑出" id="btn2">
    <input type="button" value="切换" id="btn3">
    <div id="dv"></div>

    <input type="button" value="淡入" id="btn4">
    <input type="button" value="淡出" id="btn5">
    <input type="button" value="切换" id="btn6">
    <input type="button" value="透明度变化" id="btn7">
    <div id="d"></div>
</body>
</html>
animate 方法
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        div {
            width: 200px;
            height: 100px;
            background-color: green;
            position: absolute;
        }
    </style>
    <script src="../jquery-1.12.2.js"></script>
    <script>
        $(function () {
            $('#btn').click(function () {
                // 获取div 产生动画效果
                // .animate() 
                // 1. 键值对 --- 对象
                // 2. 时间 --- ms
                // 3. 匿名函数 --- 回调函数
                $('#dv').animate({'width': '300px', 'height': '300px', 'left': '300px'}, 1000, function () {
                    $(this).bind('#dv').animate({'width': '200px', 'height': '100px', 'left': '', 'opacity': 0.2}, 1000);
                });
            });
        });
    </script>
</head>
<body>
    <input type="button" value="显示效果" id="btn">
    <div id="dv"></div>
</body>
</html>
下拉菜单的bug .stop() 方法
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
        .wrap {
            width: 330px;
            height: 30px;
            margin: 100px auto 0;
            padding-left: 10px;
            background-color: pink;
        }
        .wrap li{
            background-color: pink;
        }
        .wrap > ul > li {
            float: left;
            margin-right: 10px;
            position: relative;
        }
        .wrap a {
            display: block;
            height: 30px;
            width: 100px;
            text-decoration: none;
            color: #000;
            line-height: 30px;
            text-align: center;
        }
        .wrap li ul {
            position: absolute;
            top: 30px;
            display: none;
        }
    </style>
    <script src="../jquery-1.12.2.js"></script>
    <script>
        $(function () {
            $('.wrap > ul > li').mouseover(function () {
                // .stop()  停止动画的方法,此处如果不加的话,可能会导致同时出现两个下拉框
                $(this).children('ul').stop().show();
            });
            $('.wrap > ul > li').mouseout(function () {
                $(this).children('ul').stop().hide();
            });
        });
    </script>
</head>
<body>
<div class="wrap">
    <ul>
        <li>
            <a href="javascript:void(0);">帅哥系列</a>
            <ul>
                <li><a href="javascript:void(0);">乔峰</a></li>
                <li><a href="javascript:void(0);">张无忌</a></li>
                <li><a href="javascript:void(0);">郭靖</a></li>
            </ul>
        </li>
        <li>
            <a href="javascript:void(0);">美女系列</a>
            <ul>
                <li><a href="javascript:void(0);">柳 岩</a></li>
                <li><a href="javascript:void(0);">杨 幂</a></li>
                <li><a href="javascript:void(0);">萝 拉</a></li>
            </ul>
        </li>
        <li>
            <a href="javascript:void(0);">渣男妇腐女</a>
            <ul>
                <li><a href="javascript:void(0);">凤姐</a></li>
                <li><a href="javascript:void(0);">助教</a></li>
                <li><a href="javascript:void(0);">导师</a></li>
            </ul>
        </li>
    </ul>
</div>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/article-record/p/11456146.html
今日推荐