的选择变化可由
onfocus
属性控制
的变化可由
onchange
属性控制
selectedIndex
对应当前选中option
的index
:select.options[selectedIndex]
指向当前选项
select.value
存储的是当前选中option
的value
// 之间插入的内容是供不支持 video 元素的浏览器显示的
<video src="movie.ogg" width="320" height="240" controls="controls" id="video1">
Your browser does not support the video tag.
</video>
//video 元素允许多个 source 元素。source 元素可以链接不同的视频文件。浏览器将使用第一个可识别的格式
<video width="320" height="240" controls="controls" id="video1">
<source src="/i/movie.ogg" type="video/ogg">
<source src="/i/movie.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
//DOM控制视频的播放暂停
var myVideo=document.getElementById("video1");
function playPause()
{
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
"div1" ondrop="drop(event)" ondragover="allowDrop(event)">
"drag1" src="img_logo.gif" draggable="true" ondragstart="drag(event)" width="336" height="69" />
function allowDrop(ev){
ev.preventDefault();
}
function drag(ev){
ev.dataTransfer.setData("Text",ev.target.id);
}
function drop(ev){
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
//html
//js
var c=document.getElementById('');
var cxt=c.getContext('2d');
cxt.strokeStyle='#eee'; //cxt.fillStyle
cxt.beginPath();
cxt.moveTo(i,j);
cxt.lineTo(i,j);
cxt.closePath();
cxt.stroke(); //真正描绘出路径 cxt.fill();
cxt.strokeRect(0,0,width,height); //画矩形 cxt.fillRect()
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"
Longitude: " + position.coords.longitude;
}
使用 getCurrentPosition() 方法来获得用户的位置, 如果getCurrentPosition()运行成功,则向参数showPosition中规定的函数返回一个coordinates对象.getCurrentPosition() 方法的第二个参数用于处理错误
- web存储
localstorage sessionstorage
- 应用缓存
通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本,如需启用应用程序缓存,请在文档的 标签中包含 manifest 属性:
<html manifest="demo.appcache">
...
html>
每个指定了 manifest 的页面在用户对其访问时都会被缓存。如果未指定 manifest 属性,则页面不会被缓存(除非在 manifest 文件中直接指定了该页面)。manifest 文件的建议的文件扩展名是:”.appcache”。
请注意,manifest 文件需要配置正确的 MIME-type,即 “text/cache-manifest”。必须在 web 服务器上进行配置。
manifest 文件是简单的文本文件,它告知浏览器被缓存的内容(以及不缓存的内容)。manifest 文件可分为三个部分:
1. CACHE MANIFEST - 在此标题下列出的文件将在首次下载后进行缓存
2. NETWORK - 在此标题下列出的文件需要与服务器的连接,且不会被缓存
3. FALLBACK - 在此标题下列出的文件规定当页面无法访问时的回退页面(比如 404 页面)
//完整的 Manifest 文件
CACHE MANIFEST
# 注释:2012-02-21 v1.0.0
/theme.css
/logo.gif
/main.js
NETWORK:
login.asp
FALLBACK:
/html5/ /404.html
一旦应用被缓存,它就会保持缓存直到发生下列情况:
1. 用户清空浏览器缓存
2. manifest 文件被修改
3. 由程序来更新应用缓存
如果您编辑了一幅图片,或者修改了一个 JavaScript 函数,这些改变都不会被重新缓存。更新注释行中的日期和版本号是一种使浏览器重新缓存文件的办法。
- Web Workers
当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成。web worker 是运行在后台的 JavaScript,独立于其他脚本,不会影响页面的性能。您可以继续做任何愿意做的事情:点击、选取内容等等,而此时 web worker 在后台运行。
var w;
function startWorker()
{
if(typeof(Worker)!=="undefined")
{
if(typeof(w)=="undefined")
{
w=new Worker("/example/html5/demo_workers.js");
}
//向 web worker 添加一个 "onmessage" 事件监听器,当 web worker 传递消息时,会执行事件监听器中的代码
w.onmessage = function (event) {
document.getElementById("result").innerHTML=event.data;
};
}
else
{
document.getElementById("result").innerHTML="Sorry, your browser does not support Web Workers...";
}
}
function stopWorker()
{
w.terminate();
}
现在,让我们在一个外部 JavaScript 中创建我们的 web worker。在这里,我们创建了计数脚本。该脚本存储于 “demo_workers.js” 文件中:
var i=0;
function timedCount()
{
i=i+1;
postMessage(i);
setTimeout("timedCount()",500);
}
timedCount();
以上代码中重要的部分是 postMessage() 方法 - 它用于向 HTML 页面传回一段消息。当我们创建 web worker 对象后,它会继续监听消息(即使在外部脚本完成之后)直到其被终止为止。如需终止 web worker,并释放浏览器/计算机资源,请使用 terminate() 方法
- 服务器发送事件
- 新增表单元素
datalist:规定输入域的选项列表
keygen:密钥对生成器
output:定义不同类型的输出
box-sizing:border-box
可变成IE盒模型 position:absolute;
transform:translate(-50%,-50%);
left:50%;
top:50%;
给出宽高三种垂直水平居中法:
position:absolute;
left:50%;
top:50%;
margin-left:-width/2;
margin-top:-height/2;
position:absolute;
left,right,top,bottom:0;
margin:auto;
display:flex;//父元素
margin:auto;//子元素
个人认为居中还是flex最好用啦!
border:10px solid transparent;
border-left:#f00;
width:0;
height:0;
容器:
display:flex/inline-flex
flex-direction:row/row-reverse/column/column-reverse
flex-wrap:nowrap/wrap/wrap-reverse
flex-flow:row nowrap
justify-content:flex-start/flex-end/center/space-between/space-around
align-items:flex-start/flex-end/center/baseline/stretch
align-content:flex-start/flex-end/center/space-between/space-around/stretch //多跟轴线对齐方式
项目:
order:<integer>
flex-grow:<number>
flex-shrink:<number>
flex-basis:auto|<length> //定义了在分配多余空间之前,项目占据的主轴空间
flex:0 1 auto
align-self:flex-start/flex-end/center/baseline/stretch //覆盖父元素的align-items属性
具体可参看阮一峰的教程,总结的很好!
如果要实现背景透明而文字不透明的效果不能用opacity,可使用背景色rgba来调整
选择器 文字透明可看见背景图 模拟 文字逐个显示,且最后光标闪动 动画播放过程中,会突然停止,默认行为是跳回到动画的开始状态, 如果想让动画保持突然终止时的状态,就要使用animation-play-state属性 也可同时设置多个属性的过渡 style.top/style.left 滤镜属性的模糊效果 伪类的效果可以通过添加一个实际的类来达到,而伪元素的效果则需要通过添加一个实际的元素才能达到,这也是为什么他们一个称为伪类,一个称为伪元素的原因。伪元素创建的抽象元素不存在于文档语言里(可以理解为html源码) 这里用伪类 :first-child 和伪元素 :first-letter 来进行比较。 如果我们不使用伪类,而希望达到上述效果,可以这样做: 即我们给第一个子元素添加一个类,然后定义这个类的样式。那么我们接着看看伪元素: 那么如果我们不使用伪元素,要达到上述效果,应该怎么做呢? 即我们给第一个字母添加一个 span,然后给 span 增加样式,也就是添加了一个元素来达到效果,这就是和伪类不同之处。 如果css3 animation动画出现卡顿怎么办?
p:nth-child(n): p的父元素的第n个子元素是p元素 (p:first-child, p:last-child,p:only-child)
p:first-child 选择属于父元素的第一个子元素且这个子元素是元素。
p:only-child p的父元素只有一个子元素,并且这个子元素是p元素
p:nth-last-child(2) 同上,从最后一个子元素开始计数
p:nth-of-type(n): 选择属于p的父元素的第n个 元素(p:nth-first-type, p:nth-last-type,p:only-of-type)
p:only-of-type p的父元素可以有多个子元素,但是只能有一个p元素
div+p: 选择紧接在 元素
div~p:选择前面有div元素的p元素(div和p是兄弟元素)
[title~=flower]: 选择 title 属性包含单词 “flower” 的所有元素
[lang|=en]: 选择 lang 属性值以 “en” 开头的所有元素
a[src^=”https”]: 选择其 src 属性值以 “https” 开头的每个 元素
a[src$=”.pdf”]: 选择其 src 属性以 “.pdf” 结尾的所有 元素
a[src*=”abc”]: 选择其 src 属性中包含 “abc” 子串的每个 元素
[target]: 选择带有 target 属性所有元素
:not(p): 选择非 元素的每个元素。
background-image:-webkit-linear-gradient(left,blue,red 25%,blue 50%,red 75%,blue 100%);
background-clip:text/border-box/padding-box/content-box;
//text:从前景内容的形状(比如文字)作为裁剪区域向外裁剪,如此即可实现使用背景作为填充色之类的遮罩效果
color:transparent;
background-size:length/percentage/cover/contain
percentage:父元素的百分比
cover:背景图像扩展至覆盖背景区域(可能看不见部分图像)
contain:扩展至最大尺寸(可看见全部区域)background-attachment:scroll/fixed/inherit
background-orign:padding-box/border-box/content-box
规定背景图片的定位区域background-position:top center/x% y%/x y
<style>
@font-face
{
font-family: myFirstFont;
src: url('Sansation_Bold.ttf'),
url('Sansation_Bold.eot'); /* IE9+ */
font-weight:bold;
}
div
{
font-family:myFirstFont;
}
style>
animation:moveName duration timing-function delay iteration-count animation-fill-mode direction;
time-function: linear/ease/cubic-bezier(p1x,p1y,p2x,p2y)
iteration-count: n/infinite
animation-fill-mode:动画结束以后,会立即从结束状态跳回到起始状态。如果想让动画保持在结束状态,需要使用animation-fill-mode属性:forwards,backwards表示回到第一帧的状态,none是默认值,回到动画还没开始的状态
direction: normal/alternate(反向轮播)@keyframes moveName
{
0%: ……
50%: ……
100%: ……
}
@keyframes typing { from { width: 0; } }
@keyframes blink-caret { 50% { border-color: transparent; } }
h1 {
font: bold 200% Consolas, Monaco, monospace;
border-right: .1em solid;
width: 16.5em; /* fallback */
width: 30ch; /* # of chars */
margin: 2em 1em;
white-space: nowrap;
overflow: hidden;
animation: typing 20s steps(30, end), /* 动画分30步,不是平滑过渡 */
blink-caret .5s step-end infinite alternate;
}
div {
animation: spin 1s linear infinite;
animation-play-state: paused;
}
div:hover {
animation-play-state: running;
}
box-shadow:x y opacity color;
transition: property duration timing-function delay;
transition:width 1s,height 2s; //当不知道确切高度时可用max-height,用auto会突变
transition:width 1s,height 2s 1s; //添加delay可让动画按顺序执行
transform:translate(10px,10px)
transform:rotate(10deg);
transform:scale(2,4);
transform:rotateX(10deg);//3D转换,绕x轴旋转
若想获取style.top/style.left
的值必须以js显示定义style.top/style.left
的值,或者以内联形式定义属性的值,否则获取不到。
通过全局方法 getComputedStyle(element[,伪类]).getPropertyValue(prop) 和IE下的element.currentStyle().getPropertyValue(prop)可以获取到所有的样式,包括继承的,所以就算是一个空标签用这个方法也能获取到上百个样式,getComputedStyle获取到的样式只能读 不能写,style.padding可以写,getPropertyValue同getAttribute-webkit-filter:blur(4px);
calc()能让你给元素的宽高做计算,你可以给一个div元素,使用百分比、em、px和rem单位值计算出其宽度或者高度,比如说“width:calc(50% + 2px)”,这样一来你就不用考虑元素DIV的宽度值到底是多少,而把这个烦人的任务交由浏览器去计算。
columns:column-width,column-count
column-gap: 列之间的间隔
column-rule: 2px dotted red //列之间分割线的样式伪类和伪元素
p>i:first-child {color: red}
<p>
<i>firsti>
<i>secondi>
p>
//伪类 :first-child 添加样式到第一个子元素
.first-child {color: red}
<p>
<i class="first-child">firsti>
<i>secondi>
p>
p::first-letter {color: red}
<p>I am stephen lee.</p>
//伪元素 ::first-letter 添加样式到第一个字母
.first-letter {color: red}
<p><span class='first-letter'>Ispan> am stephen lee.p>
//点击链接设置 #func 的样式
3D轮播
perspective:3000px; //定义3D元素距视图的距离
transform-style:preserve-3d;
transform:rotateY(60deg),translateZ(500px);
1. 改用 transform 的css3属性(translate scale),因为用css3属性会自动开启GPU加速,提高动画的流畅度
2. 如果是在页面刚开始加载的时候并没有加载完全就执行了动画,那就可以让动画延迟执行