JavaScript simply returns to the top code and comments
In our daily development process, in order to provide users with a better experience, our pages will have a return to the top function. The most common one is using JavaScript to implement the return to the top code. Today Let me give you a detailed introduction to JavaScriptReturn to the top codeand comments!
I recently made a ranking page with many categories and displayed it in the form of pictures and texts... In the end, the page was obviously too long, and the user experience was naturally not good enough. Finally, I thought of adding an in-page jump navigation to the upper part of the page to navigate directly to the category naming anchor block of this page. Of course, this needs to be combined with the "return to top" function to achieve a better experience. Of course, under normal circumstances, you must first avoid pages that are too long. If the content is weak and not relevant enough, the bounce rate will be very high.
The following is a simple code implementation of the return to top effect, with comments.
1. The simplest static return to the top, click to jump directly to the top of the page, common in fixed placement at the bottom of the page return to the top function
Method 1: Use named anchors Click to return to the top element with the preset ID of top
html code
<a href="#top" target="_self">返回顶部</a>
Method 2:Operation scroll function is used to control the scroll bar position (the first parameter is the horizontal position, the second parameter is the vertical position)
html code
<a href="javascript:scroll(0,0)">返回顶部</a>
Disadvantages:Return effect It is immediate and does not conform to the scrolling feeling of general browsing pages;
is statically fixed at the bottom of the page and may not be visible to users.
2. Simple static return to the top, use js to simulate the scrolling effect and slide up to the top
js code
pageScroll(){ window.scrollBy(0,-100); scrolldelay = setTimeout('pageScroll()',100); sTop=document.documentElement.scrollTop+document.body.scrollTop; (sTop==0) clearTimeout(scrolldelay); }
html code
<a onclick="pageScroll()">返回顶部</a>
Disadvantages: The scrolling effect is not smooth, and when the page is very long, click to return to the top. If you do not reach the top of the page, you can no longer browse the page normally;
Same as above It is still statically fixed at the bottom of the page and may not be exposed to users.
3. Dynamic on-demand loading returns to the top, css side screenAbsolute positioning, combined with simple jQuery animation to achieve a better experience
js code
gotoTop(min_height){ gotoTop_html = '<p id="gotoTop">返回顶部</p>'; $("#page").append(gotoTop_html); $("#gotoTop").click( (){$('html,body').animate({scrollTop:0},700); }).hover( (){$().addClass("hover");}, (){$().removeClass("hover"); }); min_height ? min_height = min_height : min_height = 600; $(window).scroll((){ s = $(window).scrollTop(); ( s > min_height){ $("#gotoTop").fadeIn(100); }{ $("#gotoTop").fadeOut(200); }; }); }; gotoTop();
css style code
{:;:;:75;:;:;:;:;:;:;:;:;:;}{:;:}{:;:;:;}
This method determines the height of the page and displays "return to the top" to the user as needed. It uses css style to achieve absolute positioning of the screen. With the help of jQuery achieves better and smoother scrolling effect. Further consideration is that if the user sets the browser to disable js, then we can combine the third solution with the first solution. After disabling js, the third solution will not be visible to the user. If it is not disabled, we will add a sentence in the js code. Hide the first option.
Summary:
In short, long pages should be avoided as much as possible. When unavoidable, adding the "return to top" function may It will give users a relatively better experience. I hope it will be helpful to you!
Related recommendations:
The above is the detailed content of JavaScript simply returns to the top code and comments. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

The win10 system is a very excellent high-intelligence system. Its powerful intelligence can bring the best user experience to users. Under normal circumstances, users’ win10 system computers will not have any problems! However, it is inevitable that various faults will occur in excellent computers. Recently, friends have been reporting that their win10 systems have encountered frequent blue screens! Today, the editor will bring you solutions to different codes that cause frequent blue screens in Windows 10 computers. Let’s take a look. Solutions to frequent computer blue screens with different codes each time: causes of various fault codes and solution suggestions 1. Cause of 0×000000116 fault: It should be that the graphics card driver is incompatible. Solution: It is recommended to replace the original manufacturer's driver. 2,

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Termination Code 0xc000007b While using your computer, you sometimes encounter various problems and error codes. Among them, the termination code is the most disturbing, especially the termination code 0xc000007b. This code indicates that an application cannot start properly, causing inconvenience to the user. First, let’s understand the meaning of termination code 0xc000007b. This code is a Windows operating system error code that usually occurs when a 32-bit application tries to run on a 64-bit operating system. It means it should

What does the 0x000000d1 blue screen code mean? In recent years, with the popularization of computers and the rapid development of the Internet, the stability and security issues of the operating system have become increasingly prominent. A common problem is blue screen errors, code 0x000000d1 is one of them. A blue screen error, or "Blue Screen of Death," is a condition that occurs when a computer experiences a severe system failure. When the system cannot recover from the error, the Windows operating system displays a blue screen with the error code on the screen. These error codes

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

Quickly get started with Python drawing: code example for drawing Bingdundun Python is an easy-to-learn and powerful programming language. By using Python's drawing library, we can easily realize various drawing needs. In this article, we will use Python's drawing library matplotlib to draw a simple graph of ice. Bingdundun is a cute panda who is very popular among children. First, we need to install the matplotlib library. You can do this by running in the terminal

As a programmer, I get excited about tools that simplify the coding experience. With the help of artificial intelligence tools, we can generate demo code and make necessary modifications as per the requirement. The newly introduced Copilot tool in Visual Studio Code allows us to create AI-generated code with natural language chat interactions. By explaining functionality, we can better understand the meaning of existing code. How to use Copilot to generate code? To get started, we first need to get the latest PowerPlatformTools extension. To achieve this, you need to go to the extension page, search for "PowerPlatformTool" and click the Install button
