神刀安全网

Page Scroll To Top Using jQuery

Tags:-jQuery HTML CSS

In this tutorial we will show you how to scroll the page to the top using jQuery with animation effects whenever user clicks on scroll to top button the page scrolls to its top position with nice and simple jQuery animation.You may also like Create Your Own Custom Scrollbars Using CSS And HTML

See Demo

Page Scroll To Top Using jQuery

To Scroll Page To Top Using jQuery It Takes Only Three Steps:-

  1. Make a HTML file and define markup
  2. Make a js file and define scripting
  3. Make a CSS file and define styling

Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name scroll.html

<html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="scroll_script.js"></script> <link rel="stylesheet" type="text/css" href="scroll_style.css"> </head> <body> <div id="wrapper">  <p>   This is some sample content  </p>  <br>  <p>   This is some sample content  </p>  <br>  <input type="button" value="Scroll Top" id="scrollToTop" onclick="scrolltop();"> </div>  </body> </html>

In this step we create some sample content and create a button which is used to scroll the page whenever user clicks on that.

Step 2.Make a js file and define scripting

We make a js file and save it with a name scroll_script.js

$(document).ready(function(){  $(window).scroll(function(){  if ($(window).scrollTop() > 100) {   $('#scrollToTop').fadeIn();  } else {   $('#scrollToTop').fadeOut();  }  }); });  function scrolltop() {  $('html, body').animate({scrollTop : 0},500); }

In this step we use document.ready function to show and hide the button with jQuery fade effect when the difference between scrollbar and window is greater than 100 and then we create scrolltop function which is used to scroll the page to top in this function we use jQuery animate function to scroll.

Step 3.Make a CSS file and define styling

We make a CSS file and save it with a name scroll_style.css

body {  background-color:#F2F2F2;  font-family:helvetica; } #wrapper {  text-align:center;  margin:0px auto;  padding:0px;  width:700px; } #scrollToTop {  position:fixed;  left:45%;  bottom:50px;  border:3px solid #585858;  background-color:white;  color:#585858;  border-radius:100%;  height:90px;  width:90px;  font-size:15px;  display:none; }

Thats all, this is how to scroll page to top using jQuery.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Page Scroll To Top Using jQuery

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址