Simple scroll to top example



Smooth scroll to top using jQuery




  • In this example, I used jQuery to implement the smooth scrolling effect.

  • For this, no plugin is used.

  • Tooltip visible after scrolling 50 pixels.

  • When user clicks on the below button, scroll to the top of a page (0 pixels).

  • It shows your "scroll to top" element(tooltip) only after the user has scrolled down the page 50 percent

  • 800 is delay. It is scroll duration. Can be set to any number as needed.

 

 

 

Download

Demo

Example:

 

<!DOCTYPE html>
<html> 
 
 
 <title>e-Trainings demo : scroll to top of the page</title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> 
        

<!-- css for tooltip -->
<style type="text/css">

.back-to-top {
cursor: pointer;
position: fixed;
bottom: 20px;
right: 20px;
display:none;
}
</style>

<!--SMOOTH SCROLL -->
<script>
$(document).ready(function(){
$(window).scroll(function () {
    if ($(this).scrollTop() > 50) {
        $('#back-to-top').fadeIn();
    } else {
        $('#back-to-top').fadeOut();
    }
});
// scroll body to 0px on click
$('#back-to-top').click(function () {
    $('#back-to-top').tooltip('hide');
    $('body,html').animate({
        scrollTop: 0
    }, 800);
    return false;
});

$('#back-to-top').tooltip('show');

});
</script>
        

</head>
<body>

<div class="container-fluid">
<div name="top"> </div>  

Click the Scroll Top Button below to see the effect!        
<div style="background-color:skyblue;padding:30px 30px 2500px">
<h3>This example demonstrates how scrolltotop button works..</h3>

<strong>When the user clicks  it. </strong>. 
In place of this Div block, you can have any content.</div>
<div class="row">
<div class="col-md-12 bg-light text-right"> 
    
<a id="back-to-top" href="#" class="btn btn-danger btn-lg back-to-top" role="button" 
title="Click to return on the top page" data-toggle="tooltip" data-placement="left">
<span class="glyphicon glyphicon-chevron-up"></span></a>
    
</div> 
</div> 
 </body>
</html>

 

 

coding-zon-jquery-scroll-to-top

 



 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP