Heim >Web-Frontend >js-Tutorial >jQuery implementiert die Funktion „Zurück zum Anfang'.
Dieses Mal werde ich Ihnen jQuery zur Implementierung der Funktion „Zurück nach oben“ vorstellen. Was sind die Vorsichtsmaßnahmen für jQuery, um die Funktion „Zurück nach oben“ zu implementieren?
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>回到顶部</title> <style type="text/css"> body{ width: 100%; height: 10000px; } #totop{ width: 50px; height: 50px; line-height: 50px; background: magenta; font-size: 20px; position: fixed; right: 50px; bottom: 50px; } </style> </head> <body> </body> <script src="js/jquery-1.8.3.min.js" ></script> <script> //写在common.js文件中用来调用即可 //1获取滚动条当前的位置 function getScrollTop() { var scrollTop = 0; if (document.documentElement && document.documentElement.scrollTop) { scrollTop = document.documentElement.scrollTop; } else if (document.body) { scrollTop = document.body.scrollTop; } return scrollTop; }; //2获取文档完整的高度 function getScrollHeight() { return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight); }; //3回到顶部 function toTop(n) { $(window).on('scroll', function() { //console.log(getScrollTop()+"!"+getScrollHeight()); if ($("#totop").size() > 0) { if (getScrollTop() < $(window).height() * n) { $("#totop").remove(); } } else { if (getScrollTop() >= $(window).height() * n) { $("body").after("<p id='totop'>totop</p>"); //插入了新标签 ,记得添加样式! $("#totop").on('click', function() { scroll(0,200); }); } } }); }; //xxx.js文件来执行 $(function(){ toTop(2); }) </script> </html>Ich glaube, dass Sie die Methode beherrschen, nachdem Sie den Fall in diesem Artikel gelesen haben. Weitere spannende Informationen finden Sie in anderen verwandten Artikeln auf der chinesischen PHP-Website! Empfohlene Lektüre:
Das obige ist der detaillierte Inhalt vonjQuery implementiert die Funktion „Zurück zum Anfang'.. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!