模拟JD返回顶部功能

模拟JD返回顶部功能,添加在一定高度内隐藏功能。

clipboard.png

返回顶部的前端实现。涵盖的内容主要: 前端样式(html排版),展示效果(CSS3 动画),以及展示效果脚本的编写(javascript编写)

HTML
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>仿JD返回顶部</title>
    <link rel="stylesheet" href="css/index.css"/>
    <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>
    <script src="js/index.js"></script>
</head>
<body>
<div class="jd-global-toolbar">
    <div class="jd-toolbar-wrap">
        <div class="jd-toolbar-tabs">
            <div class="jd-toolbar">
                <div class="jd-toolbar-tab jd-tab-vip">
                    <i class="tab-ico"></i>
                    <em class="tab-text">留言</em>
                </div>
                <div class="jd-toolbar-tab jd-tab-follow">
                    <i class="tab-ico"></i>
                    <em class="tab-text">客服</em>
                </div>
                <div class="jd-toolbar-tab jd-tab-top jd-disno" id="returnTop">
                    <i class="tab-ico"></i>
                    <em class="tab-text">顶部</em>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
CSS
需要根据自己的背景图,修改背景位置。
@charset "utf-8";
/**********************
 *CSS Animations by:
 * JD侧边栏
 * ljc
***********************/

body {
    margin: 0;
    padding: 0;
    height: 2000px;
}

i, em {
    font-style: normal;
}

.jd-disno{
    display: none ;
}

.jd-toolbar-wrap {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 9990;
    width: 0;
    height: 100%;
}

.jd-toolbar-tabs {
    position: absolute;
    top: 82%;
    left: -35px;
    width: 35px;
    margin-top: -61px;
}

.jd-toolbar-tab {
    position: relative;
    width: 35px;
    height: 35px;
    margin-bottom: 1px;
    cursor: pointer;
    background-color: #7a6e6e;
    -webkit-border-radius: 3px 0 0 3px;
    -moz-border-radius: 3px 0 0 3px;
    border-radius: 3px 0 0 3px;
}

.jd-toolbar-tab .tab-ico {
    width: 34px;
    height: 35px;
    margin-left: 1px;
    position: relative;
    z-index: 2;
    background-color: #7a6e6e;
    _display: block;
}

.jd-toolbar-tab .tab-ico {
    display: inline-block;
    background-image: url("../img/toolbars1.png");
    background-repeat: no-repeat;
}

.jd-toolbar-tab .tab-text {
    width: 62px;
    height: 35px;
    line-height: 35px;
    color: rgb(255, 255, 255);
    text-align: center;
    font-family: "微软雅黑";
    position: absolute;
    z-index: 1;
    left: 35px;
    top: 0px;
    background-color: rgb(122, 110, 110);
    border-radius: 3px 0px 0px 3px;
    /*移出动画效果*/
    transition: left 0.3s ease-in-out 0.1s;
}

.jd-toolbar-tab-hover {
    background-color: #c81623;
}

.jd-toolbar-tab-hover .tab-ico {
    background-color: #c81623;
}

.jd-toolbar-tab-hover .tab-text {
    left: -60px;
    background: #c81623;
}

.jd-toolbar-tab-hover .tab-texts {
    left: -108px;
    background: #c81623;
}
/* 根据自己的背景图,修改背景位置。*/
.jd-tab-vip .tab-ico {
    background-position: -2px -45px;
}

.jd-tab-follow .tab-ico {
    background-position: -3px -86px;
}

.jd-tab-top .tab-ico {
    background-position: -4px -170px;
}

.jd-tab-vip img {
    margin-top: 1px;
}
JS
返回顶部添加显示隐藏功能,可根据需求更改显示隐藏效果。
$(function(){

//右侧固定栏
    var $jdToolbar = $(".jd-global-toolbar .jd-toolbar-tab");
    $jdToolbar.hover(function(){
        //鼠标移入添加class
        $(this).addClass("jd-toolbar-tab-hover");
    },function(){
        //鼠标移除如果含有class,则移除
        if($(this).hasClass("jd-toolbar-tab-hover")){
            $(this).removeClass("jd-toolbar-tab-hover");
        }
    });

//返回顶部在一定高度内隐藏
    $(window).scroll(function(){
        var docHe= $(window).scrollTop();
        var hideH = 600;
        if (docHe < hideH){
            $("#returnTop").slideUp(1000);
        }else{
            $("#returnTop").slideDown(1000);
        }
    })


//右侧固定栏,返回顶部
    $("#returnTop").click(function () {
        var speed=200;//滑动的速度
        //添加返回顶部的动画效果
        $('body,html').animate({ scrollTop: 0 }, speed);
        return false;
    });

})

如有问题,欢迎大家交流指正。QQ:1357912285

猜你喜欢

转载自www.cnblogs.com/baimeishaoxia/p/12812584.html