04——增加动态表单关联线

108 阅读1分钟

版本一

html:
​
<div class="gather">
   <div class="tree">123</div>
   <div class="tree">123</div>
   <div class="tree">123</div>
   <div class="tree">123</div>
   <div class="tree">123</div>
</div>
css:
​
<style lang="scss">
.tree {
   position: relative;
}
.gather {
   width30px;
   position: relative;
   border1px solid red;
}
.gather::after {
   content"";
   display: block;
   position: absolute;
   width3px;
   height100%;
   background: red;
   right: -23px;
   top0;
}
.tree::after {
   content"";
   display: block;
   position: absolute;
   width20px;
   height3px;
   background: red;
   right: -20px;
   top50%;
}
</style>

效果:

image.png

版本二

需求:点击表单增加按钮,自动增加一个input框,并增加关联样式,参考效果图;

html:
​
<el-form
   ref="form"
   :model="form"
   label-width="80px"
   class="form"
   :style="{ '--height': height + 'px' }"//这是改变伪元素的写法
>
   <div v-for="(item, index) in form.list" :key="index">
       <el-form-item label="活动名称">
           <el-input
               v-model="item.name"
               class="input tree1"
           ></el-input>
       </el-form-item>
   </div>
</el-form>
<button @click="btn">增加</button>
js:
​
<script>
export default {
   name"name",
   data() {
       return {
           height0,
           form: {
               list: [
                  {
                       name"123",
                  },
              ],
          },
      };
  },
   methods: {
       btn() {
           this.form.list.push({ name"456" });
           let box = document.getElementsByClassName("form")[0];
           //计算节点高度,最后一个input框的高度减去第一个input框的高度//重点:动态计算高度
           setTimeout(() => {
               let rectObject =box.firstElementChild.getBoundingClientRect().top;
               let rectObject1 =box.lastElementChild.getBoundingClientRect().top;
               this.height = rectObject1 - rectObject;
          }, 100);
      },
  },
};
</script>
<style lang="scss">
   .input {
       width220px;
  }
   .form {
       position: relative;
       width320px;
      // height300px;
       border10px solid blue;
       border1px solid red;
  }
   .form::after {
       content"";
       display: block;
       position: absolute;
       width3px;
       heightvar(--height);
       background: red;
       right: -3px;
       top20px;
  }
   .tree1::after {
       content"";
       display: block;
       position: absolute;
       width20px;
       height3px;
       background: red;
       right: -20px;
       top50%;
  }
</style>

效果:

image.png