lxl-vue2pc 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.vue +73 -0
  2. package/package.json +15 -0
package/index.vue ADDED
@@ -0,0 +1,73 @@
1
+ <template>
2
+ <div v-bind:style="styleObject" class="scale-box">
3
+ <slot></slot>
4
+ </div>
5
+ </template>
6
+
7
+ <script>
8
+ import debounce from "lodash.debounce"
9
+ export default {
10
+ props: {
11
+ width: {
12
+ type: Number,
13
+ default: document.documentElement.clientWidth,
14
+ },
15
+ height: {
16
+ type: document.documentElement.clientHeight,
17
+ default: 1080,
18
+ },
19
+ },
20
+ components: {},
21
+ data() {
22
+ return {
23
+ scale: this.getScale(),
24
+ }
25
+ },
26
+ computed: {
27
+ styleObject(){
28
+ let obj = {
29
+ transform: `scale(${this.scale}) translate(-50%, -50%)`,
30
+ WebkitTransform: `scale(${this.scale}) translate(-50%, -50%)`,
31
+ width: this.width + "px",
32
+ height: this.height + "px",
33
+ }
34
+ return obj
35
+ }
36
+
37
+ },
38
+ mounted() {
39
+ this.getScale()
40
+ window.addEventListener("resize", this.setScale)
41
+ },
42
+ methods: {
43
+ getScale() {
44
+ // 固定好16:9的宽高比,计算出最合适的缩放比,宽高比可根据需要自行更改
45
+ console.log(window.innerWidth,'window.innerWidth');
46
+ let ww = window.innerWidth / this.width
47
+ let wh = window.innerHeight / this.height
48
+ return ww < wh ? ww : wh
49
+ },
50
+
51
+ setScale: debounce(function () {
52
+ // 获取到缩放比,设置它
53
+ let scale = this.getScale()
54
+ this.scale = scale
55
+ console.log(this.scale, "---")
56
+ }, 500),
57
+ },
58
+ beforeDestroy() {
59
+ window.removeEventListener("resize", this.setScale)
60
+ },
61
+ }
62
+ </script>
63
+
64
+ <style scoped lang="scss">
65
+ .scale-box {
66
+ transform-origin: 0 0;
67
+ position: absolute;
68
+ left: 50%;
69
+ top: 50%;
70
+ transition: 0.3s;
71
+ transform: translate(-50%, -50%);
72
+ }
73
+ </style>
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "lxl-vue2pc",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.vue",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "dependencies": {
13
+ "lodash.debounce": "^4.0.8"
14
+ }
15
+ }