4

前端提高篇(九十四):鼠标事件

 3 years ago
source link: https://blog.csdn.net/qq_43523725/article/details/115360903
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

总起:
click / dblclick / contextmenu / event.which
mouseenter / mouseleave / mouseover / mouseout / mousedown / mousemove / mouseup

另附:
1.jQuery可以对一组标签绑定事件,js需要一个个循环遍历再绑定;
2.jQuery绑定了事件,用一个函数处理这个事件,函数中的this指的是触发事件的那个原生元素;如果要使用jQuery封装的函数,需要转成jQuery对象:$(this)

正文:

1.dblclick():鼠标双击

demo:双击显示小心心

img {
    width: 30px;
    height: 30px;
    position: absolute;
    margin-left: -15px;
    margin-top: -15px;
}/*为了让点击的位置是图片的中心,提前写了margin,在添加时再根据鼠标位置定位top和left*/
<h2>双击显示小心心</h2>
$(document).dblclick(function(e){
    $('body').append('<img src="./img/1.png" alt="" style="left:' + 
    e.pageX + 'px;' + 'top:' + e.pageY + 'px;">')
})

效果:
在这里插入图片描述

2.event.which与contextmenu()

event.which:可以判断鼠标哪个键按下了
contextmenu():取消右键时出现的菜单
左键返回1,滚轮返回2,右键返回3
当鼠标右键时,显示小心心

$(document).contextmenu(function(e){
    return false;
})//右键时出现的菜单取消掉,e虽然没有用,但一定要有
$(document).mousedown(function(e){
    if (e.which == 3){
        $('body').append('<img src="./img/1.png" alt="" style="left:' + 
    e.pageX + 'px;' + 'top:' + e.pageY + 'px;">')
    }
})

没有取消菜单时,右击会是这样:
在这里插入图片描述

取消掉,右击会是这样:
在这里插入图片描述
我的小心心图是这样的,本身有两个心心,所以点一下有两个心心:
在这里插入图片描述

3.mouseenter() / mouseleave() / mouseover() / mouseout()

前两个不会冒泡
后两个冒泡

<div class="wrapper">
    <div class="box">box</div>
</div>

3.1 mouseenter / mouseleave:

$('.box').mouseenter(function(){
    console.log('enter');
}).mouseleave(function(){
    console.log('leave');
})//给box绑定mouseenter和mouseleave事件

$('.wrapper').mouseenter(function(){
    console.log('enter-wrapper');
}).mouseleave(function(){
    console.log('leave-wrapper');
})//给wrapper绑定mouseenter和mouseleave事件

效果:
1.当鼠标进入wrapper(或刷新时已在wrapper区域,然后动一下)时,触发wrapper的enter事件;
2.当鼠标进入box区域时,触发box的enter事件,没有触发wrapper的enter事件,leave同理,说明这两个事件是不冒泡的(父子设置同样的事件,子触发,父不触发)
在这里插入图片描述

3.2 mouseover / mouseout

$('.box').mouseover(function(){
    console.log('over');
}).mouseout(function(){
    console.log('out');
})//给box绑定mouseover和mouseout事件

$('.wrapper').mouseover(function(){
    console.log('over-wrapper');
}).mouseout(function(){
    console.log('out-wrapper');
})//给wrapper绑定mouseover和mouseout事件

效果:
1.当我鼠标从wrapper外部进入(或刷新时鼠标已在wrapper区域然后鼠标动一下)时,触发wrapper的over事件;
2.鼠标移动到box区域,同时触发wrapper的mouseout事件、box的mouseover事件、wrapper的mouseover事件
3.当鼠标移开box区域到wrapper区域,触发box的mouseout事件、wrapper的mouseout事件、wrapper的mouseover事件
4.所以这两个事件是冒泡的,进入box时,box(子)和wrapper(父)的over事件都被触发
5.另外,从wrapper移动到box区域时,还触发了wrapper的out事件,即系统认为此时离开wrapper,进入box
在这里插入图片描述

在这里插入图片描述

4.mousedown / mousemove / mouseup

可以实现鼠标拖拽元素进行移动的效果
为防止鼠标移动过快,脱离绑定事件的元素,mousemove一般绑定在document上,而不是绑定在移动元素上

.box {
    position: absolute;
    left: 100px;
    top: 100px;
    width: 100px;
    height: 100px;
    background: pink;
}/* 设置了postion,left和top才有意义 */
<div class="box">box</div>
$('.box').mousedown(function(e){
    var offset = $(this).offset();
    var dis = {}
    dis.x = e.pageX - offset.left;
    dis.y = e.pageY - offset.top;
    var _this = this;//保存this,后面调用不直接使用div.box,提高内聚性
    $(document).mousemove(function(e){
        $(_this).css({
            left: e.pageX - dis.x,
            top: e.pageY - dis.y
        })
    }).mouseup(function(e){
        $(this).off('mousemove mouseup');
    })//鼠标抬起之后取消这两个鼠标事件
    return false;
    //防止选中文字,对文字拖拽
})

4.1 关于位置计算:

我们要做的,是根据鼠标移动的位置,设置box的left和top,让box跟着鼠标动,且鼠标不是在最左上角(看起来好看.jpg)
在这里插入图片描述

1.鼠标一开始点中box的某个位置,比如中心位置,此时可以获取鼠标的坐标(e.pageX, e.pageY),但鼠标的坐标不一定与box的left、top值一致,除非鼠标刚好点在box的最左上角
所以,当box跟着鼠标移动时,需要计算这部分差值,才能正确设置box的位置
2.鼠标点击box之后移动,如果一开始点击在box中心,鼠标停止移动时还是在box中心的,即鼠标与box的相对定位是固定的
所以,鼠标移动之后,坐标减去这段相对距离即可
在这里插入图片描述

4.2 关于善后

1.当鼠标抬起时,取消鼠标移动和鼠标抬起事件,这样box就不会再跟着鼠标走了
2.建议在mousedown事件最后加一句:return false;,否则当鼠标点击不小心选中了文字,浏览器会以为要拖拽这段文字,就像这样:
在这里插入图片描述
加了return false就不会有这种情况


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK