5种回到顶部的写法从实现到增强

×

目录

[1]写法 [2]增强 [3]实现

前面的话

  本文先详细介绍回到顶部的5种写法,然后对其实现功能增加,最后得到最终实现

写法

【1】锚点

  使用锚点链接是一种简单的返回顶部的功能实现。该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置

  [注意]关于锚点的详细信息移步至此

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

【2】scrollTop

  scrollTop属性表示被隐藏在内容区域上方的像素数。元素未滚动时,scrollTop的值为0,如果元素被垂直滚动了,scrollTop的值大于0,且表示元素上方不可见内容的像素宽度

  由于scrollTop是可写的,可以利用scrollTop来实现回到顶部的功能

  [注意]关于页面的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>

增强

  下面对回到顶部的功能进行增强

【1】显示增强

  使用CSS画图,将“回到顶部”变成可视化的图形(如果兼容IE8-浏览器,则用图片代替)

  使用CSS伪元素伪类hover效果,当鼠标移动到该元素上时,显示回到顶部的文字,移出时不显示  

<style>
.box{
    position:fixed;
    right:10px;
    bottom: 10px;
    height:30px;
    width: 50px;
    text-align:center;
    padding-top:20px;
    background-color: lightblue;
    border-radius: 20%;
    overflow: hidden;
}
.box:hover:before{
    top:50%
}
.box:hover .box-in{
    visibility: hidden;
}
.box:before{
    position: absolute;
    top: -50%;
    left: 50%;
    transform: translate(-50%,-50%);
    content:‘回到顶部‘;
    width: 40px;
    color:peru;
    font-weight:bold;

}
.box-in{
    visibility: visible;
    display:inline-block;
    height:20px;
    width: 20px;
    border: 3px solid black;
    border-color: white transparent transparent white;
    transform:rotate(45deg);
}
</style>

<body style="height:2000px;">
<div id="box" class="box">
    <div class="box-in"></div>
</div>
</body>

【2】动画增强

  为回到顶部增加动画效果,滚动条以一定的速度回滚到顶部

  动画有两种:一种是CSS动画,需要有样式变化配合transition;一种是javascript动画,使用定时器来实现  

  在上面的5种实现中,scrollTop、scrollTo()和scrollBy()方法可以增加动画,且由于无样式变化,只能增加javascript动画

  定时器又有setIntervalsetTimeoutrequestAnimationFrame这三种可以使用,下面使用性能最好的定时器requestAnimationFrame来实现

  [注意]IE9-浏览器不支持该方法,可以使用setTimeout来兼容

  1、增加scrollTop的动画效果

  使用定时器,将scrollTop的值每次减少50,直到减少到0,则动画完毕

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

  2、增加scrollTo()动画效果

  将scrollTo(x,y)中的y参数通过scrollTop值获取,每次减少50,直到减少到0,则动画完毕

<script>
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);
        }
    });
}
</script>

  3、增加scrollBy()动画效果

  将scrollBy(x,y)中的y参数设置为-50,直到scrollTop为0,则回滚停止

<script>
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);
        }
    });
}
</script>

实现

  由于scrollTop、scrollBy()和scrollTo()方法,都以scrollTop值是否减少为0作为动画停止的参照,且三个动画的原理和实现都基本相似,性能也相似。最终,以最常用的scrollTop属性实现动画增强效果

  当然,如果觉得50的速度不合适,可以根据实际情况进行调整

<style>
.box{
    position:fixed;
    right:10px;
    bottom: 10px;
    height:30px;
    width: 50px;
    text-align:center;
    padding-top:20px;
    background-color: lightblue;
    border-radius: 20%;
    overflow: hidden;
}
.box:hover:before{
    top:50%
}
.box:hover .box-in{
    visibility: hidden;
}
.box:before{
    position: absolute;
    top: -50%;
    left: 50%;
    transform: translate(-50%,-50%);
    content:‘回到顶部‘;
    width: 40px;
    color:peru;
    font-weight:bold;

}
.box-in{
    visibility: visible;
    display:inline-block;
    height:20px;
    width: 20px;
    border: 3px solid black;
    border-color: white transparent transparent white;
    transform:rotate(45deg);
}
</style>

<body style="height:2000px;">
<div id="box" class="box">
    <div class="box-in"></div>
</div>
</body>

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

  欢迎交流

时间: 2024-08-05 02:49:04

5种回到顶部的写法从实现到增强的相关文章

转:5种回到顶部的写法从实现到增强

http://www.cnblogs.com/xiaohuochai/p/5836179.html 前面的话 本文先详细介绍回到顶部的5种写法,然后对其实现功能增加,最后得到最终实现 写法 [1]锚点 使用锚点链接是一种简单的返回顶部的功能实现.该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置 [注意]关于锚点的详细信息移步至此 <body style="height:2000px;">

5种回到顶部的写法

写法 [1]锚点 使用锚点链接是一种简单的返回顶部的功能实现.该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置 [注意]关于锚点的详细信息移步至此 <body style="height:2000px;">     <div id="topAnchor"></div>     <a href="#topAnchor" s

回到顶部的写法

[1]锚点 使用锚点链接是一种简单的返回顶部的功能实现.该实现主要在页面顶部放置一个指定名称的锚点链接,然后在页面下方放置一个返回到该锚点的链接,用户点击该链接即可返回到该锚点所在的顶部位置 <body style="height:2000px;"> <div id="topAnchor"></div> <a href="#topAnchor" style="position:fixed;rig

js回到顶部动画效果实现方法

达到回到顶部效果主要原理就是修改滚动条距离顶部的位置为零,滚动条距离顶部的位置介绍: 获取当前页面滚动条纵坐标的位置:document.body.scrollTop与document.documentElement.scrollTop 并且,document.body.scrollTop与document.documentElement.scrollTop两者有个特点,就是同时只会有一个值生效.比如document.body.scrollTop能取到值的时候,document.documentE

Android设置ScrollView回到顶部的三种方式 (转)

一.ScrollView.scrollTo(0,0)  直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置; 二.ScrollView.fullScroll(View.FOCUS_UP)  类似于手动拖回顶部,有滚动过程; 三.ScrollView.smoothScrollTo(0, 0) 类似于手动拖回顶部,有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置. 转自:http://blog.csdn.net/xuan

浮动【电梯】或【回到顶部】小插件:iElevator.js

iElevator.js 是一个jquery小插件,使用简单,兼容IE6,支持UMD和3种配置方式,比锚点更灵活. Default Options _defaults = { floors: null, btns: null, backtop: null, selected: '', sticky: -1, visible: { isHide: 'no', numShow: 0 }, speed: 400, show: function(me) { me.element.show(); }, h

点击状态栏让tableview回到顶部最简单的方法

先看官方解释,如图: 官方说一个屏幕中只能允许一个scrollsTop = YES;不然就不能滚回顶部了!! 最简单的方法: 那么就让一个屏幕中只存在一个scrollsTop = YES就可以了, 其他的scrollsTop = NO;那么就可以默认点击状态栏tableview回到顶部! 完毕!!! ************************************************************************************************* 附

【JQ+锚标记实现点击页面回到顶部】

前言:今天想写个页面常用到的[点击回到页面顶部或是首页的功能],生活和职场一样,总会有低谷的时候,这个时候咱也别怂.别怂.别怂,说三遍!那都不是事,工作没了,再找呗,就像我上周五,团队解散那天,我是笑着走的,还给小白挥了挥手,微笑一个.那句话叫什么来着,佛祖虽给你关了一扇门,说不定会再给你开一扇窗.明天我就换工作了,我感谢我的同学. a)下面看看实现,实现很简单,通过JQ判断滚动条向下滚动的长度大于多少时显示[回到顶部+回到首页]的图标(换一种理解:滚动条顶端距离页面顶部的距离),另外一种实现方

JS 回到顶部

最近在慕课网看了一个案例教程——回到顶部效果(原生js实现) <a href="javascript:;" id="btn" title="回到顶部"></a> 这个a标签就是用来实现回到顶部的链接,其中 href=”javascript:;”是用来阻止a标签的默认行为,因为我们是要触发点击事件让滚动条回到顶部,而不是跳转页面.当然,如果你将其设置为 href=””,也会默认跳转到页面顶部,但是用户体验就比较差,我们主要还