个人文档
  • AI编程Cursor
  • GPT使用笔记
  • npm常用库合集
  • 同步用
  • 小Demo们
  • 工具网站教程集合
  • HTML、CSS 工具方法集合
    • HTML 全局属性
    • css常用功能
    • font-face 字体|子集相关
    • iframe父子页面传值
    • input输入优化
    • loading状态
    • nodejs使用谷歌邮箱发邮件
    • 为 Dom 自定义事件监听
    • 初始html的head标签配置
    • 拼音输入中文汉字的事件监听
    • 文字颜色效果
    • 文档片段范围 Range
    • 移动端开发-rem
    • 等宽字体推荐
    • 网站SEO优化注意点
    • 邮件html模板
  • JS 工具方法集合
    • Axios 简单使用
    • Axios 简单封装
    • Gitbook的安装和使用
    • Github 登录开发
    • HTML转为纯文本
    • JS 中强大的操作符
    • cookie 操作
    • js 动态加载js资源
    • js 常用功能语句
    • js取代trycatch的方法封装
    • js接口下载二进制
    • script 标签的异步属性
    • 判断当前是移动端还是pc端
    • 刷新token队列管理
    • 前端多线程 Web Worker
    • 加密-AES对称加密
    • 加密-node进行rsa加密解密
    • 地区省市区三级联动的地址数据 + 功能
    • 复制插件
    • 开发时环境变量
    • 得到随机图片
    • 数字格式整理集合
    • 数学计算插件
    • 时间格式整理
    • 获取ip地址
    • 获取url传参
    • 进制转换和位运算符
    • 页面隐藏|激活|关闭的监听
  • JS 知识点研究
    • Babel 历史和原理
    • Babel 配置和使用
    • Function 的 apply、call、bind
    • HTTP浏览器缓存粗解
    • Source map 文件还原为源码
    • TS常用技巧
    • js 的加载和模块化
    • js 的新数据类型 Symbol
    • js的代理对象 proxy 和 defineProperty
    • js的原型链 prototype
    • vite 打包体积优化
    • webpack 可视化打包文件大小插件
    • webpack 基础使用配置
    • webpack 版本5的报错
    • yeoman 开发脚手架的工具
    • 同步异步和微任务宏任务
    • 移动端调试---谷歌工具+eruda+vconsole
    • 转换-Blob URL
    • 转换-FileReader
    • 转换-Js文件类型和转换
    • 转换-前端开发的URL的编码和解码
    • 转换-字符串和Base 64的转换
  • Node 和 Npm 相关
    • Node 开发环境配置
    • express + jwt 校验
    • node 常用方法
    • node后台服务器-PM2
    • node基本使用
    • npm 中依赖的版本问题
    • npm 功能使用
    • npm指令说明和其他对比
    • nvm版本管理+自动切换node版本
  • React 学习
    • React Hook
    • React 项目基础开发
    • React.memo 和 React.PureComponent
    • React懒加载进阶
    • useContext Hook
    • useEffect Hook
    • useMemo 和 useCallback - Hook
    • useRef Hook
    • useState Hook
    • 同步修改变量功能封装 useVal for react
    • 轻便的传值组件
  • Rust 语言相关
    • Rust 基本
    • Rust 基础学习
    • Rust 调用 Object-C 的API
    • Tauri 基本使用
    • Tauri 是什么
  • VUE 学习
    • Vue3 使用
    • Vue3使用hook
    • Vue开发小技术点
    • vue路由切换时的动画效果
    • 花式引入组件和资源-打包时拆包减少js体积
  • Web3相关
    • Web3.0开发上-准备和概念理解
    • Web3.0开发下-功能代码示例
    • 以太坊区块链和Web3.0
    • 开发智能合约
  • python
    • pyenv版本管理工具
    • python初始化
    • python基本概念
    • venv虚拟环境
  • 个人其他
    • Steam Deck的基本设置和插件
  • 其他编程相关
    • Git教程和常用命令
    • Java开发-JDK和Maven的安装和卸载
    • Jenkins安装和基本使用
    • Linux系统指令
    • Mac 使用2K屏幕开启缩放
    • Mac 使用VS code打开项目
    • Mac 安装 Homebrew
    • Mac 的终端 shell 与 zsh
    • Mac 软件和插件
    • MacBook使用建议
    • Mac升降级到指定版本的系统
    • Mac安装Zsh
    • Mac安装软件各种提示
    • Mac系统脚本语言 AppleScript 的使用
    • Mac终端代理工具
    • Markdown(md)文档开发-Typora
    • Mysql 的安装和使用
    • Nginx 安装和基础使用
    • Nginx 稍微高深的配置
    • Slate - Api 的文档开发工具
    • Sublime配置
    • Ubuntu的 apt-get 使用
    • VScode配置
    • Windows 软件和插件
    • curl 工具使用
    • github 网站访问优化
    • host 文件
    • inquirer 终端中和用户交互
    • uTools的插件开发教程
    • vim 文本编辑功能
    • 使用 Github Pages 免费部署网站
    • 压缩指令 zip 和 unzip
    • 油猴的安装和开发(Tampermonkey)
    • 阿里云简略使用
  • 微信开发
    • 微信小程序开发
    • 微信开发必读
    • 微信开发提前购买域名
    • 微信手机打开的页面中授权登录
    • 微信扫码登录
    • 微信服务号登录+推送服务提醒
    • 自定义分享卡片-node.js实现
  • 数据结构与算法
    • KMP算法
    • Wildcard字符串分析算法
    • 二叉树
    • 字典树
    • 时间复杂度浅析
    • 算法神器——动态规划
Powered by GitBook
On this page
  • css常用功能
  • js 读写 css
  • 使用css变量功能
  • css\less\scss的样式穿透写法
  • 贝塞尔曲线生成
  • 设置滚动条样式
  • reset.css 去除浏览器默认样式

Was this helpful?

  1. HTML、CSS 工具方法集合

css常用功能

title: css常用功能 id: c14420b9a4256a01e07704d91c9b9129 tags: [] date: 2000/01/01 00:00:00 updated: 2023/03/04 19:29:12 isPublic: true --#|[分隔]|#--

css常用功能

js 读写 css

获取实时位置信息:getBoundingClientRect() 方法

官方文档

getBoundingClientRect 方法返回元素的大小及其相对于视口的位置,返回的是 DOMRect 对象,此方式获取的元素尺寸和位置信息更方便。

语法:Element.getBoundingClientRect()

let htmlPositionInfo = document.querySelector('.p').getBoundingClientRect()
console.log(JSON.stringify(htmlPositionInfo))

打印以下对象,能清楚的看到某个元素的尺寸和位置信息:

{
  "x": 8,
  "y": 92,
  "width": 1973,
  "height": 44,
  "top": 92,
  "right": 1981,
  "bottom": 136,
  "left": 8
}

获取Dom设置的值:CSSStyleDeclaration 对象

官方文档

CSSStyleDeclaration 对象是一个 CSS 声明块,CSS 属性键值对的集合,它暴露了样式信息和各种与样式相关的方法和属性。

CSSStyleDeclaration 对象中的 css 规则的键名是驼峰命名的写法。

以下三种方式,拿去到的都是 CSSStyleDeclaration 对象:

  • Element.style:只能读取到该 dom 节点的的行内样式声明的属性,可读可写,但写完后,也是写在了行内的样式中。

  • getComputedStyle(Element):获取该 dom 节点的所有 css 样式属性计算后生成的 CSSStyleDeclaration,但是一个只读的对象。

  • CSSStyleSheet API:不常用,比如,document.styleSheets[0].cssRules[0].style 会返回文档中第一个样式表中的第一条 CSS 规则

所以一般情况:

  • 需要写时,使用第一种方法(HTMLElement.style),只有这个才有写的功能。

  • 需要读时:使用上面第二种中方法(getComputedStyle(Element))获取,可以读取到该 Dom 的全部的 css 样式值。

使用css变量功能

css的变量功能,主流浏览器均已经支持。

直接使用

下面这段代码的功能,能够实现和scss变量类似的功能。

而且,使用js是可以读取和设置css变量的。


/* 页面默认引入的css文件中设置 */
:root {
  --boxWidth: 1200px;
}
div {
  --backgroundRed: red;
}

@media screen and (max-width: 1400px) {
  :root {
    --boxWidth: 1000px;
  }
}

/* 任意某个css文件中使用 */
div.box {
  width: var(--boxWidth, 20px); /* 第二个参数选填,为缺省值 */
  background: var(--backgroundRed);
}

使用 CSSStyleDeclaration 交互

css 中:

:root {
  --color: red;
}

js 中:

// 【读】
// 首先获取 html 这个 dom 的可读完整 css 属性的 CSSStyleDeclaration
let htmlComputedStyleRead = getComputedStyle(document.querySelector('html'))
// 获取变量值
let varColor = htmlComputedStyleRead.getPropertyValue('--color').trim() // 获取后会有空格(写css时冒号后的空格)
console.log(varColor) // red

// 【写】
// 首先获取 html 这个 dom 的可写完整 css 属性的 CSSStyleDeclaration
let htmlComputedStyleWrite = document.querySelector('html').style
htmlComputedStyleWrite.setProperty('--color', 'rgb(0, 255, 255)') // 设置
let newColor = htmlComputedStyleWrite.getPropertyValue('--color') // 由于本来就是设置在了行内,所以可以直接用它来获取即可
console.log(newColor) // rgb(0, 255, 255)

css\less\scss的样式穿透写法

使用vue或react时,大多都会使用 scope来限制样式的生效范围,保证不影响全局或子组件的样式,但有时偏偏就需要样式穿透,修改子组件的样式。

而使用 css\less\scss 时,样式穿透的写法是不同的:

  • 使用css:没有使用css预处理器,可以使用 >>>,/deep/,::v-deep 来样式穿透。

  • 使用less或者node-sass:使用/deep/,::v-deep都可以。

  • 使用dart-sass:使用::v-deep来样式穿透。

贝塞尔曲线生成

贝斯尔曲线是使用 js 或 css 制作动画时,很有用的一个工具,但它的使用还是有一定门槛的,看不懂公式和配置。

下面这个网站,可以所见即所得的,拖拽小球配置曲线,然后自动生成贝斯而曲线的配置,复制使用,很有用的工具:https://cubic-bezier.com/

以下是贝塞尔曲线的js实现,把上面网站的参数传入此函数,即可得到一个方法,一个传入进程值就能得到位置的方法

// 贝塞尔曲线实现
export function cubicBezier(p1x, p1y, p2x, p2y) {
  const ZERO_LIMIT = 1e-6;

  const ax = 3 * p1x - 3 * p2x + 1;
  const bx = 3 * p2x - 6 * p1x;
  const cx = 3 * p1x;

  const ay = 3 * p1y - 3 * p2y + 1;
  const by = 3 * p2y - 6 * p1y;
  const cy = 3 * p1y;

  function sampleCurverDerivativeX(t) {
    return (3 * ax * t + 2 * bx) * t + cx;
  }

  function sampleCurveX(t) {
    return ((ax * t + bx) * t + cx) * t;
  }

  function sampleCurveY(t) {
    return ((ay * t + by) * t + cy) * t;
  }

  function solveCurveX(x) {
    var t2 = x;
    var derivative;
    var x2;

    //https://trac.webkit.org/browser/trunk/Source/WebCore/platform/animation
    //First try a few iterations of Newton's method -- normally very fast
    // http://en.wikipedia.org/wiki/Newton's_method
    for (let i = 0; i < 8; i++) {
      // f(t) - x = 0
      x2 = sampleCurveX(t2) - x;
      if (Math.abs(x2) < ZERO_LIMIT) {
        return t2;
      }
      derivative = sampleCurverDerivativeX(t2);
      // == 0, failure
      if (Math.abs(derivative) < ZERO_LIMIT) {
        break;
      }
      t2 -= x2 / derivative;
    }
    //Fall back to the bisection method for reliability
    //bisection
    //http://en.wikipedia.org/wiki/Wifi/bisection_method
    var t1 = 1;
    var t0 = 0;
    t2 = x;
    while (t1 > t0) {
      x2 = sampleCurveX(t2) - x;
      if (Math.abs(x2) < ZERO_LIMIT) {
        return t2;
      }
      if (x2 > 0) {
        t1 = t2;
      } else {
        t0 = t2;
      }
      t2 = (t1 + t0) / 2;
    }

    // Failure
    return t2;
  }

  function solve(x) {
    return sampleCurveY(solveCurveX(x));
  }

  return solve;
}

使用实例

// 几种常见动画的方程
export const ease = cubicBezier(0.25, 0.1, 0.25, 1);
export const easeIn = cubicBezier(0.42, 0, 1, 1);
export const easeOut = cubicBezier(0, 0, 0.58, 1);
export const easeInOut = cubicBezier(0.42, 0, 0.58, 1);

设置滚动条样式

/* 滚动条整体部分,其中的属性有width,height,background,border等(就和一个块级元素一样 */
div {
  &::-webkit-scrollbar {
    width: 4px;
    height: 4px;
    background: transparent;
    /* background: #202535; */
    /* box-shadow: inset 0 1px 3px 0 rgba(0,0,0,0.50); */
  }
  /*定义滚动条*/
  &::-webkit-scrollbar-thumb {
    height: 4px;
    border-radius: 4px;
    background: rgba(255,255,255,0.3);
    /* box-shadow: inset 0 1px 3px 0 rgba(6,9,16,0.20); */
  }
  /* 两头的按钮 */
  &::-webkit-scrollbar-button {
    display: none;
    background: transparent;
    background-color: transparent;
  }
}

reset.css 去除浏览器默认样式

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
abbr, address, cite, code,
del, dfn, em, img, ins, kbd, q, samp,
small, strong, sub, sup, var,
b, i,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, figcaption, figure,
footer, header, hgroup, menu, nav, section, summary,
time, mark, audio, video {
  margin:0;
  padding:0;
  border:0;
  outline:0;
  font-size:100%;
  vertical-align:baseline;
  background:transparent;
}

html, body, * {
  line-height: 1.2em;
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  outline: none;
}

article,aside,canvas,details,figcaption,figure,footer,header,hgroup,menu,nav,section,summary {
  display:block;
}

ul, li {
  list-style:none;
}

blockquote, q {
  quotes:none;
}

blockquote:before, blockquote:after,q:before, q:after {
  content:'';
  content:none;
}

a {
  margin:0;
  padding:0;
  border:0;
  font-size:100%;
  vertical-align:baseline;
  background:transparent;
  cursor: pointer;
  text-decoration: none;
  color: inherit;
}
a:hover {
  color: inherit;
}

ins {
  background-color:#ff9;
  color:#000;
  text-decoration:none;
}

mark {
  background-color:#ff9;
  color:#000;
  font-style:italic;
  font-weight:bold;
}

del {
  text-decoration: line-through;
}

abbr[title], dfn[title] {
  border-bottom:1px dotted #000;
  cursor:help;
}

table {
  border-collapse:collapse;
  border-spacing:0;
}

hr {
  display:block;
  height:1px;
  border:0;
  border-top:1px solid #cccccc;
  margin:0;
  padding:0;
}

input, select {
  vertical-align:middle;
}
button {
  outline: none;
  border: 0;
  background-color: none;
  background: none;
  cursor: pointer;
}
PreviousHTML 全局属性Nextfont-face 字体|子集相关

Last updated 3 months ago

Was this helpful?

Snipaste_2021-02-24_10-08-53.png