【发布时间】:2020-09-12 13:11:25
【问题描述】:
我试图强制两个b-buttons(来自Buefy 的按钮组件)保持与其中较宽的一个一样大。
我认为我需要将一个按钮的最小宽度设置为另一个按钮的宽度值。这样一来,如果一个按钮变大,另一个按钮也会变大。
现在,我该怎么做?
我的第一个想法是为每个按钮设置绑定到“最小宽度”样式,如下所示:
<b-button ref="firstButton" type="is-primary" size="is-large"
:style="{ 'min-width': $refs.secondButton !== undefined ? $refs.secondButton.$el.clientWidth + 'px' : 0 }">
First Button
</b-button>
<b-button ref="secondButton" type="is-primary" size="is-large"
:style="{ 'min-width': $refs.firstButton !== undefined ? $refs.firstButton.$el.clientWidth + 'px' : 0 }">
Second Button
</b-button>
但这几乎不起作用,看起来它在页面渲染时工作一次,并且大小的任何变化(例如由于本地化)都会被忽略。看起来$ref 没有反应。
我的第二个镜头涉及一个计算属性,与前一个镜头的想法相同,但不是将样式设置为内联,而是设置为一个计算属性。
但这一次,$ref 元素是未定义的,看起来计算是在设置之前发生的。
如何使两个按钮的宽度相同,而不设置固定值,并且能够随着按钮大小的增加/减小而调整大小?
这是整个代码:
(它只是被英雄部分包围的两个按钮)
<template>
<div>
<section class="hero is-primary is-bold is-relative">
<div class="hero-body">
<div class="section">
<div class="container">
<!-- $refs.secondButton !== undefined ? $refs.secondButton.$el.clientWidth + 'px' : 0 -->
<!-- $refs.firstButton !== undefined ? $refs.firstButton.$el.clientWidth + 'px' : 0 -->
<div class="columns is-centered is-multiline is-mobile">
<div class="column is-narrow has-text-centered">
<b-button ref="firstButton" type="is-primary" size="is-large"
:style="{ 'min-width': $refs.secondButton !== undefined ? $refs.secondButton.$el.clientWidth + 'px' : 0 }">
First Button
</b-button>
</div>
<div class="column is-narrow has-text-centered">
<b-button ref="secondButton" type="is-primary" size="is-large"
:style="{ 'min-width': $refs.firstButton !== undefined ? $refs.firstButton.$el.clientWidth + 'px' : 0 }">
Second Button
</b-button>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</template>
<script>
export default {
}
</script>
<style lang="scss" scoped>
//All from Buefy and Bulma.
</style>
【问题讨论】:
-
你的css在哪里?
-
我认为你可以创建属性并控制它们的宽度。所以它们总是相同的宽度
-
@Dejan.S 这一切都来自 Buefy/Bulma。我没有使用自定义 CSS。无论如何,这并不重要,因为它所做的只是集中两个按钮并添加一些边距。
-
@AsiPle 不确定。我的问题是获取按钮的宽度,因为它们会因本地化而改变大小。
-
由于本地化,您可以更改属性,按钮将更改为。也许您想说它们由于内部内容而具有宽度,您需要将最长按钮的宽度设置为另一个?
标签: javascript css vue.js vuejs2 buefy