Weex 自定義輪播indicator

  • 直接上代碼


    KoRSfsAL8B.gif
<template>
    <div>
        <div :style="size">
            <slider style="background-color: red;" class="slider" auto-play="true" :interval="interval" @change="onchange" :index="defaultIndex" :style="size">
                <div v-for="(src, i) in srcs" :key="i">
                    <image :style="size" resize="cover" :src="src" @load="onload" placeholder="https://img.alicdn.com/tfs/TB1CWnby7yWBuNjy0FpXXassXXa-32-32.gif"></image>
                </div>
            </slider>
            <div class="indicator" :style="{ width: itemWidth * srcs.length, left: (size.width - itemWidth * srcs.length) / 2 , bottom: moveMarBootm}">
                <div ref="move" class="move" :style="{ width: itemWidth, marginLeft: itemWidth * defaultIndex, backgroundColor: moveColor }"></div>
            </div>
        </div>
    </div>
</template>

<script>
const animation = weex.requireModule('animation')

export default {
    props: {
        srcs: { type: Array, defalut: [] },
        defaultIndex: { type: Number, defalut: 0 },
        itemWidth: { type: Number, default: 35 } ,//移動塊的單個寬度
        moveColor: {type: String, default: '#666666'},
        moveMarBootm: {type: Number, default: 20},
        size: {type: Object, default: {width: 680, height: 400, borderRadius: 20}}
    },
    data() {
        return {
            scrollIndex: 0, //記錄滑動后的選中
            loading: true,
            interval: 3000,
            moveInterval: 300 //移動塊, 移動動畫時間, 比slider動畫時間小
        };
    },
    components: {},
    computed: {},
    created() {},
    methods: {
        onload: function(e) {
            this.$emit(e);
        },

        onchange(e) {
            //當前改變的index
            let index = e.index;
            var time = this.moveInterval;
            if (index == 0) {
                if (this.scrollIndex == this.srcs.length - 1) {
                    time = 0;
                }
            }
            if (index == this.srcs.length - 1) {
                if (this.scrollIndex == 0) {
                    time = 0;
                }
            }
            this.animalBegin(this.itemWidth * index, time, index);
        },

        animalBegin(moveWidth, time, index) {
            var testEl = this.$refs.move;
            let self = this;
            animation.transition(
                testEl,
                {
                    styles: {
                        transform: `translateX(${moveWidth}px, 0px)`
                    },
                    duration: time, //ms
                    timingFunction: 'ease-out',
                    delay: 0 //ms
                },
                function() {
                    self.scrollIndex = index;
                    // modal.toast({ message: 'animation finished.' });
                }
            );
        }
    }
};
</script>

<style scoped>
.size {
    width: 750px;
    height: 500px;
}
.slider {
    position: relative;
}

.indicator {
    position: absolute;
    height: 7px;
    background-color: rgba(255, 255, 255, 0.6);
}
.move {
    height: 7;
}
</style>
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容