Toffee's Blog

  • 首页

  • 归档

  • 搜索

收集常用的css代码

发表于 2019-07-11

清除默认样式

reset


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
html,body{height:100%;}
html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,caption,th,td,form,fieldset,legend,input,button,textarea,menu{margin:0;padding:0;}
header,footer,section,article,aside,nav,hgroup,address,figure,figcaption,menu,details{display:block;}
table{border-collapse:collapse;border-spacing:0;}
caption,th{text-align:left;font-weight:normal;}
html,body,fieldset,img,iframe,abbr{border:0;}
i,cite,em,var,address,dfn{font-style:normal;}
[hidefocus],summary{outline:0;}
li{list-style:none;}
h1,h2,h3,h4,h5,h6,small{font-size:100%;}
sup,sub{font-size:83%;}
pre,code,kbd,samp{font-family:inherit;}
q:before,q:after{content:none;}
textarea{overflow:auto;resize:none;}
label,summary{cursor:default;}
a,button{cursor:pointer;}
h1,h2,h3,h4,h5,h6,em,strong,b{font-weight:bold;}
del,ins,u,s,a,a:hover{text-decoration:none;}
body,textarea,input,button,select,keygen,legend{font:12px/1.14 Microsoft YaHei,arial,\5b8b\4f53;color:#333;outline:0;}
body{background:#fff;}
a,a:hover{color:#333;}
*{box-sizing: border-box;}

去除input默认填充的背景颜色


1
2
3
input:-webkit-autofill {
-webkit-box-shadow: 0 0 0px 1000px white inset;
}

清除input[type=number]的默认样式


1
2
3
4
5
6
7
8
input[type=number] {
-moz-appearance:textfield;
}
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}

清除移动端 a 标签等点击区域变色


1
2
3
*{
-webkit-tap-highlight-color: rgba(255, 255, 255, 0);
}

清除移动端 input 样式


1
2
3
4
5
6
7
8
input{
border: none;
-moz-appearance:none;
-webkit-appearance : none ; /*解决ios上按钮的圆角问题*/
border-radius: 0; /*解决ios上输入框圆角问题*/
outline:medium; /*去掉鼠标点击的默认黄色边框*/
background-color: transparent;
}

避免ios滑动滚动条卡顿


1
2
3
*{
-webkit-overflow-scrolling : touch
}

滚动条样式


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
.scroll-container {
height: 250px;
border: 1px solid #ddd;
padding: 15px;
overflow: auto;
.row {
margin: 0;
line-height: 1.5;
}

&::-webkit-scrollbar {
width: 8px;
background: white;
}
&::-webkit-scrollbar-corner, /* 滚动条角落 */
&::-webkit-scrollbar-thumb,
&::-webkit-scrollbar-track {
border-radius: 4px;
}
&::-webkit-scrollbar-corner,
&::-webkit-scrollbar-track {
/* 滚动条轨道 */
background-color: rgba(180, 160, 120, 0.1);
box-shadow: inset 0 0 1px rgba(180, 160, 120, 0.5);
}
&::-webkit-scrollbar-thumb {
/* 滚动条手柄 */
background-color: #00adb5;
}
}

常用 midea媒体查询


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
34
35
36
37
/* 横屏 */
@media screen and (orientation:landscape){

}
/* 竖屏 */
@media screen and (orientation:portrait){

}
/* 窗口宽度<960,设计宽度=768 */
@media screen and (max-width:959px){

}
/* 窗口宽度<768,设计宽度=640 */
@media screen and (max-width:767px){

}
/* 窗口宽度<640,设计宽度=480 */
@media screen and (max-width:639px){

}
/* 窗口宽度<480,设计宽度=320 */
@media screen and (max-width:479px){

}
/* 设备像素比为2 */
/* 常用于1px边框,还应规定 3dppx 的情况 */
@media (min-resolution: 2dppx) {

}
/* windows UI 贴靠 */
@media screen and (-ms-view-state:snapped){

}
/* 打印 */
@media print{

}

长文本折行


1
2
3
4
.long-text{
white-space: pre-line;
word-wrap: break-word;
}

文字超出显示省略号

单行文字

1
2
3
4
5
6
7
/*注意宽度是必须的*/
.article-container {
width: 500px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}

多行文字


1
2
3
4
5
6
7
8
.article-container {
display: -webkit-box;
word-break: break-all;
-webkit-box-orient: vertical;
-webkit-line-clamp: 4; //需要显示的行数
overflow: hidden;
text-overflow: ellipsis;
}

消除图片下方间隙


通过设置给 img 元素设置 vertical-align: top 消除行内元素的间隙(仅对 img 元素有用

1
vertical-align: top;

图文居中


1
2
3
4
<div class="container">
<img src="../../public/images/bg1.jpg">
<span>安能摧眉折腰事权贵,使我不得开心颜</span>
</div>
1
2
3
4
5
6
.container{
padding:15px 0;
img{
vertical-align: middle;
}
}

使用webpack4配置一个完整的react前端工程

发表于 2019-06-13

一、基础配置

1、init

1
2
3
4
mkdir webpack-project
cd webpack-project
mkdir src
yarn init

2、安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
yarn add webpack webpack-cli webpack-dev-server
touch webpack.config.js
// webpack.config.js
module.exports = {
mode: "development",
entry: ["./src/index.js"],
output: {
path: path.join(__dirname,'dist'),
filename: "bundle.js"
},
module: {},
plugins: {},
devServer: {}
}

3、安装react

1
2
3
4
5
6
7
8
9
yarn add react react-dom react-router-dom
// 建立如下的文件目录,并编写安装react和react-router并编写react代码如下
|-src
│ index.js 主文件
├───pages
│ Count.jsx -- 实现了一个计数器的功能,点击按钮,会让数字增加,按钮会实时显示在页面上
│ Home.jsx -- 一个简单的文字展示
└───router
index.js -- 路由配置文件,两个页面分别对应两个路由 count和 home

4、babel编译es6,jsx等

1
2
3
4
5
6
7
8
9
10
11
12
13
// @babel/core-babel核心模块    @babel/preset-env-编译ES6等 @babel/preset-react-转换JSX
yarn add babel-loader @babel/core @babel/preset-env @babel/plugin-transform-runtime @babel/preset-react
// @babel/plugin-transform-runtime: 避免 polyfill 污染全局变量,减小打包体积
// @babel/polyfill: ES6 内置方法和函数转化垫片
yarn add @babel/polyfill @babel/runtime
//module配置
{
test: /\.jsx?$/,
exclude: '/node_modules/',
use: [{
loader: 'babel-loader'
}]
}

根目录新建.babelrc文件

1
2
3
4
{
"presets": ["@babel/preset-env","@babel/preset-react"],
"plugins": ["@babel/plugin-transform-runtime"]
}

阅读全文 »

Vue通信

发表于 2019-06-12

props和$emit

props父传子,$emit子传父

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
	Vue.component('child',{
data(){
return {
mymessage:this.message
}
},
template:`
<div>
<input type="text" v-model="mymessage"
@input="passData(mymessage)"> </div>
`,
props:['message'],//得到父组件传递过来的数据
methods:{
passData(val){
//触发父组件中的事件
this.$emit('getChildData',val)
}
}
})
//┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┅┄*
Vue.component('parent',{
template:`
<div>
<p>this is parent compoent!</p>
<child :message="message"
v-on:getChildData="getChildData"></child>
</div>
`,
data(){
return {
message:'张不怂'
}
},
methods:{
//执行子组件触发的事件
getChildData(val){
console.log(val)
}
}
})
//┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┅┄*

// 挂载
var app=new Vue({
el:'#app',
template:`
<div>
<parent></parent>
</div>
`
})

中央事件总线new Bus

新建一个Vue事件bus对象,然后通过bus.$emit触发事件,bus.$on监听触发的事件

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Vue.component('brother1',{
data(){
return {
mymessage:'hello brother1'
}
},
template:`
<div>
<p>this is brother1 compoent!</p>
<input type="text" v-model="mymessage"
@input="passData(mymessage)">

</div>
`,
methods:{
passData(val){
//触发全局事件globalEvent
bus.$emit('globalEvent',val)

}
}
})
//┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┅┄*

Vue.component('brother2',{
template:`
<div>
<p>this is brother2 compoent!</p>
<p>brother1传递过来的数据:{{brothermessage}}</p>
</div>
`,
data(){
return {
mymessage:'hello brother2',

brothermessage:''
}
},
mounted(){
//绑定全局事件globalEvent
bus.$on('globalEvent',(val)=>{
this.brothermessage=val;
})
}
})
//┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┄┅┄┅┄┅┄┅┄*

//中央事件总线
var bus=new Vue();

var app=new Vue({
el:'#app',
template:`
<div>
<brother1></brother1>
<brother2></brother2>
</div>
`
})

阅读全文 »

Vue数据更新问题

发表于 2019-06-11

Vue 的数组更新问题

以下参考 Vue 文档
由于 JavaScript 的限制,Vue 不能检测以下数组的变动:
1.当你利用索引直接设置一个数组项时,例如:vm.items[indexOfItem] = newValue
2.当你修改数组的长度时,例如:vm.items.length = newLength

题外话 实际上,我们在 Vue 的数组书使用 splice、push等方法,Vue 都已经做了一层封装,所以它们才能出发视图更新,如果有想更加深入了解,可以阅读源码。

Vue 强制刷新——$forceUpdate()

如果你发现你自己需要在 Vue 中做一次强制更新,99.9% 的情况,是你在某个地方做错了事。

类似的代码如下:

1
2
3
4
5
6
// 在控制变量改变的时候进行 强制渲染更新

let childrenRefs = this.$refs.elTabs.$children
this.$nextTick(() => {
childrenRefs.forEach(child => child.$forceUpdate())
})

深拷贝数据

先用一个数据深拷贝数据,这里使用了 slice 方法,然后置空,最后在 $nextTick 中赋值深拷贝出来的数组值。

1
2
3
4
5
var newArray = this.questionData.slice(0)
this.questionData = []
this.$nextTick(function () {
this.questionData = newArray;
})

gulp配置react开发环境

发表于 2019-05-25

gulp与webpack

gulp是对文件的处理,是Task Runner,节省了手动编译,手动打包这一步,但是单纯使用gulp没法去解决js中引入module的问题,可能会遇到项目中明明引入了babel语法转换,为什么浏览器会提示require is not defined报错,这是因为babel只解析语法,并不会去管js的模块引入。
而webpack解决了require的问题,根据不同的文件转换需求引入不同的loader,最后打包出符合相应规则的静态资源文件。

Browserify-让浏览器加载Node模块

官方文档开头介绍:require('modules') in the browser

目前NPM的包大多都是通过CMD的方式打包的,除了特定的可以使用CMD模块加载器加载的模块,大部分nodejs模块无法直接使用到浏览器环境中。
Browserify是一个供浏览器环境使用的模块打包工具,像在node环境一样,也是通过require(‘modules’)来组织模块之间的引用和依赖,既可以引用npm中的模块,也可以引用自己写的模块,然后打包成js文件,再在页面中通过<script>标签加载。

Browserify.transform

指定转换模块,在打包过程中自动执行,在这里需要结合babel进行语法转换

详细配置

react语法解析主要用到babel的@babel/preset-react插件,此外还会使用到browserSync进行自动检测刷新,less预处理

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const gulp = require('gulp') //引入gulp
const less = require('gulp-less') //引入gulp-less处理less语法
const LessAutoprefix = require('less-plugin-autoprefix') //css兼容前缀
const autoprefix = new LessAutoprefix({
browsers: ['last 2 versions']
})
const del = require('del') //删除文件模块,在每次gulp任务前执行
const babel = require('gulp-babel') //babel
const browserSync = require('browser-sync').create() //自动刷新
const reload = browserSync.reload //reload示例,用于浏览器刷新
const browserify = require('browserify') //模块处理
const source = require('vinyl-source-stream') //将browerify流转换成gulp文件流
const babelify = require('babelify') //用于browserify的babel编译器

//主任务
function main(cb) {
(async () => {
await del.sync('dist')
//less
gulp.src('./src/index.less').pipe(less({
plugins: [autoprefix]
})).pipe(gulp.dest('./dist'))
//js
browserify({
entries:['./src/index.js']
})
.transform(babelify,{
presets: ['@babel/env','@babel/preset-react'],
plugins: ["@babel/plugin-transform-runtime"]
})
.bundle() //打包
.pipe(source('index.js')).pipe(gulp.dest('./dist'))
gulp.src('./src/index.html').pipe(gulp.dest('./dist'))
cb()
})()

}

//开发环境
function devServer(cb){
browserSync.init({
server: {
baseDir: './dist'
}
})
gulp.watch("./src/**", gulp.series(lessChange,jsChange,htmlChange))
cb()
}

//检测html变化
function htmlChange(){
return gulp.src('./src/index.html').pipe(gulp.dest('./dist')).pipe(reload({stream:true}))
}

//检测样式文件
function lessChange() {
return gulp.src('./src/index.less').pipe(less({
plugins: [autoprefix]
})).pipe(gulp.dest('./dist')).pipe(reload({stream:true}))
}

//检测js文件
function jsChange(cb){
browserify({
entries:['./src/index.js']
})
.transform(babelify,{
presets: ['@babel/env','@babel/preset-react'],
plugins: ["@babel/plugin-transform-runtime"]
})
.bundle()
.pipe(source('index.js')).pipe(gulp.dest('./dist')).pipe(reload({stream:true}))
cb()
}

//default默认任务,用于package.json执行脚本gulp任务
gulp.task('default', gulp.series(main,devServer))

1…456…9
Toffee

Toffee

44 日志
GitHub E-Mail 我的网站 StackOverflow
0%
© 2018 – 2021 Toffee