React operation real DOM Implementation Dynamic bottom suction example, reactdom

Source: Internet
Author: User

React operation real DOM Implementation Dynamic bottom suction example, reactdom

Dynamic bottom suction: fixed is displayed on the page at the beginning. When the page is scrolled to a certain distance from the bottom, the fixed part is fixed.

This requires Page scrolling distance calculation. If Jquery or native js is used for implementation, the use of react does not recommend DOM operations, however, if you use virtual DOM, this effect cannot be achieved. Therefore, you need to introduce JavaScript to directly obtain the DOM for operations.

React finishes page rendering after componentDidMount. Therefore, you can use the native method of js to obtain DOM elements for further operations.

ComponentDidMount () {this. changeFixed ()} // calculates the height of changeFixed () {// getDOMNode const layoutNode = document. querySelectorAll ('. page-layout ') [0]; const orderPriceNode = document. querySelectorAll ('. test-price') [0]; window. addEventListener ('scroll ', function (e) {const 1_winnerheight = window. innerHeight; const layoutNodeHeight = layoutNode. offsetHeight; // scroll beyond the field of view. let scrollTop = window. pageYOffset | document.doc umentElement. scrollTop | document. body. scrollTop; const distanceBottom = layoutNodeHeight-scrollTop-bottom winnerheight; // if (distanceBottom <= 120) {orderPriceNode. classList. remove ('fixed');} else {orderPriceNode. classList. add ('fixed ');}})}

In this way, when the distance is 120 from the bottom, the bottom is sucked.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.