React操作真實DOM實現動態吸底部的樣本,reactdom

來源:互聯網
上載者:User

React操作真實DOM實現動態吸底部的樣本,reactdom

動態吸底:開始時fixed在頁面上,當頁面滾動到距離底部一定距離的時fixed部分固定。

這個是需要計算頁面滾動距離的,如果使用Jquery或者原生js實現是非常好實現的,但是由於使用react並不推崇操作DOM,但是如果使用virtual DOM的話是無法實現該效果的,所以還是要引入js去直接擷取DOM進行操作。

react在componentDidMount之後頁面渲染完成,所以可以在這裡面直接用js原生方法擷取DOM元素,進而進行操作。

componentDidMount() { this.changeFixed()}//計算高度changeFixed(){ //getDOMNode const layoutNode = document.querySelectorAll('.page-layout')[0]; const orderPriceNode = document.querySelectorAll('.test-price')[0]; window.addEventListener('scroll', function (e) {  const windowInnerHeight = window.innerHeight;  const layoutNodeHeight = layoutNode.offsetHeight;  //滾動超出視野距離  let scrollTop = window.pageYOffset|| document.documentElement.scrollTop || document.body.scrollTop;  const distanceBottom = layoutNodeHeight - scrollTop - windowInnerHeight;  //120的時候吸底  if(distanceBottom <= 120){   orderPriceNode.classList.remove('fixed');  }else{   orderPriceNode.classList.add('fixed');  } })}

這樣就實現了當距離底部120的時候吸底

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.