1)页面加载事件
基本语法
<body onload="函数名()">或window.onload = function(){javascript代码}
功能
用于在网页加载完毕后立刻执行的操作,即当 HTML 文档加载完毕后,立刻执行某个方法。
2)窗口重置事件
基本语法
<body onresize="函数名()">或window.onresize = function(){代码}
功能
3)鼠标滚轮事件
基本语法
火狐浏览器document.addEventListener("DOMMouseScroll",函数名,布尔值);说明:布尔值是可选值true - 事件句柄在捕获阶段执行false- 默认。事件句柄在冒泡阶段执行谷歌浏览器window.onwheel = 函数名;
功能
示例
/滚轮事件调用函数function mousewheel(e){e.preventDefault();//通知浏览器不执行默认的动作if(e.wheelDelta){//IE/Opera/Chromeif(e.wheelDelta < 0){//下index++;}else{//上index--;}}else if(e.detail){//Firefoxif(e.detail < 0){//上index--;}else{//下index++;}}setAnimate();}
4)触屏事件
【1】触屏开始
基本语法
document.addEventListener(“touchstart”,函数名,布尔值);
功能
【2】触屏结束
基本语法
document.addEventListener(“touchend”,函数名,布尔值);
功能
【3】触屏滑动
基本语法
document.addEventListener(“touchmove”,函数名,布尔值);
功能
当手指在屏幕上滑动的时候连续地执行某个函数。
说明:布尔值是可选值
true - 事件句柄在捕获阶段执行
false- 默认。事件句柄在冒泡阶段执行
示例
1、touchstartfunction fun1(){document.getElementById("div1").style.backgroundColor = "green";}document.getElementById("div1").style.backgroundColor = "green"; 当手指触摸屏幕时候id为div1的标签背景色为绿色2、touchendfunction fun2(){document.getElementById("div1").style.backgroundColor = "orange";}document.getElementById("div1").style.backgroundColor = "orange"; 当手指从屏幕上离开的时候id为div1的标签背景色为桔色3、touchmovevar num = 0;function fun3(){num++;document.getElementById("div1").innerHTML = num;document.getElementById("div1").style.backgroundColor = "red";}document.getElementById("div1").innerHTML = num; 当手指在屏幕上滑动的时候将数字num在id为div1的标签中输出document.getElementById("div1").style.backgroundColor = "red"; 当手指在屏幕上滑动的时候 id为div1的标签背景色为红色
