广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >Vue可左右滑动按钮组组件使用详解
  • 707
分享到

Vue可左右滑动按钮组组件使用详解

2024-04-02 19:04:59 707人浏览 薄情痞子
摘要

本文实例为大家分享了基于Vue可左右滑动按钮组组件,供大家参考,具体内容如下 左右两箭头控制按钮组左右移动,双击到最左或最右边,功能比较简单。如下所示 <template&g

本文实例为大家分享了基于Vue可左右滑动按钮组组件,供大家参考,具体内容如下

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

<template>
    <div class="demoButtons">
        <div class="buttonF">
            <el-row style="height:30px ">
                <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 :style="{'width':buttonWidth+'px'}" 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: {
            type: Array,
            default: () => {
                return []
            }
        },
        buttonWidth: {
            type: Number,
            default: 62
        }
    },
    data () {
        return {
            clickIndex: 0,
            currentSite: 0,
            showCount: 0,
            clickTimer: null,
        }
    },
    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({ marginLeft: this.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({ marginLeft: this.currentSite + 'px' })
            })
        },

        setButtons (data) {
            this.buttonData = data
        },
        resetData (index, data) {
            this.clickIndex = index
            this.$emit('resetData', data)
        }
    }
}
</script>
<style lang="sCSS" scoped>
.demoButtons {
    width: 100%;
    height: 100%;
}
.buttonF {
    width: 100%;
    margin: 0 auto;
    height: 30px;
    line-height: 30px;
}
.lableI {
    height: 30px;
    line-height: 30px;
    width: 20px;
    cursor: pointer;
}
.buttonBody {
    overflow: hidden;
    height: 30px;
    line-height: 30px;
    width: calc(100% - 40px);
    white-space: nowrap;
}

.el-tag {
    text-align: center;
    padding: 0px 8px !important;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    border-radius: 0px !important;
    overflow: hidden;
    font-size: 12px;
}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Vue可左右滑动按钮组组件使用详解

本文链接: https://www.lsjlt.com/news/140654.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue可左右滑动按钮组组件使用详解
    本文实例为大家分享了基于Vue可左右滑动按钮组组件,供大家参考,具体内容如下 左右两箭头控制按钮组左右移动,双击到最左或最右边,功能比较简单。如下所示 <template&g...
    99+
    2022-11-13
  • Vue可左右滑动按钮组组件怎么用
    这篇文章将为大家详细讲解有关Vue可左右滑动按钮组组件怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。具体内容如下左右两箭头控制按钮组左右移动,双击到最左或最右边,功能比较简单。如下所示<tem...
    99+
    2023-06-29
  • vue可滑动的tab组件使用详解
    本文实例为大家分享了vue可滑动tab组件使用的具体代码,供大家参考,具体内容如下 需求: 1. 每个tab-item的间距是相同的,可定制 2. 每一个tab-item的宽度是随着...
    99+
    2022-11-13
  • vue滑动解锁组件使用方法详解
    本文实例为大家分享了vue滑动解锁组件的使用,供大家参考,具体内容如下 这是一个pc端的滑动解锁组件 效果图: 话不多说,直接上代码 html部分 <template>...
    99+
    2022-11-13
  • JetpackCompose按钮组件使用实例详细讲解
    目录概述1.普通Button按钮2.IconButton图标按钮3.FloatingActionButton悬浮按钮总结概述 按钮组件Button是用户和系统交互的重要组件之一,它按...
    99+
    2023-05-16
    Android按钮组件 Jetpack Compose按钮组件
  • Flutter多选按钮组件Checkbox使用方法详解
    Flutter 中的多选按钮组件有两种,供大家参考,具体内容如下 1. Checkbox 多选按钮,一般用来表现一些简单的信息。 常用属性如下: (1). value  多...
    99+
    2022-11-12
  • Vue滑块解锁组件使用方法详解
    本文实例为大家分享了Vue滑块解锁组件的使用,供大家参考,具体内容如下 依据 JS拖动滑块验证 开发的 Vue 滑块解锁组件。 <template>   <div ...
    99+
    2022-11-13
  • Vue 可拖拽组件Vue Smooth DnD的使用详解
    目录简介和 Demo 展示API: Container属性生命周期回调事件API: Draggable实战简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu...
    99+
    2022-11-12
  • Vue加载中动画组件使用方法详解
    本文实例为大家分享了Vue加载中动画组件的使用,供大家参考,具体内容如下 (模仿ant-design加载中样式)效果图如下: ①创建Loading.vue组件: <templ...
    99+
    2022-11-13
  • vue-seamless-scroll无缝滚动组件使用方法详解
    本文实例为大家分享了vue无缝滚动组件vue-seamless-scroll的具体实现代码,供大家参考,具体内容如下 下载 cnpm i -S vue-seamless-scrol...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作