温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Vue可左右滑动按钮组组件怎么用

发布时间:2022-02-28 09:14:31 阅读:231 作者:小新 栏目:开发技术
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章将为大家详细讲解有关Vue可左右滑动按钮组组件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

具体内容如下

左右两箭头控制按钮组左右移动,双击到最左或最右边,功能比较简单。如下所示

Vue可左右滑动按钮组组件怎么用

<template>
    <div class="demoButtons">
        <div class="buttonF">
            <el-row >
                <el-col class="lableI"><i class="el-icon-arrow-left " @click="moveButtons('left')" @dblclick="moveSide('left')" /></el-col>
                <el-col ref="buttonBody" class="buttonBody">
                    <el-row id="buttonRow" ref="buttonRow">
                        <el-tag : v-for="(item, index) in buttonData" :key="index" :type="index == clickIndex ? '' : 'info'" @click="resetData(index, item)">
                            <el-tooltip v-if="item.name && item.name.length >  parseInt(buttonWidth/12) - 1" placement="top" :content="item.name" effect="light">
                                <span>{{  resetName(item.name) }}</span>
                            </el-tooltip>
                            <span v-else>{{ item.name }}</span>
                        </el-tag>
                    </el-row>
                </el-col>
                <el-col class="lableI"><i class="el-icon-arrow-right " @click="moveButtons('right')" @dblclick="moveSide('right')" /></el-col>
            </el-row>
        </div>
    </div>
</template>
<script>
import $ from 'jquery'
export default {
    props: {
        buttonData: {
            typeArray,
            default() => {
                return []
            }
        },
        buttonWidth: {
            typeNumber,
            default62
        }
    },
    data () {
        return {
            clickIndex0,
            currentSite0,
            showCount0,
            clickTimernull,
        }
    },
    watch: {},
    created () {
        // this.setButtons()
    },
    mounted () {
        this.$nextTick(() => {
            this.showCount = parseInt(this.$refs.buttonBody.$el.clientWidth / this.buttonWidth// 一行能展示几个按钮
        })
    },

    methods: {
        //设置名字
        resetName (val) {
            let i = parseInt(this.buttonWidth / 12) - 1;
            if (val && val.length > i) {
                return val.slice(0, i)
            } else {
                return val
            }
        },
        // 单击移一格
        moveButtons (val) {
            if (this.clickTimer) {
                window.clearTimeout(this.clickTimer)
                this.clickTimer = null
            }
            this.clickTimer = window.setTimeout(() => {
                this.$nextTick(() => {
                    if (val == 'left') {
                        if (this.currentSite < 0) {
                            this.currentSite = this.currentSite + this.buttonWidth
                        }
                    } else {
                        const totalCount = this.buttonData.length // 总共几个按钮
                        const showIndex = -parseInt(this.currentSite / this.buttonWidth// 向左移了几个按钮
                        console.log(totalCount, 'totalLength'this.showCount, showIndex)
                        if (showIndex + this.showCount < totalCount) {
                            this.currentSite = this.currentSite - this.buttonWidth
                        }
                    }
                    $('#buttonRow').animate({ marginLeftthis.currentSite + 'px' })
                })
            }, 300)
        },
        // 双击到边
        moveSide (val) {
            if (this.clickTimer) {
                window.clearTimeout(this.clickTimer)
                this.clickTimer = null
            }
            this.$nextTick(() => {
                if (val == 'left') {
                    this.currentSite = 0
                } else {
                    const totalCount = this.buttonData.length // 总共几个按钮
                    if (totalCount > this.showCount) {
                        this.currentSite = -((totalCount - this.showCount) * this.buttonWidth)
                    }
                }
                $('#buttonRow').animate({ marginLeftthis.currentSite + 'px' })
            })
        },

        setButtons (data) {
            this.buttonData = data
        },
        resetData (index, data) {
            this.clickIndex = index
            this.$emit('resetData', data)
        }
    }
}
</script>
<style lang="scss" scoped>
.demoButtons {
    width100%;
    height100%;
}
.buttonF {
    width100%;
    margin0 auto;
    height30px;
    line-height30px;
}
.lableI {
    height30px;
    line-height30px;
    width20px;
    cursor: pointer;
}
.buttonBody {
    overflow: hidden;
    height30px;
    line-height30px;
    widthcalc(100% - 40px);
    white-space: nowrap;
}

.el-tag {
    text-align: center;
    padding0px 8px !important;
    height28px;
    line-height28px;
    cursor: pointer;
    border-radius0px !important;
    overflow: hidden;
    font-size12px;
}
</style>

关于“Vue可左右滑动按钮组组件怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI

开发者交流群×