在移动设备上调整窗口大小时,Flexbox中的奇怪行为

时间:2017-06-04 02:01:40

标签: javascript css mobile flexbox vue.js

在移动浏览器上发生Flexbox有一种非常奇怪的行为。

Please see this gif for a visual of what's happening

这只发生在移动浏览器上,我目前在Google Pixel手机上使用Chrome手机。我有一个运行调整大小的脚本,它调整了页面上的多个元素的大小,作为一种解决方法,因为无法像这样正确使用100vh:

window.onresize = function() {
        document.getElementById("backgroundImage").style.height = window.innerHeight;
        document.getElementById("mainScreen").style.height = window.innerHeight;
        if (window.innerWidth > 750) {
          document.getElementById("scrollContent").style.height = window.innerHeight - "96";
        } else {
          document.getElementById("scrollContent").style.height = window.innerHeight - "72";
        }
    };

在gif中看到的正在生效的项目是flex容器中的项目。当我向上滚动以隐藏导航栏,然后单击其中一个弹性链接时,会发生此行为。

我正在使用Vuejs,并且通过检查道具更改来填充以下内容。 flex链接将字符串传递给将项目视图更改为所选项目的函数。

我还有一个脚本来添加一个更改所选链接背景颜色的类,并在所有其他链接上删除该类,这就是我改变活动链接背景的方式。

var webComp = Vue.component('design-comp', {
  template: "#webComp",
  props:['trans-state'],
  components: {
      'project-comp': projectComp,
  },
  data: function() {
    return {
      activeProj: 'default',
    }
  },
  methods: {
    changeProj: function(proj) {
      this.activeProj = proj;
      var a = document.getElementsByClassName('projClick');
      for (i=0; i<a.length; i++) {
        a[i].classList.remove('projActive');
      }
      event.currentTarget.classList.add('projActive');
      document.getElementById('webContainer').scrollTop = 0;
    }
  },
  created: function() {
    this.activeProj = "default";
  }
});

以下是我的SASS文件的相关部分:

#webContainer {
  margin-top: 50px;
  height: calc(100% - 50px);
  .projHeader {
    display: flex;
    justify-content: center;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    background-color: $headerColor;
    box-sizing: border-box;

    .projClick {
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      height: 50px;
      box-sizing: border-box;
      background-color: $contentColor;
      border-right: 2px solid $headerColor;
      border-left: 2px solid $headerColor;
      border-bottom: 4px solid $headerColor;
      border-top: 4px solid $headerColor;
      flex-basis: 50px;
      transition: background-color 0.3s;

      &.projActive {
        background-color: $linkActiveColor;
      }

      p {
        color: $fontColor;
        font-family: $titleFont;
        font-size: 2em;
      }
    }
  }
}

以下是相关的HTML:

<div class="viewContainer" id="webContainer">
        <div class="transitionBox"></div>
        <div class="stickyImageBottomLeft"><img src="./img/AfricaMountainPixelR.gif" /></div>
        <div class="stickyImageBottomRight"><img src="./img/AfricaMountainPixel.gif" /></div>
        <div class="projHeader">
            <div class="projClick projActive" v-on:click="changeProj('default')">
                <p>0</p>
            </div>
            <div class="projClick" v-on:click="changeProj('kyount')">
                <p>1</p>
            </div>
            <div class="projClick" v-on:click="changeProj('prodigal')">
                <p>2</p>
            </div>
            <div class="projClick" v-on:click="changeProj('joy')">
                <p>3</p>
            </div>
            <div class="projClick" v-on:click="changeProj('cgl')">
                <p>4</p>
            </div>
            <div class="projClick" v-on:click="changeProj('mikeg')">
                <p>5</p>
            </div>
            <div class="projClick" v-on:click="changeProj('reddit')">
                <p>6</p>
            </div>
            <div class="projClick" v-on:click="changeProj('westbeach')">
                <p>7</p>
            </div>
        </div>
        <div class="contentContainer">
        Project Page is loaded here
        </div>
</div>

我希望有人可能知道发生了什么事,因为我真的没有看到发生这种情况的原因。

2 个答案:

答案 0 :(得分:1)

试试这个:

#webContainer {
  margin: 0;
  padding-top: 50px;
  height: 100%;
  box-sizing: border-box; /* if you didn't apply it before */
...

答案 1 :(得分:0)

在Kosh Very的帮助下,我能够找到一个完美的解决方案,同时仍然保持元素的预期风格。

#webContainer {
  margin-top: 50px;
  box-sizing: border-box;
  position: relative;
  height: 100%;

    .projHeader {
        ...
        position: fixed;
        ...
    }
}

我仍然觉得最初的问题是某种错误,但这至少是一种解决方法。

相关问题