原则上,我想做下面列出的事情.该页有两种可能的状态.在第一个标题中,两个文本(它们的长度可能不同)集中在一起作为标题.对于第二种状态,文本在页面上的不同位置分别居中.两种状态之间的转换是通过按一下按钮启动的,并顺利进行.

Sketch of Positions

我的代码是用Vue编写的,但我认为在这里我需要一些CSS帮助.这是我到目前为止最好的try .

export default {
  name: 'App',
  data() {
    return {
      solved: false,
    };
  },
  methods: {
    change_solved() {
      this.solved = !this.solved;
    },
  },
};
.cat_num {
  position: fixed;
  transition: all 2s ease;
  display: inline;
}

.cat_num.active {
  font-size: 40px;
  top: 100px;
}

.cat_name {
  position: fixed;
  transition: all 2s;
  display: inline;
}

.cat_name.active {
  font-size: 50px;
  top: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<template>
<button v-on:click="change_solved">Solve</button>
<h1 class="cat_num" :class="{active : solved}">Text 1</h1>
<h1 class="cat_name" :class="{active : solved}" >Text 2 (possibly lengthy)</h1>
</template>

Vue SFC Playground

这一转变运作良好.问题是,我无法实现文本的共同对齐.

如有任何帮助或意见,我们将非常感激.这感觉像是一个非常简单的问题.然而,我已经花了相当长的时间来研究它.谢谢!

推荐答案

我们可以使用position: absolute的简单定位,并将其应用于两个文本.然后,我们可以将文本相对于中心锚点定位,使它们居中,如下所示:

var app = new Vue({
  el: "#app",
  data: {
    solved: false,
  },
  methods: {
    change_solved() {
      this.solved = !this.solved;
    },
  },
})
.container {
  position: relative;
}

.cat_num {
  transition: all 2s ease;
  position: absolute;
  right: 50%;
  top: 0;
  transform: translate(0, 0);
}

.cat_num.active {
  font-size: 40px;
  top: 100px;
  transform: translate(50%, 0);
  text-align: center;
}

.cat_name {
  transition: all 2s;
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(0, 0);
}

.cat_name.active {
  font-size: 50px;
  top: 200px;
  transform: translate(-50%, 0);
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <button v-on:click="change_solved">Solve</button>
  <div class="container">
    <h1 class="cat_num" :class="{active : solved}">Text 1</h1>
    <h1 class="cat_name" :class="{active : solved}">Text 2 (possibly lengthy)</h1>
  </div>
</div>

长文本可能是问题所在,因为如果长文本超过一行,就很难使转换更流畅,因为文本对齐方式发生了变化(使用text-align: left表示第二个状态会使其看起来不好,如果我们使用text-align: center表示第一个状态,它也会看起来不好).

EDIT:个 要将两个文本作为一个整体居中,我们可以对它们使用内联显示,并将初始位置设置为静态.

var app = new Vue({
  el: "#app",
  data: {
    solved: false,
  },
  methods: {
    change_solved() {
      this.solved = !this.solved;
    },
  },
})
.container {
  position: relative;
  text-align: center;
}

.cat_num {
  transition: all 2s ease;
  right: 50%;
  top: 0;
  transform: translate(0, 0);
  display: inline;
  position: static;
}

.cat_num.active {
  font-size: 40px;
  top: 100px;
  transform: translate(50%, 0);
  text-align: center;
  position: absolute;
}

.cat_name {
  transition: all 2s;
  left: 50%;
  top: 0;
  transform: translate(0, 0);
  display: inline;
  position: static;
}

.cat_name.active {
  font-size: 50px;
  top: 200px;
  transform: translate(-50%, 0);
  text-align: center;
  position: absolute;
  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <button v-on:click="change_solved">Solve</button>
  <div class="container">
    <h1 class="cat_num" :class="{active : solved}">Text 1</h1>
    <h1 class="cat_name" :class="{active : solved}">Text 2 (possibly lengthy)</h1>
  </div>
</div>

但正如上面的代码片段所示,在转换到第二个状态时,它会感觉像跳到了中间(这是因为我们更改了position属性.要解决这个问题,我们可能需要一个小的脚本来保持positionabsolute,并计算leftright的值.

var app = new Vue({
  el: "#app",
  data: {
    solved: false,
    isMounted: false,
  },
  computed: {
    textsTotalWidth() {
      if (!this.isMounted) {
        return 0
      }
      
      return (this.$refs.catNum.offsetWidth + this.$refs.catName.offsetWidth) / 2
    },
    catNumRight() {
      if (!this.isMounted) {
        return ''
      }
      
      return `calc(50% + ${this.textsTotalWidth - this.$refs.catNum.offsetWidth}px)`
    },
    catNameLeft() {
      if (!this.isMounted) {
        return ''
      }
      
      return `calc(50% - ${this.textsTotalWidth - this.$refs.catNum.offsetWidth}px)`
    }
  },
  methods: {
    change_solved() {
      this.solved = !this.solved;
    },
  },
  mounted() {
    this.isMounted = true;
  }
})
.container {
  position: relative;
}

.cat_num {
  transition: all 2s ease;
  position: absolute;
  top: 0;
  transform: translate(0, 0);
}

.cat_num.active {
  font-size: 40px;
  top: 100px;
  transform: translate(50%, 0);
  text-align: center;
  right: 50% !important;
}

.cat_name {
  transition: all 2s;
  position: absolute;
  top: 0;
  transform: translate(0, 0);
}

.cat_name.active {
  font-size: 50px;
  top: 200px;
  transform: translate(-50%, 0);
  text-align: center;
  left: 50% !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <button v-on:click="change_solved">Solve</button>
  <div class="container">
    <h1 ref="catNum" class="cat_num" :class="{active : solved}" :style="{'right':catNumRight}">Text 1</h1>
    <h1 ref="catName" class="cat_name" :class="{active : solved}" :style="{'left':catNameLeft}">Text 2 (possibly lengthy)</h1>
  </div>
</div>

Html相关问答推荐

我的表的第一列似乎是推其他2列到右边,我不能改变它

我已经在我的网站上创建了按钮,但我不能让它们正确排列.'

如何在htmlAngular 17的@for中使用索引

CURL邮箱中的图像不能调整其大小

将弹性容器设置为内容宽度

带有MathJax SVG的HTML代码在XHTML中不起作用

有没有一种方法可以动态地从网格或Flexbox中取出HTML元素?

如何在将文本垂直居中的同时将文本右对齐?

IFRAME中的Chrome图像未调整大小

如何使活动选项卡背景作为父背景图像

伪元素:after和溢出隐藏

在Firefox中使用keySplines时,SVG: <animateMotion>不起作用

如何动态调整彼此重叠的两个 div 的大小,使它们在增长时保持相同的高度

是否可以使 huxtable 输出悬停?

如何在react 中向按钮添加禁用属性?

如何使用 :before 在 CSS 中为列表增加计数器

如何创建带有偏移边框线的双色边框?

浮动元素忽略 margin-top 属性后的块元素

让 Bootstrap 5 模式和背景占据父容器的宽度和高度,而不是全屏

如何在锚点可点击的伪元素周围制作空白?