Skip to content

Commit

Permalink
feat: add css transition height and swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
mx committed Nov 17, 2023
1 parent d62c118 commit bef3f63
Show file tree
Hide file tree
Showing 16 changed files with 1,101 additions and 31 deletions.
14 changes: 12 additions & 2 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default {
{ text: '首页', link: '/' },
{ text: '随手一记', link: '/hand-notes/JavaScript/数字四舍五入' },
{ text: '知识沉淀', link: '/knowledge-deposition/前端工程化/Babel/Babel基础' },
{ text: '收藏夹', link: '/favorites/' },
{ text: '收藏夹', link: '/favorites/CSS/' },
],
// 左侧边栏配置
sidebar: {
Expand Down Expand Up @@ -58,6 +58,7 @@ export default {
{ text: 'CSS实现突出不同颜色的圆角', 'link': '/hand-notes/CSS/CSS实现突出不同颜色的圆角'},
{ text: 'CSS计数器-counter', 'link': '/hand-notes/CSS/CSS计数器-counter'},
{ text: 'CSS模拟数字loading', 'link': '/hand-notes/CSS/CSS模拟数字loading'},
{ text: '实现高度过渡', 'link': '/hand-notes/CSS/实现高度过渡'},
]
},
{
Expand Down Expand Up @@ -126,7 +127,7 @@ export default {
{ text: '依赖预构建', link: '/knowledge-deposition/前端工程化/Vite/依赖预构建'},
{ text: 'esbuild相关', link: '/knowledge-deposition/前端工程化/Vite/esbuild相关'},
{ text: 'Rollup的插件机制', link: '/knowledge-deposition/前端工程化/Vite/Rollup的插件机制'},
{ text: 'Vite插件', link: '/knowledge-deposition/前端工程化/Vite/Vite插件'},
{ text: 'Vite插件机制', link: '/knowledge-deposition/前端工程化/Vite/Vite插件机制'},
]
}
]
Expand Down Expand Up @@ -234,6 +235,7 @@ export default {
{ text: "路由分组", link: "/knowledge-deposition/GoLang/Gin/路由分组" },
{ text: "中间件", link: "/knowledge-deposition/GoLang/Gin/中间件" },
{ text: "支持跨域", link: "/knowledge-deposition/GoLang/Gin/支持跨域" },
{ text: "集成Swagger", link: "/knowledge-deposition/GoLang/Gin/集成Swagger" },
]
},
{
Expand All @@ -260,6 +262,14 @@ export default {
{ text: '日志分割', link: '/knowledge-deposition/GoLang/Logrus/日志分割' },
]
},
{
text: "爬虫",
collapsible: false,
collapsed: true,
items: [
{ text: 'HTTP请求方法', link: '/knowledge-deposition/GoLang/爬虫/HTTP请求方法' },
]
}
]
}
],
Expand Down
8 changes: 8 additions & 0 deletions docs/favorites/CSS/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ layout: doc

# CSS

## CSS相关

- [我写CSS的常用套路](https://juejin.cn/post/6844904033405108232#heading-1)
- [你会用到的 15 个前端小知识(含css)](https://mp.weixin.qq.com/s/UVifGMNXac3_KPLhR_cLNw)
- [30个你必须记住的CSS选择符](https://yanhaijing.com/css/2014/01/04/the-30-css-selectors-you-must-memorize/)
Expand All @@ -12,3 +14,9 @@ layout: doc
- [CSS :where:is 伪类函数是什么?](https://mp.weixin.qq.com/s/ZRBBTAjBLlposuMFJeNdcA)
- [You-need-to-know-css](https://lhammer.cn/You-need-to-know-css/#/)
- [10 Useful CSS Tricks ](https://stackdiary.com/useful-css-tricks/#hacking-wordpress-with-css)
- [CSS3剪贴路径(Clip-path)在线生成器工具](http://tools.jb51.net/code/css3path)
- [CSS一些效果合集带源码](https://www.lingdaima.com/css/#/)

## SCSS相关

- [Sass Tutorials](https://www.tutorialsteacher.com/sass/sass-functions)
11 changes: 0 additions & 11 deletions docs/favorites/index.md

This file was deleted.

210 changes: 210 additions & 0 deletions docs/hand-notes/CSS/实现高度过渡.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
---
layout: doc
---

# 实现高度过渡

在工作中有时候会遇到点击实现Collapse折叠效果,而折叠的元素高度可能不固定的,使用`height: 0``height: auto`的方法是行不通的,因为一个值从数字变为非数字是不能使用过渡效果的,要实现该效果可使用其他方法。

```html
<div id="root">
<h1>元素高度过渡效果(通过max-height实现)</h1>
<button class="btn">hover me</button>
<div class="parent">
<div class="inner">
三十六计的顺序是:
第1套胜战计,瞒天过海、 围魏救赵、借刀杀人、以逸待劳、趁火打劫、声东击西。

第2套敌战计,无中生有、暗渡陈仓、隔岸观火、笑里藏刀、李代桃僵、顺手牵羊。

第3套攻战计,打草惊蛇、借尸还魂、调虎离山、欲擒故纵、抛砖引玉、擒贼擒王。

第4套混战计,釜底抽薪、混水摸鱼、金蝉脱壳、关门捉贼、远交近攻、假道伐虢、

第5套并战计,偷梁换柱、指桑骂槐、假痴不颠、上楼抽梯、虚张声势、反客为主。

第6套败战计,美人计、空城计、反间计、苦肉计、连环计、走为上策。
</div>
</div>
<div>Footer</div>
</div>
```

## max-height

- 点击[max-height](https://mx52jing.github.io/Notes/css-related/transition-height/max-height.html)查看效果

可以对需要折叠的元素设置`max-height`来实现过渡,例如从`max-height: 0``max-height: 1000px`,最终设置的max-height只要比该元素实际高度大就可以。

:::warning
这种方法会有一个问题,就是元素展开和收起的效果不一致,因为有一段时间会作用于`max-height`减去`折叠元素实际高度`的折叠,设置的`max-height - 元素实际高度`越大,过渡差异越明显
:::

```css
.parent {
overflow: hidden;
max-height: 0;
transition: max-height 2s linear;
}
.btn:hover + .parent {
max-height: 1200px;
}
```

> 效果如下:
![](/image/css/transition-height-1.gif)

可以看到元素收起会比展开时慢了一点,效果不一致

## scale

- 点击[scale](https://mx52jing.github.io/Notes/css-related/transition-height/scale.html)查看效果

```css
.parent {
transform: scaleY(0);
transform-origin: top center;
transition: .5s linear;
}
.btn:hover + .parent {
transform: scaleY(1);
}
```

这种方法会使文字有拉伸效果,而且不做其他处理的话,未展开时,还占据了布局空间

> 效果如下:
![](/image/css/transition-height-2.gif)

## grid

- 点击[grid](https://mx52jing.github.io/Notes/css-related/transition-height/grid.html)查看效果

可以通过grid布局来时间,通过设置元素的`grid-template-rows`属性从`0fr``1fr`切换实现

```html
<div id="root">
<h1>元素高度过渡效果(通过grid实现)</h1>
<button class="btn">hover me</button>
<div class="parent">
<div class="child">
<div class="inner">
三十六计的顺序是:
第1套胜战计,瞒天过海、 围魏救赵、借刀杀人、以逸待劳、趁火打劫、声东击西。

第2套敌战计,无中生有、暗渡陈仓、隔岸观火、笑里藏刀、李代桃僵、顺手牵羊。

第3套攻战计,打草惊蛇、借尸还魂、调虎离山、欲擒故纵、抛砖引玉、擒贼擒王。

第4套混战计,釜底抽薪、混水摸鱼、金蝉脱壳、关门捉贼、远交近攻、假道伐虢、

第5套并战计,偷梁换柱、指桑骂槐、假痴不颠、上楼抽梯、虚张声势、反客为主。

第6套败战计,美人计、空城计、反间计、苦肉计、连环计、走为上策。
</div>
</div>
</div>
<div>Footer</div>
</div>
```

```css
.parent {
display: grid;
grid-template-rows: 0fr;
transition: .5s linear;
}
.btn:hover + .parent {
grid-template-rows: 1fr;
}
.child {
overflow: hidden;
}
```

> 效果如下:
![](/image/css/transition-height-3.gif)

## 使用js+css来实现

- 点击[use-js](https://mx52jing.github.io/Notes/css-related/transition-height/use-js.html)查看效果

该方法的原理如下:
- 需要展开时先设置元素`height: auto`,这时元素就有实际高度。

- 然后去获取元素的实际高度
- 先将元素高度设置为0,然后在设置为上一步获取的实际高度,这样高度就是准确值直接的切换,可以有过渡效果

:::tip
- 该方法的兼容性最好

- 将元素高度设置为0后,一定要触发一次`重绘`,不然样式设置会被优化掉,不会有过渡效果
:::

```html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./index.css">
<title>通过js+css实现</title>
<style>
.parent {
height: 0;
overflow: hidden;
transition: .5s linear;
}
</style>
</head>
<body>
<div id="root">
<h1>元素高度过渡效果(通过js+css实现)</h1>
<button class="btn">hover me</button>
<div class="parent">
<div class="inner">
三十六计的顺序是:
第1套胜战计,瞒天过海、 围魏救赵、借刀杀人、以逸待劳、趁火打劫、声东击西。

第2套敌战计,无中生有、暗渡陈仓、隔岸观火、笑里藏刀、李代桃僵、顺手牵羊。

第3套攻战计,打草惊蛇、借尸还魂、调虎离山、欲擒故纵、抛砖引玉、擒贼擒王。

第4套混战计,釜底抽薪、混水摸鱼、金蝉脱壳、关门捉贼、远交近攻、假道伐虢、

第5套并战计,偷梁换柱、指桑骂槐、假痴不颠、上楼抽梯、虚张声势、反客为主。

第6套败战计,美人计、空城计、反间计、苦肉计、连环计、走为上策。
</div>
</div>
<div>Footer</div>
</div>
<script>
const btn = document.querySelector('.btn')
const parentEl = document.querySelector('.parent')
btn.addEventListener('mouseenter', () => {
// 先设置为auto 然后去获取实际高度
parentEl.style.cssText += `height: auto`;
const { height } = parentEl.getBoundingClientRect();
// 先将高度设置为数字0 为了让过渡生效
parentEl.style.cssText += `height: 0`;
// 这行一定要加 需要让页面进行重绘
parentEl.offsetHeight;
parentEl.style.cssText += `height: ${height}px`;
});
btn.addEventListener('mouseleave', () => {
parentEl.style.cssText += `height: 0`;
})
</script>
</body>
</html>
```

> 效果如下:
![](/image/css/transition-height-4.gif)
2 changes: 1 addition & 1 deletion docs/hand-notes/Sass/通过mixin简化响应式代码.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ $layouts: (

### 效果预览

- [mixin-optimize-style](https://github.com/mx52jing/Notes/sass-related/mixin-optimize-style/index.html)
- [mixin-optimize-style](https://mx52jing.github.io/Notes/sass-related/mixin-optimize-style/index.html)
Loading

0 comments on commit bef3f63

Please sign in to comment.