回到顶部的几种方案(js)

我最喜欢用最后一种:过渡回到顶部!推荐
注意: 最外层父级不可以设置overflow:scroll;否则失效.解决方法:是在外面在加一层div.
1.锚点

<body style="height:2000px;">
    <div id="topAnchor"></div>
    <a href="#topAnchor" style="position:fixed;right:0;bottom:0">回到顶部</a>
</body>


2.scrollTop

<body style="height:2000px;">
    <button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
    <script>
    test.onclick = function(){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    }
    </script>
</body>

3.scrollTo():scrollTo(x,y)方法滚动当前window中显示的文档,让文档中由坐标x和y指定的
点位于显示区域的左上角,设置scrollTo(0,0)可以实现回到顶部的效果.

<body style="height:2000px;">
    <button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script>
test.onclick = function(){
    scrollTo(0,0);
}
</script>
</body>

4.scrollBy():scrollBy(x,y)方法滚动当前window中显示的文档,x和y指定滚动的相对量

只要把当前页面的滚动长度作为参数,逆向滚动,则可以实现回到顶部的效果

<body style="height:2000px;">
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script>
test.onclick = function(){
var top = document.body.scrollTop || document.documentElement.scrollTop
scrollBy(0,-top);
}
</script>
</body>

5.scrollIntoView():Element.scrollIntoView方法滚动当前元素,进入浏览器的可见区域.
该方法可以接受一个布尔值作为参数。如果为true,表示元素的顶部与当前区域的可见部分的
顶部对齐(前提是当前区域可滚动);如果为false,表示元素的底部与当前区域的可见部分
的尾部对齐(前提是当前区域可滚动)。如果没有提供该参数,默认为true.

<body style="height:2000px;">
<div id="target"></div>
<button id="test" style="position:fixed;right:0;bottom:0">回到顶部</button>
<script>
test.onclick = function(){
target.scrollIntoView();
}
</script>
</body>

========================================
增加scrollTop的动画效果:

returnTop(){
      let that = this;
      cancelAnimationFrame(this.timer);
      this.timer = requestAnimationFrame(function fn(){
        let oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if(oTop > 0){
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          that.timer = requestAnimationFrame(fn);
        }else{
          cancelAnimationFrame(that.timer);
        } 
      });
}


增加scrollTo()动画效果:

var timer = null;
box.onclick = function(){
cancelAnimationFrame(timer);
    timer = requestAnimationFrame(function fn(){
    var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if(oTop > 0){
            scrollTo(0,oTop-50);
            timer = requestAnimationFrame(fn);
        }else{
            cancelAnimationFrame(timer);
        } 
    });
}

增加scrollBy()动画效果:
var timer = null;
box.onclick = function(){
    cancelAnimationFrame(timer);
    timer = requestAnimationFrame(function fn(){
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if(oTop > 0){
            scrollBy(0,-50);
            timer = requestAnimationFrame(fn);
        }else{
            cancelAnimationFrame(timer);
        } 
    });
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过监听RecyclerView的滑动事件来控制BottomSheet的状态。具体实现步骤如下: 1. 获取RecyclerView对象,设置滑动监听。 ```kotlin val recyclerView: RecyclerView = findViewById(R.id.recyclerView) recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) // 滑动事件处理 } }) ``` 2. 判断RecyclerView是否滑动到顶部。 ```kotlin val isTop = !recyclerView.canScrollVertically(-1) ``` 3. 获取BottomSheetBehavior对象,设置状态。 ```kotlin val bottomSheetLayout: LinearLayout = findViewById(R.id.bottomSheetLayout) val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout) if (isTop) { bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED } else { bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED } ``` 完整示例代码如下: ```kotlin val recyclerView: RecyclerView = findViewById(R.id.recyclerView) recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) val isTop = !recyclerView.canScrollVertically(-1) val bottomSheetLayout: LinearLayout = findViewById(R.id.bottomSheetLayout) val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout) if (isTop) { bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED } else { bottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED } } }) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值