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'
};
相关推荐
多加点辣也没关系4 小时前
JavaScript|第4章:类型转换
开发语言·javascript
yqcoder4 小时前
httpOnly 是什么,又有什么用?
开发语言·前端·javascript
烬羽4 小时前
还在手动拼路径、写回调地狱?一文吃透 Node.js 的 path 和 fs
javascript·程序员·node.js
mONESY4 小时前
LangChain JS 高性能并行执行解析、工具调用底层封装
javascript
子兮曰5 小时前
TypeScript 7.0 RC 深度解读:Go 重写完成,10 倍提速,编译器的「奇点时刻」
前端·后端·typescript
迅速的自行车5 小时前
从一段模板说起
前端·javascript·vue.js
YIAN6 小时前
LangChain JS 工具调用实战:基于 DeepSeek-V4-Flash 实现本地文件读取 AI 代码助手(完整可运行源码)
javascript·langchain·前端框架
BAIGAOa6 小时前
不止 React:我开源了一个框架无关的终端键盘引擎,Vue/Svelte 都能用
typescript
尼斯湖皮皮怪6 小时前
iceCoder 记忆系统深度分析
前端·javascript
小蚂蚁i6 小时前
把 TypeScript 内置工具类型彻底搞懂,顺便自己封几个好用的
前端·typescript