vue js css 刻度尺选取体重身高


css

复制代码
rem=  document.documentElement.clientWidth / 7.5 + "px";

 .weightAndHeight {
                width: 6.54rem;
                box-sizing: border-box;
                margin: 0 auto;
                background-color: #fff;
                border-radius: 12px;
                padding: 0.32rem;
                .title {
                    font-size: 0.32rem;
                    color: #222;
                    line-height: 0.5rem;
                    margin-bottom: 0.2rem;
                }
                .box {
                    &:first-child {
                        margin-bottom: 1.5rem;
                    }
                    &:last-child {
                        margin-bottom: 1rem;
                    }
                    position: relative;
                }
                //刻度尺
                .rulerContainer {
                    position: relative;
                    //红色刻度线
                    &::after {
                        content: "";
                        position: absolute;
                        // width: 20px;
                        height: 0.32rem;
                        border-right: 2px solid #FF4E3E;
                        top: 0.74rem;
                        left: calc(50% - 1px);
                    }
                    .scaleBox {
                        overflow-x: scroll;
                        //去除滚动条
                        &::-webkit-scrollbar {
                            display: none;
                        }
                        height: 65px;
                  		.ruler {
                            margin: 0 2.95rem;
                            display: inline-block;
                            width: 960px;
                            //刻度图片见文章上面
                            background-image: url(../img/testInfo/scale.png);
                            height: 16px;
                            background-repeat: repeat;
                            background-size: contain;
                            ul {
                                display: flex;
                                align-items: center;
                                justify-content: space-between;
                                position: relative;
                                top: -25px;
                                left: -5px;
                                li {
                                    list-style: none;
                                    font-size: 12px;
                                    color: #999;
                                    width: 60px;
                                }
                            }
                        }
                    }
                    //左右阴影部分
                    .shadowLeft {
                        position: absolute;
                        left: 0;
                        width: 0.6rem;
                        height: 1.1rem;
                        background-size: cover;
                        background-image: url(../img/testInfo/left.png);
                    }
                    .shadowRight {
                        position: absolute;
                        right: 0;
                        width: 0.6rem;
                        height: 1.1rem;
                        background-size: cover;
                        background-image: url(../img/testInfo/right.png);
                    }
                }
                //刻度值显示
                .data {
                    margin-top: 0.2rem;
                    position: absolute;
                    width: 100%;
                    text-align: center;
                    font-size: 0.48rem;
                    color: #1f1f1f;
                    font-weight: bold;
                    span {
                        font-size: 0.28rem;
                        color: #1f1f1f;
                    }
                }
                .rulerContainer2 {
                    @extend .rulerContainer;
                    .scaleBox {
                        .ruler {
                            width: 1320px;
                        }
                    }
                }
            }

**

html

**

复制代码
<div class="weightAndHeight">
                <div class="box">
                    <div class="title">体重</div>
                    <div class="rulerContainer">
                        <div class="shadowLeft"></div>
                        <div class="shadowRight"></div>
                        <div class="scaleBox weightScaleBox" @scroll="handleScroll('weight')" @touchEnd="handleScrollEnd('weight')">
                            <div class="ruler">
                                <ul>
                                    <li>0</li>
                                    <li>10</li>
                                    <li>20</li>
                                    <li>30</li>
                                    <li>40</li>
                                    <li>50</li>
                                    <li>60</li>
                                    <li>70</li>
                                    <li>80</li>
                                    <li>90</li>
                                    <li>100</li>
                                    <li>110</li>
                                    <li>120</li>
                                    <li>130</li>
                                    <li>140</li>
                                    <li>150</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="data">{{form.weight}}<span>kg</span></div>
                </div>
                <div class="box">
                    <div class="title">身高</div>
                    <div class="rulerContainer2">
                        <div class="shadowLeft"></div>
                        <div class="shadowRight"></div>
                        <div class="scaleBox heightScaleBox" @scroll="handleScroll" @touchEnd="handleScrollEnd">
                            <div class="ruler">
                                <ul>
                                    <li>30</li>
                                    <li>40</li>
                                    <li>50</li>
                                    <li>60</li>
                                    <li>70</li>
                                    <li>80</li>
                                    <li>90</li>
                                    <li>100</li>
                                    <li>110</li>
                                    <li>120</li>
                                    <li>130</li>
                                    <li>140</li>
                                    <li>150</li>
                                    <li>160</li>
                                    <li>170</li>
                                    <li>180</li>
                                    <li>190</li>
                                    <li>200</li>
                                    <li>210</li>
                                    <li>220</li>
                                    <li>230</li>
                                    <li>240</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="data">{{form.height}}<span>cm</span></div>
                </div>

            </div>

**

js

**

复制代码
data() {
            return {
                type: 1,
                form: {
                	weight: 65,
                    height: 175
                },
                nearWeightNumber: "",
                nearHeightNumber: "",
            };
        }

页面初始化的时候

复制代码
this.nearWeightNumber = this.form.weight * 6;
this.nearHeightNumber = (this.form.height - 30) * 6;//我身高最小值是30所以-30
document.getElementsByClassName("weightScaleBox")[0].scrollLeft = this.nearWeightNumber;
document.getElementsByClassName("heightScaleBox")[0].scrollLeft = this.nearHeightNumber;

滚动条滚动的时候调用

复制代码
handleScroll(type) {
                if (type == "weight") {
                    let scrollLeft = document.getElementsByClassName("weightScaleBox")[0].scrollLeft;
                    let number = (scrollLeft / 60).toFixed(1);//我单个刻度条的宽度是60
                    this.nearWeightNumber = number * 60//就近的刻度线的像素
                    this.form.weight = number * 10;
                } else {
                    let scrollLeft = document.getElementsByClassName("heightScaleBox")[0].scrollLeft;
                    let number = (scrollLeft / 60).toFixed(1);
                    this.nearHeightNumber = number * 60
                    this.form.height = number * 10 + 30;
                }
            },
            //滚动结束时 需要滚动到就近的刻度线的像素 也就是滚动时候存下来的变量
            handleScrollEnd(type) {
                if (type == "weight") {
                    document.getElementsByClassName("weightScaleBox")[0].scrollLeft = this.nearWeightNumber;
                } else {
                    document.getElementsByClassName("heightScaleBox")[0].scrollLeft = this.nearHeightNumber;
                }
            }
相关推荐
我命由我123455 小时前
CesiumJS 笔记 - 获取容器中心点、Cartesian3 clone 方法、修改 Cartesian3 对象的高度
前端·javascript·css·前端框架·html·html5·js
Hopebearer_6 小时前
页面突然只剩 DOM?一次静态资源版本错配排查
前端·部署
抱抱宝6 小时前
Agent-study项目教程(03):手写 Mini-ReAct Agent(不依赖框架)
javascript·人工智能·gpt·react.js·prompt·agent
东风破_6 小时前
ESLint 是什么?为什么你的项目需要它?
前端·后端·代码规范
用户938515635077 小时前
Next.js 笔记系统(二):Redis 数据服务与侧边栏组件拆分实战
javascript·全栈
java1234_小锋8 小时前
Vue3专题 - 组件基础
vue.js·vite
圣殿骑士-Khtangc8 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
BigTopOne9 小时前
【ijkplayer】 硬解码流程
前端
kyriewen9 小时前
DeepSeek Harness开源第一天我就上手了——和Claude Code的差距比想象中大
前端·ai编程·deepseek
捡田螺的小男孩9 小时前
什么是 Skill?手把手带你写一个简单有用的 Skill!
前端·后端·程序员