Home > Article > Web Front-end > JavaScript imitates Taobao return to top effect (code example)
What this article brings to you is the JavaScript imitation Taobao return to the top effect (code example). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Requirement: When the scroll bar reaches a certain position, the sidebar is fixed at a certain position, and when it slides down to a certain position, the back to top button is displayed. . After clicking the button, the page will dynamically slide to the top, sliding upwards from fast to slow.
Ideas:
1. The js code can only be executed after the page is loaded.
You can add js The code is written at the bottom (this is the way to go back to the top example)
If you want to write it at the top, you can use the following two:
①window.onload = function() {...}
②window.addEventListener('load', function() {...})
2. Get the elements you need to use
3. Bind the scroll event scroll
When the user scrolls to the banner module Make the sidebar fixed Event click
After clicking the return to top button, the page will dynamically slide to the top, sliding upwards from fast to slow
if(window.pageYOffset >= bannerTop) { // window.pageYOffset 屏幕被滚上去的距离 sliderbar.style.position = 'fixed'; // 当用户滚到banner模块时使侧边栏变为固定状态 sliderbar.style.top = sliderbarTop + 'px'; } else { sliderbar.style.position = 'absolute'; sliderbar.style.top = '300px'; }
5. About animation Function animate(obj, target, callback)
The obj object here is window; the target target position is 0; callback is the callback function. If no parameters are passed, it can be ignored.
Set a timer setInterval();
Declare a step as the step value, the value is from the top position to the current scroll bar position The difference is divided by 10 (the step will become smaller and smaller, and the scrolling speed will become slower and slower, realizing the speed of the scroll bar sliding up from fast to slow)
if(window.pageYOffset >= mainTop) { // 当用户滚到main模块时显示返回顶部按钮 goBack.style.display = 'block'; } else { goBack.style.display = 'none'; }
However, step It is not always an integer. When step is not an integer, you can make the scroll bar move forward a little. The scroll bar can slide up and down, so step may be greater than zero or less than zero. If it is greater than zero, round up, if it is less than zero, round it down.
sliderbar.addEventListener('click', function() { animate(window, 0); })
window.scroll(x, y) Scroll to a specific position in the document,
The timer is called each time The function will slide up a littlevar step = (target - window.pageYOffset) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
Details The code is as follows:
window.scroll(0, window.pageYOffset + step);
More cool javascript special effects codes are available in: js special effects collection
The above is the detailed content of JavaScript imitates Taobao return to top effect (code example). For more information, please follow other related articles on the PHP Chinese website!