四、进阶版完整代码

JavaScriptJqueryExampleHtml2canvas

# css文件glass.css

*{
    margin:0;
    padding:0;
}
body{
    background-image: url('../images/hua.jpg');
    background-repeat: no-repeat;
    position: absolute;
    width: 100%;
    height: 100%;
}
#dv{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.5);
    display: none;
}

#btn{
    position:absolute;
    top: 100px;
    left: 100px;
    width: 100px;
    height:50px;
    background-color:rgba(0,0,0,.9);
    border-radius: 8px;
    line-height: 50px;
    text-align: center;
    color: #fff;
    cursor: pointer;
    font-size: 16px;
}

#mask{
    width: 200px;
    height:200px;
    position: absolute;
    top: 0;
    left: 0;
    background-color: rgba(255,150,0,.5);
    cursor: move;
}

#look{
    width: 300px;
    height:300px;
    position: absolute;
    bottom: 30px;
    right: 30px;
    border: 1px solid #f40;
    cursor: move;
    overflow: hidden;
}
#box{
    width: 100%;
    height:100%;
}

#jb{
    position:absolute;
    left: 100px;
    top:200px;
    width: 100px;
    height: 50px;
    background-color: rgba(0,0,0,.9);
    border-radius: 8px;
    line-height: 50px;
    text-align: center;
    color: #fff;
    cursor: pointer;
    font-size: 16px;
}

#txt{
    position: absolute;
    left: 100px;
    top: 300px;
    width: 100px;
    height: 50px;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

# index.html

<link rel="stylesheet" href="css/glass.css"/>
<script type="text/javascript" src="libs/jquery.min.js"></script>
<script type="text/javascript" src="libs/html2canvas.js"></script>
<script type="text/javascript" src="js/glass.js"></script>

<body id="bd">
hello world!!
<div id="jb">显示图标</div>
<input type="text" id="txt"/>
<div id="show"></div>
<div id='btn'>放大镜</div>
<div id="dv"><!--遮罩层-->
    <div id='box'></div><!--相当于小图-->
    <div id="mask"></div><!--跟着鼠标的区域-->
    <div id="look">
        <div id="move"></div><!--移动的地方-->
    </div><!--显示的区域-->
</div>

<script>
    $(function(){
        $('#dv').glass();

        $('#jb').on('click',function(){
            var img = document.createElement('img');
            img.src = 'images/jb.jpg';
            img.width = 100;
            img.height = 100;
            var leftRandom = Math.random()*1366;
            var topRandom = Math.random()*768;
            img.style.position = 'absolute';
            img.style.left = leftRandom + 'px';
            img.style.top = topRandom + 'px';
            $('#show').append(img);

        })
    });
</script>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# js文件glass.js

(function($){
    $.fn.glass = function(){
        var mask = $('#mask');
        var box = $('#box');
        var look = $('#look');
        var move = $('#move');
        var btn = $('#btn');
        var imgMaxMove;
        var maskMaxMove;
        var _this = $(this);

        //点击按钮
        btn.on('click',function(){
            move.html('');
            takeScreenshot();
            _this.css('display','block');
        });

        //点击屏幕的时候就消除
        _this.on('click',function(){
            _this.css('display','none');
        })


        //获取屏幕截图
        function takeScreenshot() {
            alert('1');
            html2canvas($('#bd'), {
                allowTaint: true,
                taintTest: false,
                onrendered: function(canvas) {
                    canvas.id = 'cv';
                    var dataUrl = canvas.toDataURL();
                    var newImg = document.createElement("img");
                    newImg.src = dataUrl;
                    newImg.width = 4 * $('#bd').width();
                    newImg.height = 4* $('#bd').height();
                    move.width(newImg.width);
                    move.height(newImg.height);
                    move.append(newImg);


                    _this.on('mousemove',function(e){
                        var left = e.clientX - mask.width() / 2;//横坐标
                        var top = e.clientY - mask.height() / 2;//纵坐标
                        //console.log(mask.width());
                        //设置遮挡层的left和top
                        var x = left;//margin
                        var y = top;//margin
                        x=x<0?0:x;//如果横坐标小于0就设置为0
                        y=y<0?0:y;//如果纵坐标小于0就设置为0
                        x=x>box.width()-mask.width()?box.width()-mask.width():x;
                        y=y>box.height()-mask.height()?box.height()-mask.height():y;
                        mask.offset({"left":x,"top":y});


                        //大图的最大的移动距离
                        imgMaxMove=newImg.width-look.width();
                        console.log(imgMaxMove);
                        //遮挡层的最大的移动距离
                        maskMaxMove=box.width()-mask.width();
                        console.log(maskMaxMove);

                        //大图的横向移动的距离
                        var imgMoveLeft=x*maskMaxMove/imgMaxMove*9;
                        //大图的纵向移动的距离
                        var imgMoveTop=y*maskMaxMove/imgMaxMove*9;
                        move.css("marginLeft",-imgMoveLeft);
                        move.css("marginTop",-imgMoveTop);
                    })
                },
            });
        }
    }
})(jQuery);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
更新时间: 2021-09-15 12:03