Home > Web Front-end > JS Tutorial > JavaScript imitates Taobao return to top effect (code example)

JavaScript imitates Taobao return to top effect (code example)

青灯夜游
Release: 2019-11-30 17:10:37
forward
2730 people have browsed it

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.

JavaScript imitates Taobao return to top effect (code example)

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';
}
Copy after login

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';
 }
Copy after login

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);
 })
Copy after login

window.scroll(x, y) Scroll to a specific position in the document,

The timer is called each time The function will slide up a little

var step = (target - window.pageYOffset) / 10;
Copy after login

Determine whether the animation is completed, and if it is completed, close the timer clearInterval();

step = step > 0 ? Math.ceil(step) : Math.floor(step);
Copy after login
Details The code is as follows:

window.scroll(0, window.pageYOffset + step);
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template