# 有趣的拖动黑白对比图片特效

# 快速导航

# 示例效果

# 原生版本实现

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>有趣的拖动黑白对比图片特效</title>
  <style>
    .container{
      max-width:900px;
      margin:auto;
    }

    #inked-painted { 
      position: relative;
      font-size: 0; 
      -ms-touch-action: none;
      -webkit-touch-callout: none;
      -webkit-user-select: none;
    }

    #inked-painted img { 
      width: 100%;
      height: auto; 
    }

    #colored { 
      background-image: url(https://codyhouse.co/demo/image-comparison-slider/img/img-original.jpg);
      position: absolute;
      top: 0; 
      left: 0;
      height: 100%;
      width: 50%;
      background-size: cover; 
    }

    #inked-painted:hover {
      cursor: col-resize; 
    }
  </style>

</head>
<body>
<div class="container">
  <div id="inked-painted">
    <img src="https://codyhouse.co/demo/image-comparison-slider/img/img-modified.jpg" id="inked" alt>
    <div id="colored"></div>
  </div>
  
</div>
<script>
    var inkbox = document.getElementById("inked-painted");
    var colorbox = document.getElementById("colored");

    var fillerImage = document.getElementById("inked");

    inkbox.addEventListener("mousemove",trackLocation,false);
    inkbox.addEventListener("touchstart",trackLocation,false);
    inkbox.addEventListener("touchmove",trackLocation,false);

    function trackLocation(e){
        var rect = fillerImage.getBoundingClientRect();
        var position = ((e.pageX - rect.left) / fillerImage.offsetWidth)*100;
        if(position <= 100){ 
        colorbox.style.width = position+"%";
    }
    }   
</script>

</body>
</html>
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

# Vue版本实现

如下是示例代码

<template>
    <div>
        <div class="container">
            <div id="inked-painted" ref="inkedPainted">
                <img src="https://codyhouse.co/demo/image-comparison-slider/img/img-modified.jpg" id="inked" ref="inkedImg" alt>
                <div id="colored" ref="colored"></div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'drawHeiBai',
    mounted() {
        this.$refs.inkedPainted.addEventListener('mousemove',this.trackLocation,false);
        this.$refs.inkedPainted.addEventListener('touchstart',this.trackLocation,false);
        this.$refs.inkedPainted.addEventListener('touchmove',this.trackLocation,false);
    },
    methods: {
        trackLocation(e) {
            let rect = this.$refs.inkedImg.getBoundingClientRect();
	        let position = ((e.pageX - rect.left) / this.$refs.inkedImg.offsetWidth)*100;
	        if(position <= 100){ 
                this.$refs.colored.style.width = position+"%";
            }
        }
    }

}
</script>
<style scoped>
#inked-painted { 
  position: relative;
  font-size: 0; 
  -ms-touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
}

#inked-painted img { 
  width: 100%;
  height: auto; 
}

#colored { 
  background-image: url(https://codyhouse.co/demo/image-comparison-slider/img/img-original.jpg);
  position: absolute;
  top: 0; 
  left: 0;
  height: 100%;
  width: 50%;
  background-size: cover; 
}

#inked-painted:hover {
  cursor: col-resize; 
}
</style>

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
白色

关注公众号

一个走心,有温度的号,同千万同行一起交流学习

加作者微信

扫二维码 备注 【加群】

扫码易购

福利推荐