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'
};
相关推荐
鹿鸣天涯29 分钟前
使用VMware Workstation Pro搭建Ubuntu服务器虚拟机
ubuntu
linjoe994 小时前
【Deep Learning】Ubuntu配置深度学习环境
人工智能·深度学习·ubuntu
bug攻城狮6 小时前
解决Ubuntu中apt-get -y安装时弹出交互提示的问题
linux·运维·ubuntu
雾恋6 小时前
最近一年的感悟
前端·javascript·程序员
华仔啊6 小时前
Vue3 的 ref 和 reactive 到底用哪个?90% 的开发者都选错了
javascript·vue.js
xiachong276 小时前
ubuntu18.04安装PCL1.14
linux·ubuntu
A黄俊辉A7 小时前
axios+ts封装
开发语言·前端·javascript
小李小李不讲道理7 小时前
「Ant Design 组件库探索」四:Input组件
前端·javascript·react.js
郑板桥308 小时前
tua-body-scroll-lock踩坑记录
前端·javascript
解道Jdon8 小时前
SpringBoot4与Spring7发布:云原生深度进化
javascript·reactjs