vue实现input获取焦点后label标签上移缩小

该博客展示了如何在移动端使用Vue.js实现输入框的聚焦和失去焦点时的样式变化,通过修改`rem`单位来适应不同设备。文章详细描述了在`focus`和`blur`事件中对元素样式调整的代码实现,包括字体大小、颜色和边框效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于移动端的,如果是PC端要用,将rem改成需要的px大小即可

<template>

  <div id="app">

    <div class="itemInput">

      <label for="name">姓名</label>

      <input type="text" v-model="name" @focus="focusEvent($event)" @blur="blurEvent($event)" class="form-line">

    </div>

  </div>

</template>

 

<script>

import _ from "lodash/lodash";

export default {

  data() {

    return {

      name: ''

    }

  },

  methods: {

    focusEvent(e) {

      e.currentTarget.previousElementSibling.style.top = '0'

      e.currentTarget.previousElementSibling.style.fontSize = '0.32rem'

      e.currentTarget.previousElementSibling.style.color = '#343434'

      e.currentTarget.parentElement.style.borderBottom = '1px solid #E4504C'

    },

    blurEvent(e) {

      if (this.name === '') {

        e.currentTarget.previousElementSibling.style.top = '0.48rem'

        e.currentTarget.previousElementSibling.style.fontSize = '0.48rem'

        e.currentTarget.previousElementSibling.style.color = '#cccccc'

        e.currentTarget.parentElement.style.borderBottom = '1px solid #e2e2e2'

      }

 

    }

  },

  components: {

 

  }

}

</script>

 

<style scoped>

#app {

  padding: 40px;

}

.itemInput {

  position: relative;

  padding: 20px 0 0;

  box-sizing: border-box;

  border-bottom: 1px solid #e2e2e2;

}

.itemInput > label {

  position: absolute;

  left: 10px;

  top: 36px;

  transition: 0.3s;

  font-size: 36px;

  color: #cccccc;

}

.itemInput > input {

  background: none;

  outline: none;

  border: none;

  width: 100%;

  font-size: 36px;

  padding: 20px 10px;

  box-sizing: border-box;

  caret-color: #e4504c;

}

</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值