HTML学习笔记之二(回到顶部 与 回到底部)

回到顶部 回到底部

回到顶部的俩种方式

 一、使用js

      

 $('html, body').animate({ scrollTop: 0 }, 'fast');//带动画
        $('html,body').scrollTop(0); //不带动画

     
  $(window).scroll(function () {
            //You've scrolled this much:
               $('p').text("You've scrolled " + $(window).scrollTop() + " pixels");
        });



二、使用 a 标签的name属性

          

 <a name="top">top</a>
            <a href="#top">Click here go back to the top.</a>



三、获取高度


 1. 整个文档高度

      

 var body = document.body,
            html = document.documentElement;

        var height = Math.max( body.scrollHeight, body.offsetHeight,
                       html.clientHeight, html.scrollHeight, html.offsetHeight );

       // 或者
        var height = $(document).height();



 2. 当前屏幕高度

       

var wheight = $(window).height();


HTML代码

<!-- 侧边栏 按钮-->
        <div id="back-top">
		  <button class="styled-button">TOP</button>
		</div>
		<div id="back-end">
		  <button class="styled-button">TOP</button>
		</div>
		<!--底部 内容-->
        <div id="footer"></div>


js代码

jQuery(document).ready(function($){
    /**
     * 回到顶部
     */
    $('#back-top').click(function(){
        $('html,body').stop();
        $('html,body').animate({
            scrollTop:'0px'
        },1000);
    });
    
    /**
     * 回到底部
     */
    $('#back-end').click(function(){
        $('html,body').stop();
        $('html,body').animate({
            scrollTop:$('#footer').offset().top
        },1000);
    });
});

//回到顶部的 显示 隐藏代码
		   $(document).ready(function(){

			  // hide #back-top first
			  $("#back-top").hide();

			  // fade in #back-top
			  $(function () {
			    $(window).scroll(function () {
			      if ($(this).scrollTop() > 100) {
			        $('#back-top').fadeIn();
			      } else {
			        $('#back-top').fadeOut();
			      }
			    });

			    // scroll body to 0px on click
			    $('#back-top').click(function () {
			      $('body,html').animate({
			        scrollTop: 0
			      }, 'fast');
			      return false;
			    });
			  });

			});



css代码

#back-top{position: fixed; bottom:20px; right: 2%; z-index: 100; }



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值