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.