点击平滑滚动效果

 <div>
        <button>点击跳转</button>
        <div>

            <span>我是开始位置</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>

        </div>
    </div>```
    只是留给实现功能的笔记 样式不管了
 

```javascript


给个高度

 span {
            width: 70px;
            height: 100px;
            display: block;
            border: 1px saddlebrown solid;
        }

        div {
            width: 200px;

            margin: auto;
            text-align: center;
        }

复制几份变得更高

   <button>点击跳转</button>
        <div>

            <span>我是开始位置</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>
            
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>

            <span>9</span>

        </div>

然后写效果

   let start = document.querySelectorAll("span")
    //获取按钮
    let button1 = document.querySelectorAll("button")[0]


    // 点击事件函数
    button1.onclick = function () {

        //高度*长度 
        var dis = start[0].offsetHeight * Number(start.length)

        // 设置一个初始值
        let a = 20
        aa(a, dis)
        function aa(x1, y1) {
            let distance = y1 - x1  // 全部长度减掉初始值
            let xx = x1   //赋值


            setTimeout(() => {//定时器
                const dist = Math.ceil(distance / 10)  //常量
                //加
                xx += dist
                console.log(xx)
                window.scrollTo(xx, x1)
                // 在调用
                // xx等于初始值+ distance/10 因为每次循环xx都会变大增加,每次循环都会y1减x1 所有distance会变小 所以xx最大只能达到和y1一样大
                aa(xx, y1)

            }, 100)//设置动画时间



        }




    }

效果不传了就这样了
如果有需要可以加个判断条件清除定时器

全部代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            width: 70px;
            height: 100px;
            display: block;
            border: 1px saddlebrown solid;
        }

        div {
            width: 200px;

            margin: auto;
            text-align: center;
        }
    </style>
</head>

<body>

    <div>
        <button>点击跳转</button>
        <div>

            <span>我是开始位置</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>

            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
            <span>7</span>
            <span>8</span>
            <span>9</span>

            <span>9</span>

        </div>
    </div>

</body>
<script>



    // 获取第一个span 开始位置
    let start = document.querySelectorAll("span")
    //获取按钮
    let button1 = document.querySelectorAll("button")[0]


    // 点击事件函数
    button1.onclick = function () {

        //高度*长度 
        var dis = start[0].offsetHeight * Number(start.length)

        // 设置一个初始值
        let a = 20
        aa(a, dis)
        function aa(x1, y1) {
            let distance = y1 - x1  // 全部长度减掉初始值
            let xx = x1   //赋值


            setTimeout(() => {//定时器
                const dist = Math.ceil(distance / 10)  //常量
                //加
                xx += dist
                console.log(xx)
                window.scrollTo(xx, x1)
                // 在调用
                // xx等于初始值+ distance/10 因为每次循环xx都会变大增加,每次循环都会y1减x1 所有distance会变小 所以xx最大只能达到和y1一样大
  if (y1 > xx) {
                    // 
                    aa(xx, y1)
                }
               // aa(xx, y1)

            }, 100)//设置动画时间



        }




    }

</script>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值