CSS下拉框收缩动画 Vue组件

实现效果

在这里插入图片描述


思路

利用CSS的 scaleY() 方法,将div在垂直方向上缩放,实现缩放效果。

  • 收起状态: transform: scaleY(0);
  • 展开状态: transform: scaleY(1);
  • 缩放中心设置在顶部: transform-origin: center top;
  • 动画效果: transition: all ease-in-out .3s;

代码示例

1. 定义 ToolButton 组件

// ToolButton 组件
<template>
  <div class="tool-btn">
    <div class="btn" @click="handleBtnClick">
      <span><slot name="btn-name" /></span>
      <i :class="['el-icon-caret-bottom', expandVisible ? 'active':'']"></i>
    </div>
    <div :class="['expand-box', expandVisible ? 'active':'']">
      <slot name="content" />
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      expandVisible: false
    }
  },
  methods:{
    handleBtnClick(){
      this.expandVisible = !this.expandVisible
    }
  }
}
</script>

<style lang="scss">
.tool-btn{
  z-index: 99;
  position: absolute;
  top: 10vh;
  left: 26vw;
  font-size: 0.91vw;
  font-family: SimHei;
  color: #fff;
  .btn{
    display: flex;
    height: 2.4vw;
    width: 7vw;
    align-items: center;
    justify-content: center;
    background-color: rgba(9, 31, 120, 0.6);
    &:hover{
      cursor: pointer;
    }
    i{
      margin-left: 3px;
      transition: all ease-in-out .3s;
      transform:rotate(0deg);
      &.active{
        transform:rotate(-180deg);
      }
    }
  }
  .expand-box{
    position: relative;
    margin-top: 5px;
    background-color: rgba(9, 31, 120, 0.6);
    transition: all ease-in-out .3s; // 过渡动画
    width: 150px;
    transform-origin: center top; // 缩放中心设置在面板顶部
    transform: scaleY(0);  // 面板收起
    max-height: 0; // 收起时div不占高度
    &.active{
      transform: scaleY(1); // 面板展开
      max-height: 200px;  // heigth:auto 无法触发过渡
    }
  }
}
</style>

2. 调用组件

<template>
  <tool-button class="planning-tool">
    <template #btn-name>
      规划归集
    </template>
    <template #content>
      XXXXXX
    </template>
  </tool-button>
</template>

<script>
import ToolButton from '@/components/ToolButton'

export default {
  components:{
    ToolButton
  }
}
</script>

<style lang="scss">
.planning-tool{
  margin-left: calc(6vw + 20px);
}
</style>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现vue切换下拉框刷新子组件组件中的信息,可以使用`props`和`watch`来实现组件之间的通信。 首先,在父组件中定义一个`selected`的数据属性,并将其传递给子组件作为`props`。例如: ```html <template> <div> <select v-model="selected"> <option v-for="option in options" :value="option.value">{{ option.label }}</option> </select> <child-component :selected="selected" /> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { selected: '', options: [ { value: 'option1', label: '选项1' }, { value: 'option2', label: '选项2' } ] } } } </script> ``` 在这里,我们将`selected`作为`props`传递给了子组件`ChildComponent`。 然后,在子组件中,可以使用`watch`来监听`selected`的变化,并在变化时更新子组件的数据。例如: ```html <template> <div> <p>当前选中的选项是:{{ selected }}</p> <p>其他组件的信息:{{ otherInfo }}</p> </div> </template> <script> export default { props: { selected: { type: String, required: true } }, data() { return { otherInfo: '' } }, watch: { selected: { immediate: true, handler(selected) { // 根据选中的选项更新子组件的信息 this.otherInfo = `选项${selected}的信息` } } } } </script> ``` 在这里,我们使用了`watch`来监听`selected`的变化,并在变化时更新了子组件的数据。同时,我们设置了`immediate`为`true`,以便在组件初始化时立即执行`handler`函数,从而初始化子组件的数据。 通过这种方式,我们就可以实现在vue切换下拉框时刷新子组件组件中的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值