近期在开发设计网站必须制做回到顶部按钮,可是我关键为后端开发开发设计,对前端开发不太娴熟,历经在网上材料查寻,制做出了回到顶部的按钮,下面是两种简易的方法,纪录1下.喜爱本网站的盆友能够个人收藏下,会不确定期升级学习培训材料.
第1种:引入外界jQuery
新建HTML网页页面,将下面编码拷贝储存,根据访问器开启,便可看到实际效果.
<!doctype html> <html lang="en"> <head> <meta charset="UTF⑻"> <title>doc</title> <style> .arrow{ border: 9px solid transparent; border-bottom-color: #三dA0DB; width: 0px; height: 0px; top:0px } .stick{ width: 8px; height: 14px; border-radius: 1px; background-color: #三dA0DB; top:15px; } #back_top div{ position: absolute; margin: auto; right: 0px; left: 0px; } #back_top{ background-color: #dddddd; height: 38px; width: 38px; border-radius: 3px; display: block; cursor: pointer; position: fixed; right: 50px; bottom: 100px; display: none; } </style> </head> <body> <div id="article"></div> <div id="back_top"> <div class="arrow"></div> <div class="stick"></div> </div> <script src="http://cdn.staticfile.org/jquery/1.11.1-rc2/jquery.min.js"></script> <script> $(function(){ for(var i =0 ;i <100;i++){ $("#article").append("<p>xxxxxxxxxx<br></p>") } }) </script> <script> $(function(){ $(window).scroll(function(){ //要是对话框翻转,就开启下面编码 var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //获得翻转后的高宽比 if( scrollt >200 ){ //分辨翻转后高宽比超出200px,就显示信息 $("#back_top").fadeIn(400); //淡入 }else{ $("#back_top").stop().fadeOut(400); //假如回到或沒有超出,就淡出.务必再加stop()终止以前动漫,不然会出現闪烁 } }); $("#back_top").click(function(){ //当点一下标识的情况下,应用animate在200毫秒的時间内,滚到顶部 $("html,body").animate({scrollTop:"0px"},200); }); }); </script> </body> </html>
第2种:应用css及独特标志开展设定
全编码打造简约美观大方返回顶部按钮,同上,将编码拷贝进HTML文档中,开启便可看到实际效果.
<!doctype html> <html lang="en"> <head> <meta charset="UTF⑻"> <title>doc</title> <style> #back_top{ display:block; width:60px; height:60px; position:fixed; bottom:50px; right:40px; border-radius:10px 10px 10px 10px; text-decoration:none; display:none; background-color:#999999; } #back_top span{ display:block; width:60px; color:#dddddd; font-size:40px; text-align:center; margin-top:4px; } #back_top span:hover{ color:#cccccc; } </style> </head> <body> <div id="article"></div> <a id="back_top" href="script:;"> <span>⌆</span> </a> </div> <script> $(function(){ for(var i =0 ;i <100;i++){ $("#article").append("<p>xxxxxxxxxx<br></p>") } }) </script> <script> $(function(){ $(window).scroll(function(){ //要是对话框翻转,就开启下面编码 var scrollt = document.documentElement.scrollTop + document.body.scrollTop; //获得翻转后的高宽比 if( scrollt >200 ){ //分辨翻转后高宽比超出200px,就显示信息 $("#back_top").fadeIn(400); //淡出 }else{ $("#back_top").stop().fadeOut(400); //假如回到或沒有超出,就淡入.务必再加stop()终止以前动漫,不然会出現闪烁 } }); $("#back_top").click(function(){ //当点一下标识的情况下,应用animate在200毫秒的時间内,滚到顶部 $("html,body").animate({scrollTop:"0px"},200); }); }); </script> </body> </html>
以上两种方法仅出示思路,立即应用也可,实际要想的标志可自身调节,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。