This article mainly introduces how to use jquery code to achieve the effect of returning the web page to the top.
When we browse the pages of major websites, we must all have encountered it. When browsing a long page, when you scroll to the lower part, a button effect similar to the return to the top will appear.
This special effect of clicking back to the top can greatly improve the user experience. Then it is very simple to implement such a function.
Below we will use a simple code example to introduce you to the functional method of using jquery to realize clicking back to the top.
<!DOCTYPE html> <html> <head> <title>回到顶部特效</title> <meta charset="UTF-8"> <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <style> body { text-align: center; } h1 { width: 1200px; height: 1500px; background: #eee; margin: 30px auto; } a { text-decoration: none; color: #fff; } p#back { text-align: center; position: fixed; bottom: 100px; right: 60px; background: #000; border-radius: 3px; height: 50px; width: 80px; display: none; } </style> </head> <body> <h1>网页内容</h1> <a href=""><p id="back"><br> 返回顶部</p></a> </body> <script> // 文档就绪 $(function () { // 获取浏览器窗口 $(window).scroll(function () { // 获取浏览器滚动条距离顶部的高度 if ($(window).scrollTop() > 150) { // 设置按钮出现 $('#back').fadeIn(1000) } else { // 设置按钮消失 $('#back').fadeOut(1000) } }) }) </script> </html>
In this code, we mainly referenced a jquery online library, and wrote some simple HTML pages and js code to judge. When the height of the scroll bar from the top is greater than 150, the setting button appears. , otherwise the button is hidden. Finally access it through the browser, the effect is as follows:
Then we drag the scroll bar down, and the button to return to the top will slowly appear, the effect is as follows:
Note:
fadeIn() The method uses the fade-in effect to display the selected element if the element is hidden . (In this example, the parameter is set to 1000 milliseconds, which means that the fade-in transition time of the selected element is 1000 milliseconds. If it is set to 0, the selected element will appear immediately.)
fadeOut() Method uses the fade out effect to hide the selected element, if the element is hidden. (Parameters are the same as above)
This article is about the introduction of jquery to achieve the effect of returning the web page to the top. It is simple and easy to understand. I hope it will be helpful to friends in need!
If you want to know more about front-end related knowledge, you can follow PHP Chinese website jQuery video tutorial or HTML video tutorial, CSS video tutorial, Bootstrap video tutorial and other related video tutorials, welcome everyone to refer to and learn!
The above is the detailed content of How to achieve the effect of clicking on the web page to return to the top in jquery? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!