vue3 :first-child和:last-child的使用

文章展示了如何巧妙运用CSS选择器`:first-child`和`:last-child`,结合Vue.js的模板语法和动态样式,来实现一个简洁的界面效果。通过点击切换选中状态,改变元素的边框和背景色,同时展示了一个删除按钮的显示和隐藏效果。代码示例详细解释了实现过程,适合初学者学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



前言

:first-child和:last-child的巧妙使用。


一、先看效果图

item1选中效果
请添加图片描述

鼠标到item2上的效果
请添加图片描述

二、代码效果

<script lang="ts" setup>
const tabData = [{ name: 'item1', content: '' }, { name: 'item2', content: '' }, { name: 'item3', content: '' }]
const currentContentIndex = ref(0)
</script>

<template>
  <div class="flex-none bg-white">
    <div
      class="ml-20px mt-20px mr-20px flex flex-row h-40px product-tab"
    >
      <div
        v-for="(item, index) in tabData"
        :key="item.id"
        class="tab_item h-40px pl-10px pr-10px bg-white cursor-pointer min-w-132px flex items-center text-#333"
        :class="{ active: currentContentIndex === index }"
        @click.stop="currentContentIndex = index"
      >
        <div class="center font-500 text-16px">
          {{ item.name }}
        </div>
        <div class="del-handle">
          <i
            class="edit-icon i-mx-edit block"
          />
          <i
            v-if="index !== 0"
            class="delete-icon i-mx-close block"
            alt="删除按钮"
          />
        </div>
      </div>
      <div
        class="h-100% w-40px delete_button center cursor-pointer"
      >
        +
      </div>
    </div>
    <div class="bottom_line mb-30px" />
  </div>
</template>

<style lang="scss" scoped>
.product-tab {
  & > div {
    &:first-child {
      border-radius: 10px 0 0 0;
      border-left-width: 1px;
      border-top-width: 1px;
    }

    border-width: 1px;
    border-color: #e5e5e5;
    border-style: solid;
    border-bottom-width: 0;

    &:last-child {
      border-radius: 0 10px 0 0;
      border-left-width: 0;
      border-right-width: 1px;
      border-top-width: 1px;
    }
  }

  & > div + div {
    border-left-width: 0;
  }
}
.tab_item {
  justify-content: center;
  &.active {
    background: #f0f2f5;
    position: relative;
    border-color: #f0f2f5 !important;
    &::after {
      content: '';
      display: block;
      position: absolute;
      left: 50%;
      bottom: 0;
      transform: translateX(-50%);
      background: #01bfbf;
      width: 32px;
      height: 3px;
    }
  }
  &:hover {
    justify-content: space-between;
    padding-left: 20px;
    padding-right: 10px;
    color: #00a7a7;
    .del-handle {
      display: flex;
    }
  }
  .del-handle {
    margin-left: 20px;
    display: none;
    align-items: center;
    justify-content: flex-end;
    .edit-icon,
    .delete-icon {
      cursor: pointer;
      width: 20px;
      height: 20px;
    }
    .edit-icon {
      margin-right: 10px;
    }
  }
}
.delete_button {
  border-top-right-radius: 10px;
  border: 1px solid #e5e5e5;
  color: #333333;
  &:hover {
    color: #00a7a7;
  }
}

.bottom_line {
  border-top: 0.5px solid #e5e5e5;
  height: 1px;
  margin-left: 20px;
  margin-right: 20px;
}
</style>


总结

这个实现例子虽然简单,但是对于小白来说,还是要一点时间去理解的,希望能帮助到您!

/deep/ .ant-tree { position: relative; /deep/ &::before { content: &#39;&#39;; width: 1px; height: 100%; height: calc(100% - 35px); position: absolute; border-left: 1px solid #d9d9d9; left: -6px; top: 18px; } /deep/ .ant-tree-treenode-switcher-open, /deep/ .ant-tree-treenode-switcher-close { position: relative; } >li:first-child:nth-last-child(1)::after { border: 0; } } /deep/ .ant-tree-treenode-switcher-close::after, /deep/ .ant-tree-treenode-switcher-open::after { content: &#39;&#39;; width: 8px; height: 1px; position: absolute; border-top: 1px solid #d9d9d9; left: -6px; top: 18px; } /deep/ .ant-tree > li:last-child::after { content: &#39;&#39;; width: 8px; height: 100%; border-top: 1px solid #d9d9d9; background-color: #fff; position: absolute; left: -6px; top: 18px; } /deep/ .ant-tree-treenode-switcher-close::before{ border: 0 !important; } /deep/.ant-tree-child-tree.ant-tree-child-tree-open > li:first-child { padding-top: 4px; } .last-menu-line() { position: absolute; left: 12px; width: 1px; height: 100%; margin: 22px 0 0; border-left: 1px solid #d9d9d9; content: &#39; &#39;; } ul.ant-tree.icon-tree /deep/li { & > .ant-tree-checkbox { margin-left: 24px; & + .ant-tree-node-content-wrapper > .ant-tree-title { > i:nth-child(1) { position: absolute; left: -42px; top: 5px; } } } } ul.ant-tree /deep/li { position: relative; &:nth-last-child(1) { /deep/&.ant-tree-treenode-switcher-open::before { .last-menu-line; height: calc(100% - 46px); } } &:before { position: absolute; left: 12px; width: 1px; height: 100%; height: calc(100% - 42px); margin: 22px 0 0; border-left: 1px solid #d9d9d9; content: &#39; &#39;; } .ant-tree-node-content-wrapper { padding: 0; position: relative; } ul.ant-tree-child-tree.ant-tree-child-tree-open > li { &::after { content: &#39;&#39;; position: absolute; width: 8px; height: 1px; border-top: 1px solid #d9d9d9; background: transparent; top: 15.5px; left: -5px; margin: 0; } &:nth-last-child(1) { &.ant-tree-treenode-switcher-open::before { .last-menu-line; height: calc(100% - 37px); } } } span.ant-tree-switcher { background: rgba(0, 0, 0, 0); } } div ul.ant-tree /deep/li.ant-tree-treenode-switcher-open { > ul.ant-tree-child-tree-open > li:nth-last-child(1) > ul::before { content: &#39;&#39;; width: 1px; height: 100%; position: absolute; background: #fff; left: -6px; top: 16px; } > ul.ant-tree-child-tree-open > li:nth-last-child(1) > span { &.ant-tree-node-content-wrapper::before { content: &#39;&#39;; width: 1px; position: absolute; background: #fff; left: -30px; top: 13px; height: calc(100% + 10px); } &.ant-tree-checkbox + .ant-tree-node-content-wrapper::before { top: 13px; left: -52px; } } > .ant-tree-node-content-wrapper-normal::before { content: &#39;&#39;; width: 1px; height: 100%; position: absolute; background: #fff; left: -12px; top: 16px; } }vue3写法
最新发布
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明似水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值