Vue.js动态获取浏览器高度、宽度并进行实时修改DOM元素高度

た 入场券 2021-07-26 13:46 1170阅读 0赞

需求:项目中有高度、宽度自适应需求,需要适应不同分辨率的高度及宽度,在不同分辨率下效果区别不会很大

html代码如下:

  1. <template>
  2. <div id="home">
  3. <div class="head" >
  4. <v-head></v-head>
  5. </div>
  6. <div class="content" :style="GetWindowInfo" >
  7. <router-view></router-view>
  8. </div>
  9. </div>
  10. </div>

js代码如下:

  1. <script>
  2. export default {
  3. data () {
  4.   return {
  5. GetWindowInfo:{
  6. height:'', //动态获取content高度
  7. width:'' //动态获取content宽度
  8. },
  9.   }
  10. },
  11. methods:{
  12. GetWindowInfo(){
  13. // 获取浏览器高度,减去顶部导航栏的值70(可动态获取),再减去head-DIV高度值80
  14. this.GetWindowInfo.height=window.innerHeight-70-80+'px';
  15. this.GetWindowInfo.width=window.innerWidth+'px';
  16. }
  17. },
  18. created(){
  19. window.addEventListener('resize', this.GetWindowInfo); //注册监听器
  20. this.GetWindowInfo() //页面创建时先调用一次
  21. },
  22. destroyed(){
  23. window.removeEventListener('resize', this.GetWindowInfo)
  24. }
  25. }
  26. </script>

css代码:

  1. <style scoped> .home{
  2. overflow:scroll;
  3. text-align: center;
  4. background: rgb(255, 255, 255);
  5. }
  6. /*头DIV*/
  7. .head{
  8. background: #ffffff;
  9. margin:auto;
  10. width:1000px;
  11. height:80px;
  12. }
  13. /*内容DIV*/
  14. .content{
  15. overflow:scroll;
  16. overflow-x:hidden;
  17. margin: 0 auto;
  18. text-align: center;
  19. background: #ffffff;
  20. }
  21. </style>

发表评论

表情:
评论列表 (有 0 条评论,1170人围观)

还没有评论,来说两句吧...

相关阅读