duyuhe / blog Goto Github PK
View Code? Open in Web Editor NEW写点文字,记录自己的一点一滴
写点文字,记录自己的一点一滴
原文:What a CSS Code Review Might Look Like
作者: Geoff Graham
许多代码上线之前都会先进性审查,同行评审亦或者单元测试,这样可以让我们放心上线网站。
那,CSS的代码审查会是什么样子呢?
好处就是提前发现并修正错误,从而将我们与机器生成的代码区分开。
审查代码排版、风格;对响应式页面来说,确定其断点清晰准确。
确保代码整洁,风格一致,以便利于团队协作、可阅读性和扩展性。
虽然有相关组织和机构,制订和推广行业标准;但是不同浏览器对不同CSS属性仍有兼容性问题。
Hack
吗?代码风格是什么?怎么处理属性之间的兼容性和优先级?
!important
重要吗?为什么要避免使用?LESS / SASS
性能问题非常重要。
上面提到的步骤或工具,不是必须的,或许你有更优秀的想法,欢迎反馈给我。
我们不一定让CSS非常完美,但一定要努力做到最好。
富文本编辑器:WYSIWYG(所见即所得)。
利用<iframe>
嵌入一个空白页面或者是,直接定义一个可编辑元素
<div id=edit contenteditable=true designmode=on></div>
比如:加粗、斜体和链接之类的行为
//让元素可编辑是HTML5的属性,值可以不加双引号
<input type=button id=bold value=B>
<input type=button id=italic value=i>
<input type=button id=underline value=U>
<input type=button id=createlink value=a>
更多操作请看文章最后推荐的书籍。
使用document.execCommand()
操作编辑器
document.execCommand(“命令”,false,值(或Null));第二个参数设置false是为了兼容性
$(document).ready(function(){
function doExec(fx, extra) {
document.execCommand(fx, false, extra);
}
$(“#bold”).click( function() {
doExec(“bold”, ““);
});
$(“#italic”).click( function() {
doExec(“italic”, ““);
});
$(“#underline”).click( function() {
doExec(“underline”, ““);
});
$(“#createlink”).click( function() {
var xtra = prompt(“Enter a URL:”, “http://”);
doExec(“createlink”, xtra);
});
});
保存文章:因为富文本编辑器不是表单无法提交,所以需要使用一个form
来进行保存,提交。
<form action="_.php" >
<textarea name="textarea"></textarea>
<input type="submit" value="完成提交" />
</form>
form.onsubmit =function(){
/* 用户是看不见textarea的,但是服务器可以看到 */
textarea.value = edit.innerHTML;
}
一步一步实现富文本编辑器制作
10行代码实现简单编辑器
《JavaScript高级程序设计》--第十四章第5节
《HTML5 Mobile Application Development in 24 Hours Sams Teach Yourself》--第十四章
原文作者:James Hibbard
原文链接:jQuery setTimeout() Function Examples
setTimeout
虽然是JS原生API,但是在jQuery中使用效果良好。
setTimeout
是window
下的方法,不过大家很少这样写:
var timer=window.setTimeout(func,[delay,param]);
而是直接写成— —var timer=setTimeout(func,[delay,param]);
。
timer
,是为了可以方便取消定时clearTimeout
func
,可以直接写函数代码function(){/* code */}或者函数名字funcNamefunc
,也可以是字符串“alert(code);”
— —不推荐delay
,定义时间,默认为0param
,参数下面有讲带参函数,常规写法:
setTimeout(function(){ greet(randomGreeting);}, 1000);
或者是将参数放在最后(注意这种写法<IE9不支持):
setTimeout(greet, 1000, randomGreeting);
定时器中this
指向window
:
var person = {
firstName: "Jim",
introduce: function(){
console.log("Hi, I'm " + this.firstName);
}
};
person.introduce();
// Outputs: Hi, I'm Jim
setTimeout(person.introduce, 50);
// Outputs: Hi, I'm undefined
你可以使用bind函数,来让this
指向对象。
setTimeout(person.introduce.bind(person), 50);
// Outputs: Hi, I'm Jim
或者使用jQuery中的$.proxy方法来设置
setTimeout($.proxy(person.introduce, person), 50);
// Outputs: Hi, I'm Jim
setTimeout
可以让函数延迟执行,效果等同jQuery中的delay()
$( "button" ).click(function() {
// 元素隐藏.8s之后再渐显
$( "div.first" ).slideUp( 300 ).delay( 800 ).fadeIn( 400 );
});
如果需要一直运行定时器,推荐setInterval
。
网站缓存:
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="-1">
在移动设备中禁止百度等搜索引擎对网站进行转码
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
禁止谷歌翻译EN
<meta name="google" value="notranslate">`
iOS 设备上禁止将数字识别为可点击的 tel link
<meta content="telephone=no" name="format-detection"/>
官方文档
一篇相关博客
MDN-META
viewport双城记(1)
viewport双城记(2)
跨终端Web开发实战
原文:3 Things (Almost) No One Knows About CSS
作者:Kevin Yank
你真的了解CSS吗?我要讲的三点技巧就是在观察参与者测试之后,发现的有趣的问题。
line-height
首先,先排除了double
,虽然有部分人会选择这个答案。
2em
有39%的人会选择它,另有21%的人会选择200%
;看来,你们还不会使用更时尚的百分比。
然而,正确答案是2
.
指定一个无单位的数字,这样font-size
后代的值将继承这一数字,而不是固定行高。
比如说,你想将网页中的行距设置成双倍,普通元素字体12px、h1元素字体24px,你如果将line-height
设置成2em
或200%
,页面所有的文本行高都是24px;但是是设置成无单位的2
的话,页面所有的文本行高都会相应的变大x倍,普通元素24px,h1元素48px行高。
首先,还是先排除最不可能的值background
。
有46%的人会选择z-index
,看来你们还是没有理解z-index
,它不做任何动作,只有在需要重叠的地方设置才有效(position),MDN上有一篇“理解 CSS z-index”需要好好阅读。
overflow
控制文档溢出处理,并不对控制重叠起作用;但有相当部分人不这么认为。
正确答案是margin
,将其设置成负值会引起元素相叠的效果,效果如下图:
Web爱好者,根据margin
重叠特性研究的多列布局的效果One True Layout(和后来的In Search of the Holy Grail)。
利用伪元素
和伪类
可以作出非常炫的页面效果。
伪类
是一个特定的状态,比如:hover(鼠标放上去才会触发)。
伪元素
是一个虚拟的HTML元素(:after),是文档的一部分,让你的页面风格更加优秀。
实例讲解:略!
或许你可以试着去做一下测试。
中文:理解CSS z-index
全文翻译-前端外刊
12 Little-Known CSS Facts (The Sequel) 译
12个很少被人知道的CSS事实
原文:10 Ecmascript-6 tricks you can perform right now
作者:Dale Schouten
推荐ES6编译器traceur compiler 和测试使用浏览器Firefox Aurora 。
1.变量的解耦赋值— —详细讲解
var [day, month, date, year] = Date().split(' ');
2.设置默认参数
f = function(x, y=1) {console.log(x,y)};
f(22);
// 22 1
3.省略参数arguments
f = function(x,y,z, ...a) { console.log(a) }
f(1,2,3,7,8,9,'a','b','c');
// 7 8 9 'a' 'b' 'c'
4.创建箭头函数
f = (x) => x*x
5.使用局部变量
f = function () {
let n=1;
for (let n=5,i=0; i<n; i++) {
console.log(i);
}
console.log(n);
};
f();
// 0 1 2 3 4 1
6.定义常量
f = function() {
const two = 2;
two = 5;
console.log(two);
}
f();
7.重写属性Proxy
var point = {x:1};
var w = {set: function(t,p,v) {
console.log(p,v);
}
};
p = Proxy(point, w);
p.x = 2;
// prints 'x 2', but doesn't change p.x
代理(Proxy)包装对象提供了丰富的功能,因此,许多类型的访问可能监控或修改。列表
8.创建和设置对象
s = new Set();
[2,3,5,4,5,2,2].map(x => s.add(x))
for (i of s) {console.log(i)}
// 2 3 4 5
Set()创建对象,Map() Map 文章 文章二设置键值对集合。
9.使用set设置一个键值对 key:value
m = new Map();
o = {passwd: "my name is my password"};
m.set(o, "content")
console.log(m.get(o))
// "content"
10.迭代Iterate和生成Generate
nums = function () {yield 1; yield 2; yield 3;}
for (i of nums()) {console.log(i)}
// 1 2 3
它仅遍历数组内的元素,外部的属性值会忽略:
a = [1,2,3,4]
a.sparse = false
for (t of a) {
console.log(t)
}
// 1 2 3 4
浏览器问题:略!
书籍:《ECMAScript 6入门》
Code:ES6新特性与ES5对比
标准:ES7的提案
一:鼠标光标只有一点;而触摸点有很多
二:鼠标光标可以悬停;而触摸点不可以
canvas.ontouchstart=function(e){}
canvas.addEventListener("touchstart",function(e){});
注意:将e
传入函数中。
事件 | 是否触发冒泡 | 描述 | 浏览器对该事件默认处理方式 |
---|---|---|---|
touchstart | yes | 触摸 | 未定义 |
touchmove | yes | 移动 | 未定义 |
touchend | yes | 离开触摸区域 | mouseover/mousedown/mouseup/click视具体情况 |
touchcancel | yes | 触摸动作被打断或者触摸点个数过大 | 未定义 |
注意:
e.stopPropagation(e)
阻止事件冒泡e.preventDefault(e)
阻止事件默认行为,如浏览器滚动页面的动作属性 | 属性值得数据类型 | 描述 |
---|---|---|
touches | TouchList | 重要:触发事件的触摸点集合 |
changedTouches | TouchList | 重要:与上次触摸点来说,发生改变的触摸点。 |
targetTouches | TouchList | 触发事件的元素上面有多少触摸点 |
allKey/ctrlKey/metaKeyshiftKey | boolean布尔 | 是否同时按着其中某一键 |
TouchList
对象拥有两个属性
1. length
canvar.ontouchstart =function(e){
var len= e.touches.length, // 触摸点的个数
first = e.touches[0]; // 可以使用下标获取第N个触摸点
}
2. identifier 返回每个Touch对象的独特标识符,比如想了解某一个触摸点是否参与多个事件
属性 | 属性值得数据类型 | 描述 |
---|---|---|
clientX | long | 触摸点相对于视窗的X坐标,不包含滚动条 |
clientY | long | 触摸点相对于视窗的Y坐标,不包含滚动条 |
identifier | long | 返回触摸点的标识符 |
pageX | long | 触摸点相对于视窗的X坐标,含滚动条 |
pageY | long | 触摸点相对于视窗的Y坐标,含滚动条 |
screenX | long | 触摸点相对于屏幕的X坐标 |
screenY | long | 触摸点相对于屏幕的Y坐标 |
target | EventTarget | 保留触摸事件触发时第一个触摸点所在的元素 |
实例:
canvar.ontouchstart =function(e){
var first = e.touches[0].pageX;
// 或者
var last = e.changedTouches[0].pageX;
... ...
}
只使用上述事件配合事件属性:
touchstart/touchmove ... ...
touches/changedTouches ... ...
又或者使用以下提供的事件:
1.更好的编译JS
2.小型的库将优于大型框架
大型框架不会消失:第四条
3.专注、优化某一库
4.优秀的大型框架还会流行
5.实现Babel功能的编译器(工具):
6.应用跨平台运行
ReactJS ... ...
7.ES7规范将制订
8.新大型框架会减产
这只是预测,以实际为准。
所谓断点:就是如移动前端
下《数据分析与分级》一文中提到的,对浏览器进行分级— —不过是只针对屏幕大小。
展开来说,就是:
1.当屏幕大小大于900px时
2.当屏幕大小大于600px时
3.当屏幕大小小于600px时
... ...
布局、功能或者内容等等发生变化,都可以算做断点。
使用CSS媒体查询
@media screen and (min-width:600px){
/* code */
}
使用JS触发
// 第一种
if(document.documentElement.clientWeight >= 600){
// code
}
// 第二种
if(window.matchMedia("(min-width:600px)").matches){
// code
}
LESS是动态样式语言,属于CSS预处理语言的一种。
推荐编译工具:koala(考拉)
.less
文件要在less.js
之前
/* a little text */
Less编译成css的时候,注释会保留
//a lillte text
Less编译成css的时候,注释会删除
@name :value
别忘了@
符号
@color :red;
body{color:@color;} => body{color:red;}
一个类可以直接引用另一个类,继承并使用其中的样式
.base{color:red}
.box{background:green; .base;} =>.box{background:green;color:red;}
进一步,可以带参数混合
.base(@color){color:@color;}
.box{background:green; .base(red);} =>.box{background:green;color:red;}
还可以指定默认样式
.base(@color:red){color:@color;}
.box{background:green; .base();} =>.box{background:green;color:red;}
另一种自定义 :.box{background:green; .base(gray);} =>.box{background:green;color:gray;}
即是css中的body header p {}
这类情况
body{
header{
p{color:red;}
}
}
需要注意的是,注意性能问题,切勿深层嵌套。
实现:hover效果
a{
color:red;
&:hover{color:pink;}
}
可以运用加减乘除等数学运算设置颜色、宽高等... ...
@color :#f00;
.base{color:@color -20;} =>转换成rgb(0-255),再转换成十六进制
1.@arguments 接收参数数组
.base(5px,solid,red){
border:@arguments;
}
2.~
避免编辑
让Less无视,原文输出
.base{color:~'这里面的内容会原文输出';}
3.!important 尽量不使用
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.