自定義彈出層.css 和 .animate的區別

weixin_34127717發表於2018-02-23
<!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>
        .message{
            position: fixed;
            top: -50px;
            left: 50%;
            width: 100px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            background-color: #00aaee;
            color: #fff;
            box-sizing: border-box;
            border-radius: 4px;
        }

    </style>
</head>
<body>
    <div class="message">hello</div>
    <button id="btn">btn</button>
</body>
</html>
<script src="http://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
    function fade(text){
        $('.message').html(text);

        //.css無動畫過渡效果
        // $('.message').css({
        //     top: "50px",
        // })
        // setTimeout(() => {
        //     $('.message').css({
        //         top: "0"
        //     })
        // },2000)

        // .animate有動畫過渡效果
        $('.message').animate({
            top: "50px"
        })
        setTimeout(() => {
            $('.message').animate({
                top: "-50px"
            })
        },2000)
    }
    $('#btn').click(() => {
        fade("hello")
    })
</script>

相關文章