【CSS + ElementUI】el-tree下拉扩展图标置于右侧

  1. 效果图
    在这里插入图片描述

  2. 代码实现

<template>
    <div class="search_resource">
        <el-tree class="filter-tree" ref="tree" default-expand-all :data="directoryList" :props="defaultProps"
            icon-class="el-icon-arrow-right" node-key="id" @node-click="handleNodeClick">
            <span class="custom-tree-node" slot-scope="{ node, data }">
                <span class="tree_title" v-if="node.level == 1">
                    {
   {
    data.name }}
                </span>
                <span class="tree_title" v-else-if="node.level == 2">
                    {
   {
    data.name }}
                </span>
                <div v-else class="tree_son_title">
                    <div class="son_title_area">
                        <span class="ellipse"></span>
                        <span class="son_title">{
   {
    data.name }}</span>
                    </div>
                    <div class="son_num">({
   {
    node.id }})</div>
                </div>
            </span>
        </el-tree>
    </div>
</template>
<script>
export default {
   
    data() {
   
        return {
   
            query: {
   },
            directoryList: [
                {
   
                    id: '1',
                    name: '一级',
                    children: [
                        {
   
                            id: '1-1',
                            name: "一级 1.1"
                        },
                        {
   
                            id: '1-2',
                            name: "一级 1.2"
                        }
                    ]
                },
                {
   
                    id: '2',
                    name: '二级',
                    children: [
                        {
   
                            id: '2-1',
                            name: "二级 2.1",
                            children: [
                                {
   
                                    id: '2-1-1',
                                    name: "二级 2.2.1"
                                },
                                {
   
                                    id: '2-1-2',
                                    name: "二级 2.2.2"
                                }
                            ]
                        },
                        {
   
                            id: '2-2',
                            name: "二级 2.2"
                        }
                    ]
                }
            ],
            defaultProps: {
   
                children: 'children',
                label: 'label'
            }
        }
    },
	mounted(){
   
		this.fetchData()
	}
    methods: {
   
        fetchData() {
   
            // 树
            getTreeList(this.query).then(res=>{
   
            	// this.directoryList = res.data
            })
        },

        //点击tree传id,搜索对应数据
        handleNodeClick(data) {
   
            this.$emit('handClick', data.id)
        }
    }
}
</script>
<style lang="less" scoped>
.search_resource {
   
    padding: 12px 16px 8px 16px;

    .filter-tree /deep/ .el-tree-node__content {
   
        // width: 220px !important; // 树节点内容限制宽度
        height: 32px !important;
    }

    // 让tree 箭头在右边
    .filter-tree /deep/ .el-tree-node__expand-icon {
   
        position: absolute;
        right: -3%;
        font-size: 18px;
    }

    .custom-tree-node {
   
        // 垂直居中
        // display: flex;
        // align-items: center;
        display: inline-block;
        width: 100%;

        .tree_title {
   
            color: #333;
            font-family: "Source Han Sans CN";
            font-size: 16px;
            font-style: normal;
            font-weight: 500;
            line-height: 24px;
        }

        .tree_son_title {
   
            width: 170px; // 此处过长,导致树在展开收缩时宽度轻微的左右浮动;也可以与.el-tree-node__content搭配使用
            display: flex;
            justify-content: space-between;
            align-items: center;

            .son_title_area {
   
                display: flex;
                align-items: center;
                /* 垂直居中 */
                justify-content: center;
                /* 水平居中 */
                text-align: center;
                /* 修复IE11中的bug */
            }

            .ellipse {
   
                display: inline-block;
                width: 5px;
                height: 5px;
                background: #d9d9d9;
                border-radius: 50%;
                margin: 0;
            }

            .son_title {
   
                display: inline-block;
                color: #999;
                font-family: "Source Han Sans CN";
                font-size: 14px;
                font-style: normal;
                font-weight: 350;
                line-height: 24px;
                margin: 0 0 0 3px;
            }

            .son_num {
   
                color: #999;
                font-family: "Source Han Sans CN";
                font-size: 12px;
                font-style: normal;
                font-weight: 300;
                line-height: 24px;
            }
        }
    }
}
</style>

  1. 问题
    tree左右轻微移动
    (1)与 overflow-y: auto; 有关
    (2)与 .el-tree-node__content 和 tree_son_title 的 width 有关

相关推荐

  1. vue2 el-tree树形

    2024-02-02 13:30:03       10 阅读
  2. Flutter,点击图标后,显示条目选框

    2024-02-02 13:30:03       22 阅读
  3. Uniapp导航栏右侧自定义图标文字按钮

    2024-02-02 13:30:03       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-02 13:30:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-02 13:30:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-02 13:30:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-02 13:30:03       20 阅读

热门阅读

  1. sql INOT IN踩的坑

    2024-02-02 13:30:03       31 阅读
  2. Linux(ubuntu) -- 安装后调配

    2024-02-02 13:30:03       31 阅读
  3. CCF-CSP 202206-1 归一化处理

    2024-02-02 13:30:03       29 阅读
  4. IDEA常用快捷健

    2024-02-02 13:30:03       26 阅读
  5. 点亮第一个LED实验

    2024-02-02 13:30:03       35 阅读
  6. 【极简】Pytorch中的register_buffer()

    2024-02-02 13:30:03       31 阅读
  7. MySQL运维实战(5.4) MySQL元数据乱码

    2024-02-02 13:30:03       39 阅读
  8. 【springBoot】统一功能处理

    2024-02-02 13:30:03       32 阅读
  9. InDesign Server-功能介绍-IDML 文件-ID插件

    2024-02-02 13:30:03       28 阅读