前端-用js实现图层蒙版效果

1、设置viewport这个是视点,主要是在移动web上面应用

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">

2、使用到了:

background-image: url("case2/2.jpg");设置背景图片
background-repeat: no-repeat; 设置背景填充的模式
background-size: cover;设置背景大小
background-position: center;设置背景的position

-webkit-mask-image:url("case2/1.png");设置图层蒙版的image
-webkit-mask-size:1px 1px; 设置图层蒙版的大小
-webkit-mask-repeat:no-repeat; 设置图层蒙版的填充模式
-webkit-mask-position:;设置图层蒙版的position

3、用到了手指触屏的事件:

事件触碰开始:ontouchstart触屏开始

事件触碰move:ontouchmove触屏move

事件触碰end:ontouchend

其中用到:

var touch=e.touches[0]; //获取到触碰的对象
var x=touch.clientX ; //获取x
var y=touch.clientY ; //获取y

 

例:

<!DOCTYPE html>
<html>
<head>
    <title>this is day3 practice</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        html,body,div{
            height: 100%;
        }
        div{
            background-image: url("case2/2.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            background-position: center;
        }
        .m{
            -webkit-mask-image:url("case2/1.png");
            -webkit-mask-size:1px 1px;
            -webkit-mask-repeat:no-repeat;
            -webkit-mask-position:;
        }
        .show{
            -webkit-mask-size:200px 200px;
        }
    </style>
</head>
<body>
<div class="m"></div>
<script type="text/javascript">
    var div=document.querySelector("div");
    div.ontouchstart=function(e){
        var touch=e.touches[0];
        var x=touch.clientX + div.offsetLeft - 100;
        var y=touch.clientY + div.offsetTop - 100;
        div.style.webkitMaskPosition = x + "px "+y+"px";
        div.className="m show";

        div.ontouchmove=function(e2){
            var touch2=e2.touches[0];
            var x2=touch2.clientX + div.offsetLeft - 100;
            var y2=touch2.clientY + div.offsetTop - 100;
            div.style.webkitMaskPosition = x2 + "px "+ y2+"px";
            div.className="m show";
        }

        div.ontouchend=function(){
            div.className="m";
            div.ontouchmove=null;
        }
    }
</script>
</body>
</html>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值