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;
                }
            }
相关推荐
web150850966413 小时前
【React&前端】大屏适配解决方案&从框架结构到实现(超详细)(附代码)
前端·react.js·前端框架
理想不理想v3 小时前
前端项目性能优化(详细)
前端·性能优化
CodeToGym3 小时前
使用 Vite 和 Redux Toolkit 创建 React 项目
前端·javascript·react.js·redux
Cachel wood4 小时前
Vue.js前端框架教程8:Vue消息提示ElMessage和ElMessageBox
linux·前端·javascript·vue.js·前端框架·ecmascript
PP东5 小时前
ES6学习Generator 函数(生成器)(八)
javascript·学习·es6
桃园码工6 小时前
4_使用 HTML5 Canvas API (3) --[HTML5 API 学习之旅]
前端·html5·canvas
桃园码工6 小时前
9_HTML5 SVG (5) --[HTML5 API 学习之旅]
前端·html5·svg
人才程序员6 小时前
QML z轴(z-order)前后层级
c语言·前端·c++·qt·软件工程·用户界面·界面
m0_548514776 小时前
前端三大主流框架:React、Vue、Angular
前端·vue.js·react.js
m0_748232397 小时前
单页面应用 (SPA):现代 Web 开发的全新视角
前端