手风琴效果(vue实现)

效果图:
在这里插入图片描述
源码1:

<!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>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        ul{
            list-style: none;
        }

        li{
            padding-left: 10px;
            line-height: 2;
        }

        li:hover{
            background-color: #eee;
        }

        .list{
            padding: 0 50px;
        }

        .list .title{
            border: 1px solid #ccc;
            padding: 10px 5px;
            cursor: pointer;
            -webkit-user-select: none;
            user-select: none;
            font-size: 18px;
            font-weight: bold;
        }

        .list .item{
            border: 1px solid #ccc;
        }

        .list:not(:nth-of-type(1)) .item{
            display: none;
        }
    </style>
</head>
<body>
    <div id="app">
        <div class="list" v-for="(l,i) in lists" :key="i">
            <div class="title" @click="toggle($event)">{{l.title}}</div>
            <ul class="item">
                <li v-for="(item,index) in l.items" :key="index">{{item}}</li>
            </ul>
        </div>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                lists: [
                    { title:'我的同学', items:["张三", "李四", "王五"] },
                    { title:'我的同事', items:["张三1", "李四1", "王五1"] },
                    { title:'我的家人', items:["张三2", "李四2", "王五2"] }
                ]
                
            },
            methods: {
                toggle: function(ev){
                    var $title = $(ev.target);
                    $title.parents('#app').find('.item').hide();
                    $title.next().show(1000);
                }
            }
        })
    </script>
</body>
</html>

源码2.

<!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>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            list-style: none;
        }

        li {
            padding-left: 10px;
            line-height: 2;
        }

        li:hover {
            background-color: #eee;
        }

        .list {
            padding: 0 50px;
        }

        .list .title {
            border: 1px solid #ccc;
            padding: 10px 5px;
            cursor: pointer;
            -webkit-user-select: none;
            user-select: none;
            font-size: 18px;
            font-weight: bold;
        }

        .list .item {
            border: 1px solid #ccc;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="list" v-for="(l,i) in lists" :key="i">
            <div class="title" @click="toggle(i)">{{l.title}}</div>
            <ul class="item" v-show="l.show">
                <li v-for="(item,index) in l.items" :key="index">{{item}}</li>
            </ul>
        </div>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                lists: [
                    { title: '我的同学', items: ["张三", "李四", "王五"], show: false },
                    { title: '我的同事', items: ["张三1", "李四1", "王五1"], show: true },
                    { title: '我的家人', items: ["张三2", "李四2", "王五2"], show: false }
                ]

            },
            methods: {
                toggle: function (index) {
                    // ES6的新语法,称为箭头函数, 它是语法糖。
                    // 当箭头函数函数体中只有一条语句时,return语句可省略。
                    this.lists.forEach((list, i) => list.show = i == index)
                    // 回调函数, 回调函数有三个形参,list当前循环中对象,i表示当前循环中的索引
                    // arr 表示循环的数组本身
                    // this.lists.forEach(function(list, i, arr){
                    //     // console.log(arr[i]==list);
                    //     // return list.show = i == index;
                    //     if(i == index){
                    //         list.show = true;
                    //     } else {
                    //         list.show = false;
                    //     }
                    // });
                }
            }
        })
    </script>
</body>

</html>

https://www.cnblogs.com/Smiled/p/7610905.html

toggle() 方法切换元素的可见状态。

如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素
$(selector).toggle(speed,callback,switch)
效果图:再点击时候只打开点击哪个的列表,其他的 关闭
在这里插入图片描述在这里插入图片描述在这里插入图片描述
安装 npm install --save-dev less-loader less

<template>
    <div>
        <button v-on:click="decrement">-</button>
        <input type="text"  size="1" v-model="num">
        <button @click="increment">+</button>

        <div class="header">
        <ul>
            <!-- 循环数据在点击调用changeli方法时将当前索引和本条数据传进去,并使用当前数据show的bool值添加或移除样式 -->
            <li :class="[{active:item.show}]" @click="changeli(index,item)" v-for="(item,index) in headerData" :key="index">
                <!-- 在这里打印出boll值方便查看 -->
                {{item.name}}{{item.show}}
                <!-- 判断当前这条数据的bool值如果是true就打开二级菜单,如果是false就关闭二级菜单 -->
                <ul v-show="item.show"> 
                    <!-- 循环二级菜单数据并使用.stop阻止冒泡 -->
                    <li v-for="(a,index) in item.list" v-on:click.stop="doThis(index)" :key="index">{{a}}</li>
                </ul>
            </li>
        </ul>
    </div>
    </div>
</template>
<script>
export default {
    //props:["num"],//从helloworld组件里面传一个初始值10。父组件到子组件
    data(){
        return{
            num:0,
            headerData: [{
                    name: '客舱服务',
                    list: ["机上升舱", "呼唤铃", "机上餐食", "我的行程", "安全须知"],
                    show: true
                }, {
                    name: '精彩华夏',
                    list: ["品牌介绍", "华夏通程", "航线特惠", "华夏文化"],
                    show: true
                }, {
                    name: '娱乐',
                    list: ["影视", "音乐", "游戏", "阅读"],
                    show: true
                }]
        }  
    },
    methods:{
        increment(){
            this.num++
        },
        decrement(){
            if (this.num<=0){
                alert('受不了啦,宝贝不能再减少啦')
                this.num=0;
            }else {
                this.num--;
            }

        },

        changeli: function(ind, item) {
                // 先循环数据中的show将其全部置为false,此时模板里的v-if判断生效关闭全部二级菜单,并移除样式
                this.headerData.forEach(i => {
                    // 判断如果数据中的headerData[i]的show属性不等于当前数据的show属性那么headerData[i]等于false
                    if (i.show !== this.headerData[ind].show) {
                        i.show = false;
                    };
                });
                // 取反(true或false)
                item.show = !item.show;
                console.log(item.name)
            },
            doThis: function(index) {
                alert(index)
            }
    }
}
</script>
<style lang="less" scoped>

input{
      text-align: center;
    }

.header {
        width: 20%;
        background-color: rgb(0, 50, 116);
        color: #ffffff;
        >ul {
            width: 100%;
            // @include clearfix;
            >li {
                width: 100%;
                // border: 1px solid #ffffff;
                cursor: pointer; // float: left;
                color: 20px;
                text-align: center;
                line-height: 60px;
                &:hover {
                    background-color: rgb(7, 27, 53);
                }
                >ul {
                    width: 100%;
                    background: rgb(0, 50, 116);
                    li{
                        &:hover{
                            background: rgb(0, 50, 116);
                        }
                    }
                }
            }
            .active {
                background-color: rgb(12, 48, 94);
            }
        }
    }
</style>

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
手风琴动画是指在一组元素中,其中一个元素被展开时,其他元素同时被收缩,形成一个类似于手风琴的效果。在 Vue 移动端开发中,可以使用 transition 动画来实现手风琴效果。 首先,在 HTML 中定义一组需要展开和收缩的元素,可以使用 v-for 指令来动态渲染元素,并绑定一个 active 变量表示当前展开元素的索引: ``` <div class="accordion"> <div v-for="(item, index) in items" :key="index" :class="{ active: index === active }"> <div class="title" @click="toggle(index)"> {{ item.title }} </div> <div class="content"> {{ item.content }} </div> </div> </div> ``` 其中,toggle 方法用于切换展开和收缩状态: ``` methods: { toggle(index) { this.active = index === this.active ? -1 : index; } } ``` 接下来,在 CSS 中定义手风琴动画的样式,使用 transform 属性实现元素的展开和收缩: ``` .accordion { .title { cursor: pointer; } .content { overflow: hidden; transition: height 0.3s ease-out; &.active { height: auto; transition: height 0.3s ease-in; } } } ``` 其中,active 类用于控制元素的展开和收缩状态,当元素被激活时,添加 active 类,并将高度设置为 auto,实现元素的展开效果;当元素被取消激活时,移除 active 类,并将高度设置为 0,实现元素的收缩效果。 最后,在 Vue 中使用 transition 组件包裹元素,实现动画效果: ``` <transition name="accordion"> <div v-for="(item, index) in items" :key="index" :class="{ active: index === active }"> <div class="title" @click="toggle(index)"> {{ item.title }} </div> <div class="content"> {{ item.content }} </div> </div> </transition> ``` 其中,name 属性用于指定动画的名称,可以在 CSS 中定义对应的动画样式。 这样就完成了手风琴动画的实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值