滚动条在滚动,下面的盒子跟着滚动条移动

实现一起滚动 且不超过盒子的位置,需要添加算法,计算相对斜率,得到滚动条的滚动相对的比例 就是下面盒子的相对比例。

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <!-- 引入vue3的地址 CDN 引入  这时候需要访问远程的服务器-->

    <script src="../vue3/lib/vue.js"></script>

    <style>

        * {

            margin: 0;

            padding: 0;

            background: grey;

        }

        .box {

            width: 50%;

            margin: 100px auto;

            background-color: black;

        }

        .g-container {

            position: relative;

            width: 100%;

            height: 30px;

            border-radius: 30px;

            background: #eee;

            background-image: repeating-linear-gradient(-60deg, #f32b3f 0%, #f43d51 3%);

        }

        .g-progress {

            position: relative;

            width: 70%;

            height: 100%;

            border-radius: 30px;

            background: pink;

            max-width: 100%;

            min-width: 4%;

            background-image: linear-gradient(-60deg, #fff6f8 100%, #ffdedf 0%);

        }

        .shuxian {

            position: absolute;

            right: 0;

            /* margin-right: -5px; */

            top: 15px;

            height: 40px;

            border-left: 1px solid red;

            width: 1px;

            /* transform: translate(-100%); */

        }

        .shuxianshang {

            position: absolute;

            right: 0;

            /* margin-right: -5px; */

            bottom: 15px;

            height: 40px;

            border-left: 1px solid red;

            width: 1px;

        }

        .pointshang {

            position: absolute;

            border: 2px solid red;

            border-radius: 2px;

            bottom: -1px;

            left: -2px;

        }


 

        .contentshang {

            position: absolute;

            top: -20px;

            right: 0;

            background-color: aqua;

            white-space: nowrap;

            border-radius: 15px;

            padding: 0 5px;

            /* transform: translate(); */

        }

        .point {

            position: absolute;

            border: 2px solid red;

            border-radius: 2px;

            top: -1px;

            left: -2px;

        }


 

        .content {

            position: absolute;

            top: 45px;

            /* left: 0; */

            background-color: aqua;

            white-space: nowrap;

            border-radius: 15px;

            padding: 0 5px;

            /* transform: translate(); */

        }

        .leftclass {

            /* margin-left: 300px; */

            left: 0;

        }

        .rightclass {

            /* riht: 0; */

            right: 0;

        }

    </style>

</head>

<body>

    <div id="app">{{baifenbi}}

        <button @click="baifenbi+= 10">dianji</button>

        <div class="box">

            <div class="g-container" ref="containerpocess">

                <div class="g-progress" :style="{width:baifenbi+'%'}">

                    <!-- 设置小竖条 -->

                    <div class="shuxian" :style="{}">

                        <div class="point"></div>

                        <!-- 设置竖条下的框 -->

                        <div class="content" ref="content" :style="bottomstyle">

                            预估退休后收入 ¥8,300</div>

                    </div>


 

                </div>

                <div class="shuxianshang">

                    <div class="pointshang"></div>

                    <!-- 设置竖条下的框 -->

                    <div class="contentshang">

                        预估退休后调整支出 ¥8,300</div>

                </div>

            </div>



 

        </div>

    </div>

    <script>

        const app = Vue.createApp({

            data: function () {

                return {

                    baifenbi: 0,

                    shutiao: 0,

                    divwidth: '',


 

                }

            },

            mounted () {

                console.log(this.$refs.containerpocess.offsetWidth);

                console.log(this)

            },

            watch: {

                baifenbi: function (newval, oldval) {

                    console.log(oldval, newval);

                    newval = Math.max(4, newval)

                    newval = Math.min(100, newval)

                    middlePos = this.$refs.content.offsetWidth / 2

                    console.log(middlePos);


 

                    if (newval > 50) {

                        v = this.lerp(50, middlePos, 100, 0, newval)

                        console.log("v=", v)

                        this.bottomstyle = { 'right': 0 + 'px', 'margin-right': -v + 'px' }

                    } else {

                        v = this.lerp(4, 0, 50, middlePos, newval)

                        console.log("v=", v)

                        this.bottomstyle = { 'left': 0 + 'px', 'margin-left': -v + 'px' }

                    }

                }

            },

            computed: {

                // leftOrright () {

                //     // debugger

                //     if (parseInt(this.baifenbi) < 50) {

                //         return true

                //     } else {

                //         return false

                //     }

                // }

            },

            methods: {

                lerp (x0, y0, x1, y1, x) {

                    k = (y1 - y0) / (x1 - x0)

                    return y0 + (x - x0) * k

                },



 

            }

        })

        app.mount('#app')

    </script>

</body>

</html>

  • 32
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值