仿京东分类栏组件

news/2024/7/10 0:03:39 标签: vue, js, html, css

京东效果展示

在这里插入图片描述
功能描述:
1.底部进度条跟随左滑变化
2.可以规定单行展示个数,以及单行总个数

代码

1.index.html" title=vue>vue 入口组件

html" title=js>js"><template>
    <div class="container">
        <div class="title">nav-show</div>
        <!-- column-num 一行多少个  row-show 视口展示几个 -->
        <nav-show :column-num="8" :row-show="3">
            <nav-show-item v-for="item in list">
                <div class="show_box">
                    <img :src="item.img" alt="" />
                    <div>{{ item.name }}</div>
                </div>
            </nav-show-item>
        </nav-show>
    </div>
</template>
<script>
import navShow from "./navShow.html" title=vue>vue";
import navShowItem from "./navShowItem.html" title=vue>vue";
export default {
    components: {
        navShow,
        navShowItem,
    },
    data() {
        return {
            msg: "nav",
            img: require("./girl.jpg"),
            list: [
                {
                    img: require("./girl.jpg"),
                    name: "看我1",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我2",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我3",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我4",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我5",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我6",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我7",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我8",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我9",
                },
                {
                    img: require("./girl.jpg"),
                    name: "看我10",
                },
            ],
        };
    },
};
</script>
<style lang="less" scoped>
.container {
    width: 100%;
    height: 100vh;
    padding-top: 50px;
    .title {
        text-align: center;
    }

    .show_box {
        margin: auto;
        width: 100%;
        background: azure;
        img {
            width: 100%;
            height: 50px;
        }
        div {
            height: 50px;
            line-height: 50px;
            font-size: 16px;
            color: aqua;
        }
    }
}
</style>

  1. NavShow.html" title=vue>vue父容器组件
html" title=js>js"><template>
    <!-- 最外层父容器 -->
    <div class="nav_show">
        <!-- :style="{ color: activeColor, fontSize: fontSize + 'px' }" -->
        <!-- 栅格展示容器 -->
        <div class="nav_show_top" ref="show_box" @scroll="move">
            <!-- 背景容器  实际单行盛满的容器 -->
            <div class="bg_box" ref="bg_box" :style="{ width: bgWidth + '%' }">
                <!-- 使用者自定义单个子容器 -->
                <slot></slot>
            </div>
        </div>
        <!-- 进度条 -->
        <div class="progress_bar" ref="bar">
            <!-- 可移动浮标 -->
            <div
                class="buoy"
                ref="buoy"
                :style="{ left: buoyPer + 'px' }"
            ></div>
        </div>
    </div>
</template>
<script>
export default {
    name: "navShow",
    props: {
        // 单行展示列数
        columnNum: {
            type: Number,
            default: 5,
        },
        // 可视区内单行展示个数
        rowShow: {
            type: Number,
            default: 5,
        },
    },
    data() {
        return {
            itemWidth: "", //单个子组件宽度
            scrollLeft: "", //滚动条向左滚动距离
            buoyPer: "", //浮标可移动百分比
        };
    },
    computed: {
        // 按照 单行展示列数 计算单行宽度
        bgWidth() {
            let per = (100 / this.rowShow) * this.columnNum;
            console.log("bg:" + per);
            return per;
        },
    },
    methods: {
        // 滚动条事件
        move(e) {
            // 获取滚动距离
            this.scrollLeft = e.target.scrollLeft;
            // 公式:滚动条移动距离/滚动条可移动总长度 = 浮标移动距离/浮标可移动总长度
            // 视图部分
            let bgWidth = this.$refs.bg_box.offsetWidth; //实际背景宽度
            let showWidth = this.$refs.show_box.offsetWidth; //展示窗口宽度
            let moveWidth = e.target.scrollLeft; //移动距离
            let moveMax = bgWidth - showWidth; //可移动宽度
            // 浮标部分
            this.movePer = moveWidth / moveMax; //滚动条移动比例
            let barWidth = this.$refs.bar.offsetWidth; //进度条长度
            let buoyWidth = this.$refs.buoy.offsetWidth; //浮标宽度
            let buoyMax = barWidth - buoyWidth; //浮标可移动总长度
            this.buoyPer = buoyMax * this.movePer; //浮标移动的距离
        },
    },
    created() {
        this.itemWidth = 100 / this.columnNum; // 计算单个子容器的宽度
    },
    mounted() {
        console.log(this.$refs);
    },
};
</script>
<style lang="less" scoped>
.nav_show {
    position: relative;
    padding-bottom: 20px;
    // border: 1px dashed lightcoral;

    width: 100%;
    .nav_show_top::-webkit-scrollbar {
        width: 0 !important;
    }
    .nav_show_top {
        width: 100%;
        overflow: scroll;
        .bg_box {
            display: flex;
            flex-wrap: wrap;
            height: 100%;
        }
    }
    .progress_bar {
        position: absolute;
        width: 50%;
        height: 5px;
        background: #c3c3c3;
        border-radius: 3%;
        left: 50%;
        bottom: 15px;
        transform: translateX(-50%);
        .buoy {
            position: absolute;
            width: 50%;
            height: 100%;
            background: lightblue;
            border-radius: 3%;
        }
    }
}
</style>

3.子容器组件NavShowItem.html" title=vue>vue

html" title=js>js"><template>
    <div class="nav_show_item" :style="{ width: itemWidth + '%' }">
        <!-- 使用者的每一个样式 -->
        <slot></slot>
    </div>
</template>
<script>
export default {
    data() {
        return {
            itemWidth: "", //每一个子盒子的宽度
        };
    },
    mounted() {
        // 从父容器里面data中获取计算后的宽度
        this.itemWidth = this.$parent.itemWidth; //从父容器获得子容器宽度
    },
};
</script>


拓展功能

这两个拓展小编没有添加,难度不大,可根据业务添加

html" title=js>js">1.底部进度条可配置 显示/隐藏
2.进度条宽度、浮标宽度可配置

http://www.niftyadmin.cn/n/1852450.html

相关文章

5分钟实战获取1000条锚文本外链技巧

大家好&#xff0c;很久没有给各位带来实实在在的干货了&#xff0c;我记得今年在4月份的时候在卢松松博客上面投稿了两篇关于SEO的文章&#xff0c;谈到了很多外链的核心点和SEO的一些面的层面&#xff0c;虽然评论不错&#xff0c;但是很多的朋友加了我以后都问了我既然外链是…

CSS(一) 经典布局(两边固定,中间自适应)的五种方式

两边固定&#xff0c;中间自适应 本篇总结五种思路实现方式&#xff0c; 圣杯布局 圣杯布局&#xff0c;方便理解是带有两只耳朵的奖杯&#xff0c;耳朵跟奖杯是一体&#xff0c;所以左右两边跟中间同级&#xff0c;但是content在上面 第一步&#xff1a;中间盒子100%&#…

45-java中的try--catch的使用

程序运行过程中可能会出现异常情况&#xff0c;比如被0除、对负数计算平方根等&#xff0c;还有可能会出现致命的错误&#xff0c;比如内存不足&#xff0c;磁盘损坏无法读取文件等&#xff0c;对于异常和错误情况的处理&#xff0c;统称为异常处理。 try{……}里面是一些你觉…

前端模板 artTemplate之辅助方法template.helper

var labelMap { onlinePayment:{ label:"在线支付", desc:"支持大部分储蓄卡、信用卡及第三方平台支付", name:"", href:"" }, cashOnDelivery:{ label:"货到付款", desc:"货到付款只支持POS机刷卡支付&#xff0c;不…

揭秘今日头条推荐10万+

我是一个很不喜欢夸张式标题党的运营人。尤其是那些&#xff1a; 《我用1个标题引爆了10w》《我是如何在3天内&#xff0c;吸粉300万》《一场活动0成本&#xff0c;引来用户100万》 这种“深藏不漏”的运营文&#xff0c;不知祸害了多少运营人。天天提着裤子被老板追着分享&a…

46-数组合并和判断集合是否为空的代码以及源代码

package com.xukaiqiang.ArrayList;import java.util.ArrayList; import java.util.List;import org.apache.commons.lang3.ArrayUtils; import org.springframework.util.CollectionUtils;/*** 数组的合并,集合判断是否为空*/ public class App {public static void main(Stri…

sql 优化之:实现小数据量和海量数据的通用分页显示存储过程(系列四)

建立一个web 应用&#xff0c;分页浏览功能必不可少。这个问题是数据库处理中十分常见的问题。经典的数据分页方法是:ADO 纪录集分页法&#xff0c;也就是利用ADO自带的分页功能(利用游标)来实现分页。但这种分页方法仅适用于较小数据量的情形&#xff0c;因为游标本身有缺点&a…

ABAP区别CLEAR、REFRESH、FREE

CLEAR、REFRESH、FREE 内表&#xff1a;如果使用有表头行的内表&#xff0c;CLEAR 仅清除表格工作区域。例如 clear gs-school 清除工作区。 要重置整个内表而不清除表格工作区域&#xff0c;使用REFRESH语句或 CLEAR 语句CLEAR <itab>[].&#xff1b;REFRESH加不加中括号…