码迷,mamicode.com
首页 > Web开发 > 详细

jQuery实现返回顶部按钮功能

时间:2016-05-21 11:40:37      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

  • html结构
  • <div class="toolbar">
                <a href="javascript:;" class="toolbar-item">
                    <span class="toolbar-btn" id="backtop">
                        <i class="toolbar-icon icon-upload2"></i>  //图标字体
                        <span class="toolbar-text">返回<br />顶部</span>
                    </span>
                </a>
    </div>
  • css样式
  • .toolbar{
        position: fixed;
        right: 10px;
        bottom: 20px;
    }
    .toolbar-item{
        position: relative;
        display: block;
        width: 50px;
        height: 50px;
    
    
        &:hover{
            .toolbar-icon{
                top: -@toolbar-size;
            }
            .toolbar-text {
                top: 0;
            }
        }
    }
    .toolbar-btn{
        position: absolute;
        left: 0;
        top: 0;
        width: 50px;
        height: 50px;
        overflow: hidden;
    
        .toolbar-icon{
            position: absolute;
            left: 0;
            top: 0;
            width: 50px;
            height: 50px;
            background-color: #ccc;
            font-size: 30px;
            color: #fff;
            text-align: center;
            transition: top 1s;
        } 
        .toolbar-text{
            position: absolute;
            left: 0;
            top: 50px;
            width: 50px;
            height:50px;
            background-color: #A0A0A0;
            font-size: 14px;
            color: #fff;
            text-align: center;
            transition: top 1s;
        }
    }
  • jQuery实现返回顶部
  • <script type="text/javascript">
            $(function() {
                $(‘#backtop‘).on(‘click‘,move);
                $(window).on(‘scroll‘,function(){
                    checkposition( $(window).height() );
                });
    
                function move() {
                    $(‘html,body‘).animate({
                        scrollTop: 0
                    },800);
                }
    
                function checkposition(H) {
                    if($(window).scrollTop() > H){
                        $(‘#backtop‘).fadeIn();
                    }
                    else{
                        $(‘#backtop‘).fadeOut();
                    }
                }
            })
    </script>

     

至此,返回顶部按钮功能基本实现

jQuery实现返回顶部按钮功能

标签:

原文地址:http://www.cnblogs.com/zhuyinxiaozi/p/5514321.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!