在我的 VueJS 组件中,我从以下位置获取div
数据:
this.$refs.mydiv.getBoundingClientRect().top
然后我用它来计算和设置其他div
的高度。
但是,每次我重新加载页面时,都需要清除浏览器缓存,否则div
的高度会混乱。
我该如何解决?
下面是我与 Laravel 一起使用的 VueJS 组件(不确定是否有可能使其适应Stackoverflow片段)。
<template>
<div class="row">
<div
class="col- tree"
ref="perbase"
>
<ul>
<li>
<a ref="per1">Grand parent</a>
<ul ref="per2">
<li>
<a>parent 1</a>
</li>
<li>
<a>parent 2</a>
</li>
<li>
<a>parent 3</a>
<ul ref="per3">
<li>
<a>child 1</a>
</li>
<li>
<a>child 2</a>
<ul ref="per4">
<li>
<a>grand child</a>
<ul>
<li>
<a>grand grand child</a>
<ul ref="per5">
<li>
<a>grand grand grand child</a>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div
class="periods col-"
ref="periods"
>
<div
class="period p1"
v-bind:style="{ height: p1height + 'px' }"
>1</div>
<div
class="period p2"
v-bind:style="{ height: p2height + 'px' }"
>2</div>
<div
class="period p3"
v-bind:style="{ height: p3height + 'px' }"
>3</div>
<div
class="period p4"
v-bind:style="{ height: p4height + 'px' }"
>4</div>
<div
class="period p5"
v-bind:style="{ height: p5height + 'px' }"
>5</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
isMounted: false,
base: '',
botP1: '',
botP2: '',
botP3: '',
botP4: '',
botP5: '',
p1height: '',
p2height: '',
p3height: '',
p4height: '',
p5height: ''
};
},
mounted() {
this.base = this.$refs.perbase.getBoundingClientRect().top;
this.botP1 = this.$refs.per2.getBoundingClientRect().bottom;
this.botP2 = this.$refs.per3.getBoundingClientRect().bottom;
this.botP3 = this.$refs.per4.getBoundingClientRect().bottom;
this.botP4 = this.$refs.per5.getBoundingClientRect().bottom;
this.botP5 = this.$refs.perbase.getBoundingClientRect().bottom;
this.p1height = this.botP1 - this.base - 10;
this.p2height = this.botP2 - this.botP1;
this.p3height = this.botP3 - this.botP2 - 10;
this.p4height = this.botP4 - this.botP3;
this.p5height = this.botP5 - this.botP4 + 20;
},
};
</script>
<style lang="css" scoped>
div.periods {
width: 50px;
}
div.period {
text-align: center;
vertical-align: middle;
}
div.p1 {
background-color: tomato;
margin-top: 10px;
}
div.p2 {
background-color: orange;
}
div.p3 {
background-color: yellowgreen;
}
div.p4 {
background-color: royalblue;
}
div.p5 {
background-color: seagreen;
}
/*Now the CSS*/
* {
margin: 0;
padding: 0;
}
.tree ul {
padding-top: 20px;
position: relative;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
.tree li {
float: left;
text-align: center;
list-style-type: none;
position: relative;
padding: 20px 5px 0 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
/*We will use ::before and ::after to draw the connectors*/
.tree li::before,
.tree li::after {
content: "";
position: absolute;
top: 0;
right: 50%;
border-top: 1px solid #ccc;
width: 50%;
height: 20px;
}
.tree li::after {
right: auto;
left: 50%;
border-left: 1px solid #ccc;
}
/*We need to remove left-right connectors from elements without
any siblings*/
.tree li:only-child::after,
.tree li:only-child::before {
display: none;
}
/*Remove space from the top of single children*/
.tree li:only-child {
padding-top: 0;
}
/*Remove left connector from first child and
right connector from last child*/
.tree li:first-child::before,
.tree li:last-child::after {
border: 0 none;
}
/*Adding back the vertical connector to the last nodes*/
.tree li:last-child::before {
border-right: 1px solid #ccc;
border-radius: 0 5px 0 0;
-webkit-border-radius: 0 5px 0 0;
-moz-border-radius: 0 5px 0 0;
}
.tree li:first-child::after {
border-radius: 5px 0 0 0;
-webkit-border-radius: 5px 0 0 0;
-moz-border-radius: 5px 0 0 0;
}
/*Time to add downward connectors from parents*/
.tree ul ul::before {
content: "";
position: absolute;
top: 0;
left: 50%;
border-left: 1px solid #ccc;
width: 0;
height: 20px;
}
.tree li a {
border: 1px solid #ccc;
padding: 5px 10px;
text-decoration: none;
color: #666;
font-family: arial, verdana, tahoma;
font-size: 11px;
display: inline-block;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
transition: all 0.5s;
-webkit-transition: all 0.5s;
-moz-transition: all 0.5s;
}
/*Time for some hover effects*/
/*We will apply the hover effect the the lineage of the element also*/
.tree li a:hover,
.tree li a:hover + ul li a {
background: #c8e4f8;
color: #000;
border: 1px solid #94a0b4;
}
/*Connector styles on hover*/
.tree li a:hover + ul li::after,
.tree li a:hover + ul li::before,
.tree li a:hover + ul::before,
.tree li a:hover + ul ul::before {
border-color: #94a0b4;
}
</style>