-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapt-box.js
74 lines (72 loc) · 2.49 KB
/
adapt-box.js
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
(function (window) {
var AdaptBox = {
template: '<div class="adapt-box" ref="adaptBox">' +
' <div class="adapt-box-wrap" ref="adaptBoxWrap"><slot></slot></div>' +
'</div>',
props: {
gutter: {
type: Number,
default: 20
},
size: {
type: Number,
default: 180
},
offset: {
type: Number,
default: 0
}
},
mounted: function () {
this.justifyContent();
this.$refs.adaptBoxWrap.style.marginLeft = -this.gutter / 2 + 'px';
this.$refs.adaptBoxWrap.style.marginRight = -this.gutter / 2 + 'px';
window.addEventListener('resize', this.justifyContent.bind(this));
this.insetStyle();
},
data: function () {
return {}
},
methods: {
insetStyle: function () {
if(document.getElementsByClassName('adapt-box-style').length){return}
var style = document.createElement('style');
style.classList.add('adapt-box-style');
style.innerHTML = '.adapt-box{margin:0 auto;} .adapt-box-wrap:after{content:"";display:block;clear:both;}'
document.body.appendChild(style)
},
justifyContent: function () {
this.justifyContentWidth();
if(this.offset!==undefined){
this.justifyContentHeight();
}
},
justifyContentWidth: function () {
var parentWidth = this.$refs.adaptBox.parentElement.clientWidth;
var unit = this.size + this.gutter
var count = parseInt(parentWidth / unit);
var width = count * unit;
this.$refs.adaptBox.style.width = width - this.gutter + 'px';
this.$refs.adaptBox.parentElement.style.overflow = 'auto'
},
justifyContentHeight: function () {
var height = document.documentElement.clientHeight;
this.$refs.adaptBox.parentElement.style.height = height - this.offset + 'px'
}
},
beforeDestroy: function () {
window.removeEventListener('resize', this.justifyContent)
}
}
if (typeof window.define === 'function' && window.define.amd !== undefined) {
window.define('AdaptBox', [], function () {
return AdaptBox;
});
}
else if (typeof module !== 'undefined' && module.exports !== undefined) {
module.exports = AdaptBox;
}
else {
window.AdaptBox = AdaptBox;
}
})(this)