转载自:CSDN-门时

记录一下前端开发过程中遇到的一些小问题以及解决方法。

HTML 篇

一、加快 audio 标签音乐加载

1
2
3
4
5
6
7
<audio
id="audio"
autoPlay
preload="meta"
src={BG_MUSIC}
loop
></audio>

原理:给 preload 设置确定的属性值,例如音频设为 “meta”,这样会比设为 “auto” 或空值加载更快。

CSS 篇

一、图片剪裁防止图片变形

1
2
3
4
5
6
.icon-container img {
width: 150px;
height: 150px;
object-fit: cover;
border-radius: 50%;
}

二、消除 input 框选中时外边框

1
2
3
4
5
6
7
8
9
input {
border: 0;
padding: 10px;
width: 100%;
height: 100%;
box-sizing: border-box;
outline: none;
font-size: 18px;
}

三、带有透明度的渐变

1
background: -webkit-linear-gradient(left, rgba(255, 255, 255, 0.6), rgba(255, 255, 255, 1));

四、a 标签的四个伪类定义顺序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
a:link {
color: white;
}
/* 未被访问的链接 */

a:visited {
color: #00FF00;
}
/* 已被访问的链接 */

a:hover {
color: #FF00FF;
}
/* 鼠标指针移动到链接上 */

a:active {
color: #0000FF;
}
/* 正在被点击的链接 */

五、流光字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
span {
font-family: "微软雅黑";
font-size: 22px;
line-height: 50px;
background-image: -webkit-linear-gradient(left, #147B96, #E6D205 25%, #147B96 50%, #E6D205 75%, #147B96);
-webkit-text-fill-color: transparent;
-webkit-background-clip: text;
-webkit-background-size: 200% 100%;
-webkit-animation: masked-animation 4s infinite linear;
}

@-webkit-keyframes masked-animation {
0% {
background-position: 0 0;
}
100% {
background-position: -100% 0;
}
}

六、使用伪元素制作带三角的气泡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
.reat {
margin: 100rpx;
width: 400rpx;
height: 400rpx;
border: 1rpx solid #AAA;
position: relative;
}

.reat::before {
content: '';
position: absolute;
top: -40rpx;
left: 190rpx;
width: 0;
height: 0;
font-size: 0;
border-width: 20rpx;
border-style: solid dashed dashed;
border-color: transparent transparent #AAA;
}

.reat::after {
content: '';
position: absolute;
top: -35rpx;
left: 190rpx;
width: 0;
height: 0;
font-size: 0;
border-width: 20rpx;
border-style: solid dashed dashed;
border-color: transparent transparent white;
}

原理:将 ::before 和 ::after 都使用 border 构造成三角形,设置 ::before 伪元素的边框颜色与主体区域边框颜色一致,设置 ::after 伪元素的边框颜色与页面底色一致,然后使用 ::after 伪元素遮盖部分 ::before 伪元素。

七、一个元素连续执行多个动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.demo {
-webkit-animation-name: bounceInLeft1, bounce1;
-webkit-animation-duration: 2s, 1.5s;
-webkit-animation-timing-function: ease, linear;
-webkit-animation-delay: 5.2s, 7.2s;
-webkit-animation-iteration-count: 1, infinite;
-webkit-animation-fill-mode: forwards, none;

animation-name: bounceInLeft1, bounce1;
animation-duration: 2s, 1.5s;
animation-timing-function: ease, linear;
animation-delay: 5.2s, 7.2s;
animation-iteration-count: 1, infinite;
animation-fill-mode: forwards, none;
}

原理:同时设置多个动画,利用延时实现动画效果。

八、段落首行缩进

1
2
3
.demo {
text-indent: 2em; /* em 是相对单位,2em 即现在一个字大小的两倍 */
}

九、滚动条强制显示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* 隐藏默认的滚动条 */
.ant-table-body::-webkit-scrollbar {
-webkit-appearance: none;
}

/* 设置垂直滚动条宽度 */
.ant-table-body::-webkit-scrollbar:vertical {
width: 11px;
}

/* 设置水平滚动条高度 */
.ant-table-body::-webkit-scrollbar:horizontal {
height: 11px;
}

/* 自定义滚动条样式 */
.ant-table-body::-webkit-scrollbar-thumb {
border-radius: 8px;
border: 2px solid white;
background-color: rgba(0, 0, 0, 0.5);
}

JS 篇

一、捕获键盘输入事件

1
2
3
4
5
6
7
8
9
10
11
12
document.onkeydown = function(event) {
var e = event || window.event || arguments.callee.caller.arguments[0];
if(e && e.keyCode == 27) { // 按 Esc
// 要做的事情
}
if(e && e.keyCode == 113) { // 按 F2
// 要做的事情
}
if(e && e.keyCode == 13) { // enter 键
// 要做的事情
}
};

二、DIV 自动滚动到底部

1
2
3
4
5
6
.chat-container {
padding: 10px;
height: 80%;
overflow-y: scroll;
box-sizing: border-box;
}
1
$(".chat-container").scrollTop($(".chat-container")[0].scrollHeight);

三、获取元素到浏览器顶部的距离

1
2
3
4
5
6
7
8
9
// 原生 JS
var mTop = document.getElementsByClassName('mdiv')[0].offsetTop;
var sTop = document.body.scrollTop;
var result = mTop - sTop;

// jQuery
mTop = $('.mdiv')[0].offsetTop;
sTop = $(window).scrollTop();
result = mTop - sTop;

四、监听 H5 运行在前台还是后台

1
2
3
4
5
6
7
8
9
10
11
const audio = document.getElementById('audio');

document.addEventListener('visibilitychange', function (e) {
if (audio) {
if (e.currentTarget.hidden) {
audio.pause();
} else if (self.state.isPlayMusic) {
audio.play();
}
}
}, true);

五、解决 iOS12 微信上键盘收起后页面不滑动的问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function isIos12() {
const ua = navigator.userAgent.toLowerCase();
const osReg = /iphone|ipad/;
const versionReg = /os (\d+)_/;
if (!ua.match(osReg)) return false;
return Number(ua.match(versionReg)[1]) === 12;
}

function scrollPage() {
if (isIos12()) {
const pageTop = $(window).scrollTop();
window.scrollTo(0, pageTop + 1);
}
}

六、在控制台使用 document.designMode 帮助调试

在控制台执行 document.designMode = 'on',之后就可以自由编辑页面里的元素了。