Home  >  Article  >  Web Front-end  >  Create a floating button that returns to the top of an html page

Create a floating button that returns to the top of an html page

王林
王林forward
2020-12-25 11:15:518136browse

Create a floating button that returns to the top of an html page

The specific code is as follows:

(Learning video sharing: html video tutorial)

CSS style code:

.back-to {
		    bottom: 55px;
		    overflow: hidden;
		    position: fixed;
		    right: 10px;
		    width: 110px;
		    z-index: 999;
		}
		.back-to .back-top {
		    background: url("./images/top.png") no-repeat scroll 0 0 transparent;
		    display: block;
		    float: right;
		    height: 50px;
		    margin-left: 10px;
		    outline: 0 none;
		    text-indent: -9999em;
		    width: 50px;
		}
		.back-to .back-top:hover {
		    background-position: -50px 0
		}

DIV:

<div style="display:none;" class="back-to" id="toolBackTop">
		<a title="返回顶部" onclick="window.scrollTo(0,0);return false;" href="#top" class="back-top"></a>
	</div>

Javascript code:

$(function () {
	        var bt = $(&#39;#toolBackTop&#39;);
	        var sw = $(document.body)[0].clientWidth;
	 
	        var limitsw = (sw - 840) / 2 - 80;  //距离右侧距离
	        if (limitsw > 0){
                limitsw = parseInt(limitsw);
                bt.css("right",limitsw/8);
	        }
	 
	        $(window).scroll(function() {
                var st = $(window).scrollTop();
                if(st > 30){
                        bt.show();
                }else{
                        bt.hide();
                }
	        });
	});

Premise: First introduce jQuery

<script type="text/javascript" src="/static/js/jquery.min.js"></script>

Effect display:

Create a floating button that returns to the top of an html page

Button image:

Create a floating button that returns to the top of an html page

Related recommendations: html tutorial

The above is the detailed content of Create a floating button that returns to the top of an html page. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete