admin管理员组文章数量:1024614
Summary
I would like a <div>
to be square (height
equal to width
) when the size of the parent <div>
changes due to a change of its content (which can span one or more lines).
Details
The element design is
My idea is to watch for the changes of the text ("some text here" above) and recalculate the width
of the square (the one with "a" above) so that it is equal to the height
of the parent container (the margins on the drawing above are for illustration and are normally equal to zero).
I was expecting the following code (JSFiddle version) to do the trick:
var vm = new Vue({
el: "#root",
data: {
text: "this is some text<br>and some more"
},
watch: {
text: function() {
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
}
}
})
#container {
display: flex;
flex-direction: row;
}
div {
border-style: solid;
border-width: 1px;
}
<script src=".0.3/vue.js"></script>
<div id="root">
<div id="container">
<div id="square">
a
</div>
<div v-html="text">
</div>
</div>
</div>
Summary
I would like a <div>
to be square (height
equal to width
) when the size of the parent <div>
changes due to a change of its content (which can span one or more lines).
Details
The element design is
My idea is to watch for the changes of the text ("some text here" above) and recalculate the width
of the square (the one with "a" above) so that it is equal to the height
of the parent container (the margins on the drawing above are for illustration and are normally equal to zero).
I was expecting the following code (JSFiddle version) to do the trick:
var vm = new Vue({
el: "#root",
data: {
text: "this is some text<br>and some more"
},
watch: {
text: function() {
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
}
}
})
#container {
display: flex;
flex-direction: row;
}
div {
border-style: solid;
border-width: 1px;
}
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.0.3/vue.js"></script>
<div id="root">
<div id="container">
<div id="square">
a
</div>
<div v-html="text">
</div>
</div>
</div>
but the width of the square is not updated.
Is there something fundamentally flawed with this approach?
I thought that this may be a matter of the new width not being ready yet in the watch so I tried to use Vue.nextTick()
to defer the calculation but it did not solve the issue (forked JSFiddle):
var vm = new Vue({
el: "#root",
data: {
text: "this is some text<br>and some more"
},
watch: {
text: function() {
Vue.nextTick(function() {
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
})
}
}
})
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Jan 9, 2017 at 9:12
WoJWoJ
30.2k58 gold badges214 silver badges406 bronze badges
1 Answer
Reset to default 4You have to use:
document.getElementById("square").setAttribute("style","width:"+ document.getElementById("container").offsetHeight + 'px')
instead of:
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
Then, in order to account for the chnage, Vue.nextTick()
also needs to be used.
Finally, changes will be visible after a modification of text
.
The modified JS code:
var vm = new Vue({
el: "#root",
data: {
text: ""
},
watch: {
text: function() {
Vue.nextTick(function() {
document.getElementById("square").setAttribute("style","width:"+ document.getElementById("container").offsetHeight + 'px')
})
}
}
})
setTimeout(function() {
vm.text = "this is some text<br>and some more"
}, 3000)
and its JSFiddle example.
Summary
I would like a <div>
to be square (height
equal to width
) when the size of the parent <div>
changes due to a change of its content (which can span one or more lines).
Details
The element design is
My idea is to watch for the changes of the text ("some text here" above) and recalculate the width
of the square (the one with "a" above) so that it is equal to the height
of the parent container (the margins on the drawing above are for illustration and are normally equal to zero).
I was expecting the following code (JSFiddle version) to do the trick:
var vm = new Vue({
el: "#root",
data: {
text: "this is some text<br>and some more"
},
watch: {
text: function() {
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
}
}
})
#container {
display: flex;
flex-direction: row;
}
div {
border-style: solid;
border-width: 1px;
}
<script src=".0.3/vue.js"></script>
<div id="root">
<div id="container">
<div id="square">
a
</div>
<div v-html="text">
</div>
</div>
</div>
Summary
I would like a <div>
to be square (height
equal to width
) when the size of the parent <div>
changes due to a change of its content (which can span one or more lines).
Details
The element design is
My idea is to watch for the changes of the text ("some text here" above) and recalculate the width
of the square (the one with "a" above) so that it is equal to the height
of the parent container (the margins on the drawing above are for illustration and are normally equal to zero).
I was expecting the following code (JSFiddle version) to do the trick:
var vm = new Vue({
el: "#root",
data: {
text: "this is some text<br>and some more"
},
watch: {
text: function() {
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
}
}
})
#container {
display: flex;
flex-direction: row;
}
div {
border-style: solid;
border-width: 1px;
}
<script src="https://cdnjs.cloudflare./ajax/libs/vue/2.0.3/vue.js"></script>
<div id="root">
<div id="container">
<div id="square">
a
</div>
<div v-html="text">
</div>
</div>
</div>
but the width of the square is not updated.
Is there something fundamentally flawed with this approach?
I thought that this may be a matter of the new width not being ready yet in the watch so I tried to use Vue.nextTick()
to defer the calculation but it did not solve the issue (forked JSFiddle):
var vm = new Vue({
el: "#root",
data: {
text: "this is some text<br>and some more"
},
watch: {
text: function() {
Vue.nextTick(function() {
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
})
}
}
})
Share
Improve this question
edited Jun 20, 2020 at 9:12
CommunityBot
11 silver badge
asked Jan 9, 2017 at 9:12
WoJWoJ
30.2k58 gold badges214 silver badges406 bronze badges
1 Answer
Reset to default 4You have to use:
document.getElementById("square").setAttribute("style","width:"+ document.getElementById("container").offsetHeight + 'px')
instead of:
document.getElementById("square").style.width = document.getElementById("container").offsetHeight
Then, in order to account for the chnage, Vue.nextTick()
also needs to be used.
Finally, changes will be visible after a modification of text
.
The modified JS code:
var vm = new Vue({
el: "#root",
data: {
text: ""
},
watch: {
text: function() {
Vue.nextTick(function() {
document.getElementById("square").setAttribute("style","width:"+ document.getElementById("container").offsetHeight + 'px')
})
}
}
})
setTimeout(function() {
vm.text = "this is some text<br>and some more"
}, 3000)
and its JSFiddle example.
本文标签: javascriptHow to set a ltdivgt style in reaction to a Vuejs data member changeStack Overflow
版权声明:本文标题:javascript - How to set a <div> style in reaction to a Vue.js data member change? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745585503a2157567.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论