简体   繁体   English

Vue - 转换期间文本溢出 div

[英]Vue - Text overflowing out of div during transition

I have created a transition in Vue, one DIV moves left to right, whilst the other, next to it, expands to fill the page.我在 Vue 中创建了一个转换,一个 DIV 从左向右移动,而另一个在它旁边展开以填充页面。

When the transition is toggled, when the DIV moving left to right, out of the page I am having an issue where its jerky and the text in the DIV overflowing out of the side in view.当切换转换时,当 DIV 从左到右移动时,我遇到了一个问题,即它的生涩和 DIV 中的文本从视图中溢出。

Have a look at the Codepen here for an example: https://codepen.io/BONDJAMES/pen/NWGBGez以 Codepen 为例: https://codepen.io/BONDJAMES/pen/NWGBGez

How do I remove the 'jerkiness' and stop the text overflowing out of the div when the transition is triggered?触发转换时,如何删除“抖动”并阻止文本溢出 div?

CSS CSS

.viewContainerLeft {
  float: left;
  height: 100%;
  padding: 10px;
}

#subContainerRight {
  height: unset;
  padding: 10px 15px 15px 15px;
}

.subContainer {
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1) !important;
  background-color: white !important;
  min-height: 200px;
  width: 100%
}

.toggleContainerLeftWidth_Full {
  /display: none;
  width: 50%;
  transition: 1s;
  background: blue
}

.toggleContainerLeftWidth_Half {
  width: 50%;
  transition: 1s;
  background: blue;
}

.toggleContainerRightWidth_Half {
  width: 50%;
  transition: 1s;
  background: red;


}

.toggleContainerRightWidth_Full {
  width: 100%;
  transition: 1s;
  background: red;
}

.viewContainerRight {
  float: right;
  padding: 10px;

}

.viewContainer {
  padding: 10px;
}

.viewBlocks {
  height: 100%;
  width: 100%;
  display: flex;
}


.verticalSlide-leave-active,
.verticalSlide-enter-active {
  transition: 1s;   
}
.verticalSlide-leave-to {
  transform: translate(-100%, 0);
  width: 0%

}
.verticalSlide-enter {
  transform: translate(-100%, 0);
  width: 0%
}

HTML HTML

<div id="app">

      <div class="viewBlocks">
        <transition name="verticalSlide">
          <template v-if="!MaxView">
            <div :class="{
                        toggleContainerLeftWidth_Half: !MaxView,
                        toggleContainerLeftWidth_Full: MaxView
                      }" class="viewContainerLeft">
              <div class="subContainer" >
                <div class="container">
                  Hide When Toggle
                </div>
              </div>
            </div>
          </template>
        </transition>

        <transition name="verticalSlide">
          <template>
            <div class="viewContainerRight" :class="{
                        toggleContainerRightWidth_Half: !MaxView,
                        toggleContainerRightWidth_Full: MaxView
                      }">
              <div id="subContainerRight" class="subContainer">
                <button @click="toggleBtn">Toggle</button>
                <div class="container">
                  Keep Open Test
                </div>
              </div>
            </div>
          </template>
        </transition>

      </div>
    </div>

VueJS VueJS

var app = new Vue({
  el: '#app',
  data: () => ({
    MaxView: false
  }),
  methods: {
    toggleBtn(){
      this.MaxView = !this.MaxView
    }
  }
})

To prevent the text being rendered outside of its container when the container is too small, simply use overflow:hidden :当容器太小时,要防止文本在其容器之外呈现,只需使用overflow:hidden

.toggleContainerLeftWidth_Half {
   overflow: hidden;
}

The "jerkiness" is caused by having padding or border values on the transitioned element. “抖动”是由过渡元素上的paddingborder值引起的。 You're trying to transition to a total width of 0 , but the total width is actually width + padding + border .您正在尝试过渡到0的总宽度,但总宽度实际上是width + padding + border So you'll get a jump after animation ends (when the element is removed from DOM).所以在 animation 结束后(当元素从 DOM 中删除时)你会得到一个跳跃。

The solution is to have a wrapper and move any border/padding values on inner element, so the outer one can smoothly transition to a total width of 0 by changing width .解决方案是有一个包装器并在内部元素上移动任何边框/填充值,因此外部元素可以通过更改width平滑过渡到总宽度0 Also, use overflow:hidden on the outer one.另外,在外部使用overflow:hidden

Here's a simplified version of what you have: https://codepen.io/andrei-gheorghiu/pen/WNQKoxQ这是您所拥有的简化版本: https://codepen.io/andrei-gheorghiu/pen/WNQKoxQ

A minor detail is I chose to switch from animating width to animating flex-grow as I find it cleaner.一个小细节是我选择从动画width切换到动画flex-grow ,因为我发现它更干净。 Another option is to animate flex-basis .另一种选择是动画flex-basis

The main point here being: when you use display:flex , width (or height for vertical) is only the starting value for flexbox calculation and it's overridden by flex-basis .这里的要点是:当您使用display:flex时, width (或垂直height )只是 flexbox 计算的起始值,它被flex-basis覆盖。 Hence, the element is not guaranteed to end up with a computed width value equal to the value specified by CSS width (unless you prevent flexbox from being a "flex-box" by telling it: don't grow, don't shrink. At which point... why use display:flex ? display:block already does that and it's the default value for <div> s).因此,不能保证元素最终的计算宽度值等于 CSS width指定的值(除非您通过告诉它:不要增长,不要收缩来防止 flexbox 成为“弹性盒” )。在这一点上......为什么使用display:flex ? display:block已经这样做了,它是<div> s的默认值)。

One last thing: I also added a min-width on the inner element to prevent text wrapping during the animation.最后一件事:我还在内部元素上添加了一个min-width ,以防止在 animation 期间文本换行。 Remove if not wanted.如果不需要,请删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM