this.children.length刷新后变成0
来源:4-4 轮播图组件实现(上)
无敌打狗棒
2020-11-29
老师,我跟着教程写,写完初始化width那里的时候,页面自动刷新,是ok的。
但是,我F5刷新一下,就不行了,为什么呢?代码完全没动。
<template>
<div class="slider" ref="slider">
<div class="slider-group" ref="sliderGroup">
<slot></slot>
</div>
</div>
</template>
<script type="text/ecmascript-6">
import {addClass} from 'common/js/dom'
export default {
name: 'slider',
props: {
loop: {
type: Boolean,
default: true
},
autoPlay: {
type: Boolean,
default: true
},
interval: {
type: Number,
default: 4000
}
},
data() {
return {
}
},
mounted() {
setTimeout(() => {
this._setSliderWidth()
}, 20)
},
methods: {
_setSliderWidth(isResize) {
this.children = this.$refs.sliderGroup.children
let width = 0
let sliderWidth = this.$refs.slider.clientWidth
console.log(this.children);
console.log(this.children.length);
for (let i = 0; i < this.children.length; i++) {
let child = this.children[i]
addClass(child, 'slider-item')
child.style.width = sliderWidth + 'px'
width += sliderWidth
}
if (this.loop && !isResize) {
width += 2 * sliderWidth
}
this.$refs.sliderGroup.style.width = width + 'px'
}
}
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
@import "~common/stylus/variable"
.slider
min-height: 1px
.slider-group
position: relative
overflow: hidden
white-space: nowrap
.slider-item
float: left
box-sizing: border-box
overflow: hidden
text-align: center
a
display: block
width: 100%
overflow: hidden
text-decoration: none
img
display: block
width: 100%
.dots
position: absolute
right: 0
left: 0
bottom: 12px
text-align: center
font-size: 0
.dot
display: inline-block
margin: 0 4px
width: 8px
height: 8px
border-radius: 50%
background: $color-text-l
&.active
width: 20px
border-radius: 5px
background: $color-text-ll
</style>
写回答
2回答
-
无敌打狗棒
提问者
2020-11-29
解决了,是我v-if那里写错了~
00 -
无敌打狗棒
提问者
2020-11-29
好像是dom没有挂载的原因,当我设置更长的延时后,就可以了。为什么会这么慢呢?(20,百分百不行,200,概率可以,2000,百分百可以) 是和电脑配置有关吗?
032020-12-01
相似问题