Home > Web Front-end > HTML Tutorial > HTML Study Notes 2 (Back to Top and Back to Bottom)_html/css_WEB-ITnose

HTML Study Notes 2 (Back to Top and Back to Bottom)_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:43:20
Original
1619 people have browsed it

Back to the top Back to the bottom

Two ways to go back to the top

1. Use js

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

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



2. Use the name attribute of the a tag

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


3. Get the height

1. The height of the entire document


 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();
Copy after login


2. Current screen height

var wheight = $(window).height();
Copy after login

HTML code

<!-- 侧边栏 按钮-->        <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>
Copy after login

js code

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);    });});
Copy after login

//回到顶部的 显示 隐藏代码		   $(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;			    });			  });			});
Copy after login


css code


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


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template