JavaScript 案例之 手风琴特效(jQuery实现)

今天学习了jQuery,突发奇想自己动手做一个手风琴特效,这个特效在开发过程中最困难的就是布局,至于jQuery特效的实现还是比较简单的。

话不多说,直接上代码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .container {
            width: 1380px;
            height: 350px;
            margin: 100px auto;
            overflow: hidden;
        }

        .wrapper {
            width: 1400px;
        }

        .box {
            width: 345px;
            height: 350px;
            float: left;
            position: relative;
            overflow: hidden;
        }

        .box .title {
            width: 345px;
            height: 350px;
            background-color: #ddd;
        }

        .box .content {
            position: absolute;
            top: 0;
            left: 345px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="wrapper">
            <div class="box">
                <div class="title" style="">"></div>
                <div class="content"><img src="https://img.zcool.cn/community/"
                        ></div>
            </div>
            <div class="box">
                <div class="title" style="">"></div>
                <div class="content"><img src="https://img.zcool.cn/community/"
                        ></div>
            </div>
            <div class="box">
                <div class="title" style="">"></div>
                <div class="content"><img src="https://img.zcool.cn/community/"
                        ></div>
            </div>
            <div class="box">
                <div class="title" style="">"></div>
                <div class="content"><img src="https://img.zcool.cn/community/"
                        ></div>
            </div>
        </div>
    </div>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script>
        // 1. 让当前的盒子,宽度变成1380px; 让其与的盒子宽度变成0;
        // 2. 移出的时候让所有的盒子宽度变成345px;

        $(".box").mouseover(function () {
            $(this)
                .stop(true)
                .animate({
                    width: 1380
                })
                .siblings(".box")
                .stop(true)
                .animate({
                    width: 0
                })
                .end()
                .children(".content")
                .css({
                    left: 0
                })
        })

        $(".box").mouseout(function () {
            $(".box")
                .stop(true)
                .animate({
                    width: 345
                })
                .queue(function () {
                    $(this)
                        .children(".content")
                        .css({
                            left: 345
                        })
                })
        })
    </script>
</body>

</html>

感兴趣的小伙伴可以自己动手试一下啦。有什么更好的写法欢迎评论区留言。

相关推荐