index.js
3.45 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 组件
import d2ContainerFull from "./components/d2-container-full.vue"
import d2ContainerFullBs from "./components/d2-container-full-bs.vue"
import d2ContainerGhost from "./components/d2-container-ghost.vue"
import d2ContainerGhostBs from "./components/d2-container-ghost-bs.vue"
import d2ContainerCard from "./components/d2-container-card.vue"
import d2ContainerCardBs from "./components/d2-container-card-bs.vue"
// import d2Source from './components/d2-source.vue'
export default {
name: "d2-container",
props: {
// 容器样式
type: {
type: String,
required: false,
default: "full"
},
// 滚动优化
betterScroll: {
type: Boolean,
required: false,
default: false
}
},
computed: {
// 始终返回渲染组件
component() {
if (this.type === "card" && !this.betterScroll) return d2ContainerCard
if (this.type === "card" && this.betterScroll) return d2ContainerCardBs
if (this.type === "ghost" && !this.betterScroll) return d2ContainerGhost
if (this.type === "ghost" && this.betterScroll) return d2ContainerGhostBs
if (this.type === "full" && !this.betterScroll) return d2ContainerFull
if (this.type === "full" && this.betterScroll) return d2ContainerFullBs
else {
return "div"
}
}
},
render(h) {
const slots = [this.$slots.default]
if (this.$slots.header) { slots.push(h("template", { slot: "header" }, [this.$slots.header])) }
if (this.$slots.footer) { slots.push(h("template", { slot: "footer" }, [this.$slots.footer])) }
return h(
"div",
{
ref: "container",
class: "container-component"
},
[
h(
this.component,
{
ref: "component",
props: this.$attrs,
on: {
scroll: (e) => this.$emit("scroll", e)
}
},
slots
)
// h(d2Source)
]
)
},
methods: {
// 返回顶部
scrollToTop() {
this.$refs.component.scrollToTop()
// 如果开启了 better scroll 还需要手动触发一遍 scroll 事件
const bs = this.$refs.component.BS
if (bs) this.$refs.component.scroll()
},
// 用法同原生方法 scrollBy
scrollBy(x = 0, y = 0, time = 300) {
if (this.betterScroll) {
const bs = this.$refs.component.BS
if (bs) {
bs.scrollBy(-x, -y, time)
// 手动触发一遍 scroll 事件
this.$refs.component.scroll()
}
} else {
this.$refs.component.$refs.body.scrollBy(x, y)
}
},
// 用法同原生方法 scrollTo
scrollTo(x = 0, y = 0, time = 300) {
if (this.betterScroll) {
const bs = this.$refs.component.BS
if (bs) {
bs.scrollTo(-x, -y, time)
// 手动触发一遍 scroll 事件
this.$refs.component.scroll()
}
} else {
this.$refs.component.$refs.body.scrollTo(x, y)
}
},
// 用法同原生方法 scrollTop
scrollTop(top = 0, time = 300) {
if (this.betterScroll) {
const bs = this.$refs.component.BS
if (bs) {
bs.scrollTo(bs.x, -top, time)
// 手动触发一遍 scroll 事件
this.$refs.component.scroll()
}
} else {
this.$refs.component.$refs.body.scrollTop = top
}
}
}
}