返回

Vue3动态标题,点击变粗和底部提示

前端

欢迎来到“Vue3动态标题,点击变粗和底部提示”教程!本文将分步讲解如何使用Vue3实现标题点击变粗且底部有提示的效果。

  1. 准备工作:
  • 确保您拥有基本的Vue3知识和开发环境。
  • 打开您的代码编辑器并创建一个新的Vue3项目。
  1. 组件创建:
  • src文件夹下创建一个新的组件文件,例如Title.vue
<template>
  <div class="title-container">
    <h1 @click="toggleActive" :class="{ active: isActive }">{{ title }}</h1>
    <div class="line"></div>
  </div>
</template>

<script>
import { ref } from 'vue';

export default {
  setup() {
    const title = ref('Vue3动态标题');
    const isActive = ref(false);

    const toggleActive = () => {
      isActive.value = !isActive.value;
    };

    return { title, isActive, toggleActive };
  },
};
</script>

<style>
.title-container {
  position: relative;
  width: 100%;
  padding: 20px;
}

h1 {
  font-size: 24px;
  font-weight: normal;
  cursor: pointer;
}

.active {
  font-weight: bold;
}

.line {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: #000;
  opacity: 0;
  transition: all 0.2s ease-in-out;
}

.active ~ .line {
  opacity: 1;
}
</style>
  1. 应用组件:
  • App.vue文件中引入并使用刚创建的组件。
<template>
  <div id="app">
    <Title :title="'Vue3动态标题,点击变粗和底部提示'" />
  </div>
</template>

<script>
import Title from './components/Title.vue';

export default {
  components: { Title },
};
</script>
  1. 运行项目:
  • 在终端运行npm run serveyarn serve来启动项目。
  1. 效果展示:
  • 打开浏览器,访问http://localhost:8080,即可看到一个标题。
  • 单击标题,观察标题变粗且底部出现黑色细线。

总结:
本文演示了如何使用Vue3实现一个标题点击变粗且底部有提示的效果。这只是一个简单的例子,您可以在此基础上进行扩展,实现更复杂的效果。希望本文对您有所帮助,请继续关注我的其他文章和教程。