Vue3(TypeScript)-CSSProperties代码示例及用法详解

Vue3-CSSProperties代码示例及用法

在Vue3中封装一个可复用的公共CSSProperties

c 复制代码
// 目录 /xxx/src/hooks/use-scroll-updown-bottom-content/index.ts 全局定义hooks
import { computed } from 'vue'
import type { CSSProperties } from 'vue'

type UpDownOperationType = 'up' | 'down'

export const useScrollUpdownBottomContent = (
  type: UpDownOperationType = 'up',
  extraHeight = 0
) => {
  const style = computed<CSSProperties>(() => {
    const style: CSSProperties = {}
    if (type === 'up') {
      style.transform = `translateY(calc(100% + ${extraHeight}px))`
    } else if (type === 'down') {
		style.transform = `translateY(calc(100% - ${extraHeight}px))`
    }
    style.transitionDuration = '0.25s'
    style.transitionTimingFunction = 'linear'
    style.transitionProperty = 'transform'
    return style
  })
  return {
    style
  }
}
c 复制代码
// 目录 xxx/pages/pages/index.vue 业务组件中使用
import { useScrollUpdownBottomContent } from '@/hooks'
const { style: authInfoScrollStyle } =  useScrollUpdownBottomContent('up', 80)
console.log(authInfoScrollStyle)
// {
//   "transform": "translateY(calc(100% + 80px))",
//   "transitionDuration": "0.25s",
//   "transitionTimingFunction": "linear",
//   "transitionProperty": "transform"
// } 

简单例子,TypeScript中定义CSS Properties

在多个地方重复使用此样式信息,提高代码的可复用性。

c 复制代码
interface CustomStyles {
  backgroundColor: string;
  color: string;
  fontSize: string;
}

const customStyles: CustomStyles = {
  backgroundColor: 'blue',
  color: 'white',
  fontSize: '16px'
};
相关推荐
多加点辣也没关系9 小时前
JavaScript|第31章:表单与控件
开发语言·javascript·ecmascript
Highcharts.js12 小时前
常见报错排雷指南2:导出失败的官方解法
javascript·react.js·ecmascript·highcharts·可视化图表·导出模块失败·导出服务
梦想平凡12 小时前
百游棋牌源代码开发搭建教程(五):房间创建、座位分配与请求幂等实现
前端·javascript·数据库·源代码管理
杨芋爽14 小时前
管理后台列表页为什么一直转圈?从一次 1+N 请求扇出聊聊前端性能优化
javascript
_山海15 小时前
Bun入门指南
前端·javascript·后端
掰头战士15 小时前
三道保险丝,最后只能放弃治疗? 一文聊聊我的agent是怎么做死循环检测的
typescript·llm·agent
YHL15 小时前
🎯 JavaScript 单例模式(Singleton Pattern)—— 从理论到实战
javascript·设计模式
vx_Biye_Design16 小时前
springboot游泳馆系统93765-计算机课程设计、毕业设计
java·javascript·spring boot·后端·python·spring·课程设计