Totop标签如何实现

当页面中出现了像如下的totop标签,如何实现?要求:开始时是隐藏的,在页面拖拽到一定距离时才出现,totop的过程有动画的效果。

4.1首先,定义html

<spanclass="totop">

<ahref="#"><iclass="icon-chevron-up"></i></a>

</span>

 4.2然后,是css

.totop {

    position: fixed;

//为什么不用absolute?因为我们是以body为定位对象,根据浏览器窗口进行元素定位。

//absolute :  将对象从文档流中拖出,使用leftrighttopbottom等属性进行绝对

//定位。而其层叠通过css z-index属性定义。此时对象不具有边距,但仍有补白和边框

    bottom: 0px;

    right: 0px;

    z-index: 104400;

    background: #fa3031;

}

 

.totop a, .totopa:visited, .totop a:hover{

    display: block;

    width: 30px;

    height: 30px;

    color: #fff;

    text-align: center;

    line-height: 30px;

    text-decoration:none;

}

4.3 JavaScript代码

$(".totop").hide();

    $(function () {

        $(window).scroll(function () {

            if($(this).scrollTop()>300){

                $('.totop').slideDown();

            }else{

                $('.totop').slideUp();

            }

        });

       $('.totop a').click(function (event) {

           event.preventDefault();

          $('body,html').animate({scrollTop:0},500);

       })

    });

4.4最终效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值