JS事件对象

事件对象----event=ev。

 

常用的属性:

1 screenX和screenY是以屏幕为基准进行测量;当前元素距离屏幕的尺寸。

2 clientX和clientY是以当前文档(绝对定位)为基准,不合适用于IE6-8.

3 pageX和pageY是以当前可视区域为基准,类似于固定定位。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        div{
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
<div></div>
<script>
    document.addEventListener('click',function (ev) {
        var e=ev || window.event;

        //alert(e.screenX +',' + e.screenY);
        alert(e.clientX +',' + e.clientY);
       // alert(e.pageX +',' + e.pageY);


    })
</script>

</body>
</html>

 

案例:结合scroll对象和事件对象,做出放大镜案例。

 

电商平台,购物网页经常有的将商品进行放大,便于观察。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
            border: none;
        }
        #box{
            width: 350px;
            height: 350px;
            /*background-color: red;*/
            margin:100px 0 0 100px;
            position: relative;
        }
        #small_box{
            width: 100%;
            height: 100%;
            border: 1px solid #cccccc;
            box-sizing: border-box;
            position: relative;
        }
        #small_box img{
            width: 100%;
            height: 100%;
        }
        #mask{
            width: 100px;
            height: 100px;
            background-color: rgba(255,255,0, .4);
            position: absolute;
            left: 0;
            top: 0;
            cursor: move;
            /*隐藏*/
            display: none;
        }
        #big_box{
            width: 600px;
            height: 600px;
            border: 1px solid #cccccc;
        /*    定位*/
            position: absolute;
            left: 360px;
            top: 0;
            overflow: hidden;
            display: none;

        }
        #big_box img{
            position: absolute;
            left: 0;
            top: 0;
        }
        #list{
            margin: 20px 0 0 100px;
        }
        #list ul li{
            float: left;
            margin: 5px;
            cursor: pointer;

        }
    </style>
</head>
<body>
<div id="box">
    <div id="small_box">
        <img src="images/pic001.jpg" alt="">
        <span id="mask"></span>
    </div>
    <div id="big_box">
        <img src="images/pic01.jpg" alt="" style="position: absolute;left:0;top:0;">
    </div>
</div>
<div id="list">
    <ul>
        <li><img src="images/pic0001.jpg" alt=""></li>
        <li><img src="images/pic0002.jpg" alt=""></li>
        <li><img src="images/pic0003.jpg" alt=""></li>
    </ul>
</div>

<script>
    window.addEventListener('load',function (ev) {
        //1获取标签
        var box=document.getElementById('box');
        var small_box=box.children[0];
        var big_box=box.children[1];
        var mask=small_box.children[1];
        var big_img=big_box.children[0];
        var list_lis=document.getElementById('list').getElementsByTagName('li');

        //2监听鼠标进入小盒子
        small_box.addEventListener('mouseover',function () {

            //2.1 显示隐藏内容
            mask.style.display='block';
            big_box.style.display='block';

            //2.2监听鼠标移动
            small_box.addEventListener('mouseover',function (ev) {
                var e=ev || window.event;

                //2.3求出鼠标坐标
                var pointX=e.pageX-box.offsetLeft - mask.offsetWidth*0.5;
                var pointY=e.pageY-box.offsetTop - mask.offsetHeight*0.5;

                //2.4边界检测
                if (pointX<0){//左右
                    pointX=0;
                }else if(pointX>=small_box.offsetWidth - mask.offsetWidth ){
                    pointX=small_box.offsetWidth - mask.offsetWidth
                }

                if (pointY<0){//上下
                    pointY=0;
                }else if(pointY>=small_box.offsetHeight - mask.offsetHeight ){
                    pointY=small_box.offsetHeight - mask.offsetHeight
                }


                //2.5让放大镜走起来
                mask.style.left=pointX+'px';
                mask.style.top=pointY+'px';

                //2.6让大盒子中图片走起来
                /**
                 * 公式:
                 smallX / bigX = Box.width / 大盒子宽度
                 bigX=smallX / (Box.width / 大盒子宽度)
                 */
                big_img.style.left= -pointX / (small_box.offsetWidth / big_box.offsetWidth)+'px';
                big_img.style.top=-pointY / (small_box.offsetHeight / big_box.offsetHeight)+'px';


            })
        });

        //3监听鼠标离开小盒子
        small_box.addEventListener('mouseout',function () {
            //3.1 显示隐藏内容
            mask.style.display='none';
            big_box.style.display='none';
        });
        //4遍历列表图片
        for (var i=0;i<list_lis.length;i++){
            (function (i) {
                var img=list_lis[i];
                img.onmouseover=function () {
                    small_box.children[0].src='images/pic00'+ (i+1) +'.jpg';
                    big_box.src='images/pic0'+ (i+1) +'.jpg';

                }

            })(i)
        }
    })
</script>

</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值