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'
};
相关推荐
梦65014 分钟前
Vue 单页面应用 (SPA) 与 多页面应用 (MPA) 对比
前端·javascript·vue.js
清铎19 分钟前
大模型训练_week3_day15_Llama概念_《穷途末路》
前端·javascript·人工智能·深度学习·自然语言处理·easyui
岛泪33 分钟前
把 el-cascader 的 options 平铺为一维数组(只要叶子节点)
前端·javascript·vue.js
zwtahql42 分钟前
ubuntu远程ssh连接
linux·ubuntu·ssh
lendsomething1 小时前
graalvm使用实战:在java中执行js脚本
java·开发语言·javascript·graalvm
冰暮流星2 小时前
javascript的switch语句介绍
java·前端·javascript
s_daqing2 小时前
ubuntu(arm)使用nginx安装静态服务器
服务器·nginx·ubuntu
小简GoGo2 小时前
前端常用设计模式快速入门
javascript·设计模式
利刃大大2 小时前
【ES6】变量与常量 && 模板字符串 && 对象 && 解构赋值 && 箭头函数 && 数组 && 扩展运算符 && Promise/Await/Async
开发语言·前端·javascript·es6
天若有情6732 小时前
ES6 模块与 CommonJS 的区别详解
前端·javascript·es6