flag.vue 2.13 KB
<template>
    <div class="flag-box" v-if="flag">
		<div class="box" :style="{'top': setPosition(position.top) + 'px', 'left': setPosition(position.left) + 'px', 'right': setPosition(position.right) + 'px', 'bottom': setPosition(position.bottom) + 'px'}">
            <img :src="flag" class="flag-img" :style="{'width': setWidth(width) + 'px'}" mode="widthFix" />
		</div>
    </div>
</template>

<script>
    export default {
        data: function () {
            return {

            }
        },
        // props: ["flag", "position"]
        props: {
            flag: {
                required: true
            },
            position: {
                type: Object,
                default: () => ({
                    top: -6,
                    left: 10,
                    right: 0,
                    bottom: 0
                })
            },
            width: {
                type: Number,
                default: 46
            }
        },
        computed: {

            /**
             * 计算flag定位
             * @param {int} x [待转换定位值]
             */
            setPosition() {
                return (x) => {

                    // 判断参数冲突,调增返回
                    if (this.position.right && this.position.right != 0) {
                        this.$set(this.position, "left", undefined);
                    }
                    if (this.position.bottom && this.position.bottom != 0) {
                        this.$set(this.position, "top", undefined);
                    }

                    return x * ((document.documentElement.clientWidth || document.body.clientWidth) / 750);
                }
            },

            /**
             * 计算flag宽度
             * @param {int} w [flag宽度]
             */
            setWidth() {
                return (w) => {
                    return w * ((document.documentElement.clientWidth || document.body.clientWidth) / 750);
                }
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style rel="stylesheet/less" lang="less" scoped>
    @import './flag';
</style>