JS学习笔记9之event事件及其他事件

时间:2023-03-09 06:29:20
JS学习笔记9之event事件及其他事件

-->鼠标事件
-->event事件对象
-->默认事件
-->键盘事件(keyCode)
-->拖拽效果

一、鼠标事件

onclick ---------------鼠标点击事件
oncontextmenu------鼠标右键点击
onmouseover --------鼠标移上
onmouseout ---------鼠标移出
onmousedown -------鼠标按下
onmousemove -------鼠标移动
onmouseup ----------鼠标抬起

 <head>
<meta charset="UTF-8">
<title>鼠标事件</title>
<style>
*{margin:0;padding:0;list-style: none;}
#con{
width:300px;
height:300px;
background: #ccc;
border:1px solid #666;
margin:10px auto;
}
#con #box{
width:200px;
height:200px;
margin:50px auto;
background: pink;
}
</style>
</head>
<body>
<div id="con">
<div id="box"></div>
</div>
</body>
<script>
var con=document.getElementById('con');
var x=0,y=0,z=0,a=0,b=0,c=0;
//onclick ---------鼠标点击事件
document.onclick=function(){
x++;
console.log('鼠标点击_onclick'+x);
}
//oncontextmenu----鼠标右键点击
document.oncontextmenu=function(){
alert('鼠标右击事件');//先弹出弹框后显示菜单
}
//onmouseover -----鼠标移上(包括子元素)
con.onmouseover=function(){
y++;
console.log('鼠标移上_onmouseover'+y);
}
//onmouseout ------鼠标移出(包括子元素)
con.onmouseout=function(){
z++;
console.log('鼠标移出_onmouseout'+z);
}
//onmouseenter -----鼠标移上
con.onmouseenter=function(){
y++;
console.log('鼠标移上_onmouseenter'+y);
}
//onmouseleave------鼠标移出
con.onmouseleave=function(){
z++;
console.log('鼠标移出_onmouseleave'+z);
}
//onmousedown -----鼠标按下
document.onmousedown=function(){
a++;
console.log('鼠标按下_onmousedown'+a);
}
//onmouseup ------鼠标抬起
document.onmouseup=function(){
b++;
console.log('鼠标按下_onmouseup'+b);
}
//onmousemove -----鼠标移动
con.onmousemove=function(){
c++;
console.log(c);
}
</script>

二、event事件对象

event对象只在事件发生的过程中才有效
用途:需要获取和事件相关的信息时使用
如:
获取键盘按下或弹起的按键
获取鼠标的位置坐标
获取事件名称
获取事件生成的日期时间
等等......
event对象中包含了所有与事件相关的信息

所有浏览器都支持event对象,只是支持的方式不一样

  • FireFox、Chrome等浏览器要获取到event对象,需要从函数中传入,参数名随意
  • 而IE在浏览器中event作为window对象的一个属性存在,可以直接使用 event 或 window.event

例如:
document.onmousedown=function ( ev ){
var Event = ev || window.event ; //兼容各个浏览器
alert( Event.clientX ) ;// 弹出鼠标相对窗口的X轴坐标
console.log(Event);
};
关于使用event事件的兼容写法:

//IE9以上 谷歌 火狐支持  /  IE6、7、8不支持
document.onclick=function (ev){
var e=ev;
console.log('鼠标指针对于浏览器页面的水平坐标'+e.clientX); }
//IE 谷歌支持/ 火狐不支持
document.onclick=function (){
var e=window.event||ev;
console.log('鼠标指针对于浏览器页面的垂直坐标'+e.clientY);
}
/*兼容各个浏览器,event事件写法*/
document.onclick=function (ev){
var eve=window.event||ev;//event事件兼容写法写法
console.log(eve.clientY);
console.log(eve.preventDefault);
}

三、默认事件

阻止默认事件(阻止使用右键事件)

document.oncontextmenu = function(ev) {
  var Event=ev||window.event;
  if (Event.preventDefault) {
    //阻止默认动作(W3C)
    Event.preventDefault();
  } else{
    //IE中阻止默认动作
    Event.returnValue=false;
  };
  alert('禁止使用右键!');
}

四、键盘事件(keyCode)

document.onkeydown=function (ev){
  var Event=ev||window.event;
  alert(Event.keyCode);
}

组合键: ctrl + c
Event.ctrlKey&&Event.keyCode==67

 /*禁止右击阻止事件的兼容方式写法*/
document.oncontextmenu=function (ev){
var ev=window.event||ev;
if (ev.preventDefault) {
ev.preventDefault();//w3c阻止默认事件
}else{
ev.returnValue=false;//IE阻止默认事件
};
}
/*对获取键盘键码的兼容写法*/
document.onkeydown=function (ev){
var e=window.event||ev;
console.log(e.keyCode);//打印键码
}

<禁止复制>的练习:

 <body>
<p id="con">我要的只是简单地,只是诚实的,好好享受平凡,会好的,一定会好的!我要的只是你爱我,可不是你恨我,哪来的那么多麻烦!</p>
</body>
<script>
var con=document.getElementById('con');
/*阻止元素右击事件*/
con.oncontextmenu=function(ev){
var Event=ev||window.event;
if (Event.preventDefault) {//阻止默认动作(W3C)
Event.preventDefault();
} else{//IE中阻止默认动作
Event.returnValue=false;
};
alert('禁止使用右键!');
}
/*阻止ctrl+c操作*/
document.onkeydown=function(ev){
var e=ev||window.event;
if (e.ctrlKey&&e.keyCode==67) {
if(e.preventDefault()){
e.preventDefault();
}else {
e.returnValue=false;
}
alert('不能这样操作!');
}
}
/*阻止鼠标按下操作*/
document.onmousedown=function(ev){
var e=ev||window.event;
if (e.preventDefault()) {
e.preventDefault();
} else {
e.returnValue=false;
}
alert('禁止鼠标按下!')
}
</script>

五、拖拽效果

主要知识点:

onmousedown onmousemove onmouseup

event.clientX event.clientY

offset client 系列属性

鼠标拖拽_T:

 <head>
<meta charset="UTF-8">
<title>鼠标拖拽_T</title>
<style>
*{margin:0;padding:0;list-style: none;}
#dot{
width:80px;
height:80px;
line-height: 30px;
text-align: center;
font-size:24px;
background: #D00000;
color:#fff;
cursor:move;
position:absolute;
left:300;
top:100;
}
</style>
</head>
<body>
<div id="dot"></div>
</body>
<script>
var dot=document.getElementById('dot');
var x,y;
var xStart,yStart;
var xEnd,yEnd;
dot.onmousedown=function(ev){
var e=window.event||ev;
x=e.offsetX;
y=e.offsetY;
dot.onmousemove=function(ev){
var e=window.event||ev;
var xEnd=e.clientX-x;
var yEnd=e.clientY-y;
dot.style.left=xEnd+'px';
dot.style.top=yEnd+'px';
}
}
dot.onmouseup=function(){
dot.onmousemove=null;
}
</script>

鼠标拖拽_M

 <head>
<meta charset="UTF-8">
<title>鼠标事件</title>
<style>
*{margin:0;padding:0;list-style: none;}
#dot{
width:80px;
height:80px;
line-height: 30px;
text-align: center;
font-size:24px;
background: #D00000;
color:#fff;
cursor:move;
position:absolute;
/* left:0;
top:0; */
}
</style>
</head>
<body>
<div id="dot"></div>
</body>
<script>
var dot=document.getElementById('dot');
var x,y;
var l1,t1;
var lm,tm;
var le,te;
var a=true;
dot.onmousedown=function(ev){
a=true;
var e=window.event||ev;
x=e.offsetX;
y=e.offsetY;
l1=e.clientX-x;
t1=e.clientY-y;
dot.style.left=l1+'px';
dot.style.top=t1+'px';
console.log(x,y);
}
dot.onmousemove=function(ev){
if(a){
var e=window.event||ev;
var lm=e.clientX-x;
var tm=e.clientY-y;
dot.style.left=lm+'px';
dot.style.top=tm+'px';
}
}
dot.onmouseup=function(ev){
a=false;
}
</script>