【问题标题】:CSS scale and transform making text bluryCSS 缩放和变换使文本模糊
【发布时间】:2019-12-24 09:03:30
【问题描述】:

我有一些翻转卡片,当您将鼠标悬停在翻转卡片上时,卡片会翻转并放大(缩放)。但是我不想将悬停卡片内的文本缩放,所以我将文本放在子 div 中。 (我只希望父卡背景缩放)。

如何阻止我的文本和内容缩放和变得模糊?我尝试将我的子 div 重置为比例大小的 1/2 以重置它,但它不起作用。

HTML

<div class="c-flip-card">
  <div class="c-flip-card__inner">
    <div class="c-flip-card__front"><img src="img_avatar.png" alt="Avatar"</div>
     <div class="c-flip-card__back">
        <h1>John Doe</h1> 
        <p>Architect & Engineer</p> 
        <p>We love that guy</p>
     </div>
   </div>
</div>

SASS

.c-flip-card {
    background-color: transparent;
    width: 180px;
    height: 200px;
    border: 1px solid #f1f1f1;
    perspective: 1000px;
    /* Do an horizontal flip when you move the mouse over the flip box container */
    &:hover {
        z-index: 10;
        position: relative;
    }
    &:hover .c-flip-card__inner {
        transform: rotateY(180deg) scale(1.4);
    }
    /* This container is needed to position the front and back side */
    .c-flip-card__inner {
        cursor:pointer;
        position: relative;
        width: 100%;
        height: 100%;
        text-align: center;
        transition: transform 0.8s;
        transform-style: preserve-3d;
    }
    /* Position the front and back side */
    .c-flip-card__front, .c-flip-card__back {
        position: absolute;
        width: 100%;
        height: 100%;
        backface-visibility: hidden;
    }
    /* Style the front side (fallback if image is missing) */
    .c-flip-card__front {
        background-color: #bbb;
        color: black;
    }
    /* Style the back side */
    .c-flip-card__back {
        background-color: red;
        color: white;
        transform: rotateY(180deg);
    }
    .abc{
        transform: scale(0.7);
        h1, p{
            color:#000;
        }
    }
}

jsfiddle

【问题讨论】:

标签: html css css-transitions


【解决方案1】:

编辑

我想我找到了解决方法:不要使用scale()

.c-flip-card .c-flip-card__inner {
  transition: all 0.8s;
}

.c-flip-card:hover .c-flip-card__inner {
  transform: rotateY(180deg) translate3d(40px,-40px,0);
  /* Just change the width and height to 140% and have them transition
     I used translate3d to position the card the same way scale() does */
  width: 140%;
  height: 140%;
}

如果您想获得与 scale() 产生的相同定位,您可以将 translate3d(40px,-40px,0) 之类的内容添加到您的转换中。

这样就不用管理文字的大小,效果一样(除了文字位置有一点过渡),也没有模糊。

.c-flip-card {
  margin: 50px;
  background-color: transparent;
  width: 180px;
  height: 200px;
  border: 1px solid #f1f1f1;
  perspective: 1000px;
}

.c-flip-card:hover {
  z-index: 10;
  position: relative;
}

.c-flip-card:hover .c-flip-card__inner {
  transform: rotateY(180deg) translate3d(40px,-40px,0);
  width: 140%;
  height: 140%;
}

.c-flip-card .c-flip-card__inner {
  cursor: pointer;
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: all 0.8s;
  transform-style: preserve-3d;
}

.c-flip-card .c-flip-card__front,
.c-flip-card .c-flip-card__back {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
}

.c-flip-card .c-flip-card__front {
  background-color: #bbb;
  color: black;
}

.c-flip-card .c-flip-card__back {
  background-color: red;
  color: white;
  transform: rotateY(180deg);
}
<div class="c-flip-card">
  <div class="c-flip-card__inner">
    <div class="c-flip-card__front">
      <img src="img_avatar.png" alt="Avatar" style="width:300px;height:300px;">
    </div>
    <div class="c-flip-card__back">
      <h1>John Doe</h1>
      <p>Architect & Engineer</p>
      <p>We love that guy</p>
    </div>
  </div>
</div>

原创

将背面卡片的内容放入div 并在其上应用scale(0.7) 似乎可行,但在第一次悬停时看起来确实不太好。

    <div class="c-flip-card__back">
      <div class="notScale">
        <h1>John Doe</h1>
        <p>Architect & Engineer</p>
        <p>We love that guy</p>
      </div>
    </div>

    .c-flip-card:hover .notScale {
      transform: scale(0.7);
    }

因此,与其对文本应用变换,因为它只在悬停时显示,为什么不简单地使用 calc() 和变量来更改字体大小:

/* define variables for p and h1 */
body {
  --sizeP : 16px;
  --sizeH1 : calc(2.5 * var(--sizeP));  
}

/* apply those variable one "regular" h1 and p */
h1 {
  font-size: var(--sizeH1);
}

p{
  font-size: var(--sizeP);
}

/* "scale" down the text for the backside by a factor of .7 */
.c-flip-card .c-flip-card__back h1{
  font-size: calc(.7 * var(--sizeH1));
}

.c-flip-card .c-flip-card__back p{
  font-size: calc(.7 * var(--sizeP));
}

对于模糊的部分,我确实通过添加:

.c-flip-card {
  -webkit-filter: blur(0);
  filter: blur(0); 
}

body {
  --sizeP: 16px;
  --sizeH1: calc(2.5 * var(--sizeP));
}

h1 {
  font-size: var(--sizeH1);
}

p {
  font-size: var(--sizeP);
}


/* The flip card container - set the width and height to whatever you want. We have added the border property to demonstrate that the flip itself goes out of the box on hover (remove perspective if you don't want the 3D effect */

.c-flip-card {
  margin: 50px;
  background-color: transparent;
  width: 180px;
  height: 200px;
  border: 1px solid #f1f1f1;
  perspective: 1000px;
  /* Do an horizontal flip when you move the mouse over the flip box container */
  /* This container is needed to position the front and back side */
  /* Position the front and back side */
  /* Style the front side (fallback if image is missing) */
  /* Style the back side */

  -webkit-filter: blur(0);
  filter: blur(0); 
}

.c-flip-card:hover {
  z-index: 10;
  position: relative;
}

.c-flip-card:hover .c-flip-card__inner {
  transform: rotateY(180deg) scale(1.39);
}

.c-flip-card .c-flip-card__inner {
  cursor: pointer;
  position: relative;
  width: 100%;
  height: 100%;
  text-align: center;
  transition: transform 0.8s;
  transform-style: preserve-3d;
}

.c-flip-card .c-flip-card__front,
.c-flip-card .c-flip-card__back {
  position: absolute;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
}

.c-flip-card .c-flip-card__front {
  background-color: #bbb;
  color: black;
}

.c-flip-card .c-flip-card__back {
  background-color: red;
  color: white;
  transform: rotateY(180deg);
}

.c-flip-card .c-flip-card__back h1 {
  font-size: calc(.7 * var(--sizeH1));
}

.c-flip-card .c-flip-card__back p {
  font-size: calc(.7 * var(--sizeP));
}
<div class="c-flip-card">
  <div class="c-flip-card__inner">
    <div class="c-flip-card__front">
      <img src="img_avatar.png" alt="Avatar" style="width:300px;height:300px;">
    </div>
    <div class="c-flip-card__back">
      <h1>John Doe</h1>
      <p>Architect & Engineer</p>
      <p>We love that guy</p>
    </div>
  </div>
</div>

对于 chrome,我尝试了 herehere 的所有解决方案,但无法获得下降结果。
唯一适用于 chrome 模糊部分的是:

@supports (zoom : 140%) {
  .c-flip-card:hover {
    zoom : 140%;
    transform: translate3d(-40px,-40px,0);
  }
  .c-flip-card:hover .c-flip-card__inner {
  transform: rotateY(180deg) scale(1);  
  } 
  .c-flip-card .c-flip-card__back h1{
  font-size: calc(var(--sizeH1));
  }
  .c-flip-card .c-flip-card__back p{
  font-size: calc(var(--sizeP));
  }
 }

但我无法转换属性zoom,所以效果不是很好。

【讨论】:

  • 还是模糊不清...!
  • 进行了编辑。部分解决。我使用 var 和 calc 使缩放部分更好。对于模糊的部分 Chrome 仍然是一个问题,Firefox 在转换结束时还可以,但在转换过程中仍然模糊。我为我尝试过的解决方案添加了链接。 @AmarjitSingh
  • 第二次编辑可能的解决方案:不要使用 scale() ! (但高度和宽度 % 代替)@AmarjitSingh
  • 是的,我也试过将scale:1.1.c-flip-card__back 它会解决,但这也是部分解决方案。
  • 在转换中添加了translate3d(40px,-40px,0),我得到了与使用 scale() 完全相同的效果,除了文本上的一点定位过渡
【解决方案2】:

我遵循 Amarjits 解决方案并将 scale:1.1 添加到 .c-flip-card__back,这是最好的结果,但是使用 Saffari 时会变得模糊

.c-flip-card__back {
    background-color: red;
    color: white;
    transform: rotateY(180deg) scale(1.1);
}

【讨论】:

    【解决方案3】:

    我找到了更好的解决方案。只是不要在背面卡片上使用缩放,所以动画只是在正面卡片淡出时补间它只显示背面卡片

    这种过渡在一半左右会产生缩放的错觉,而实际上它只是在设置的高度和宽度处显示一个 div。

    它可以在 Chrome 和 Safari 中使用,并且文字不会模糊

    .mycont{
        /* How pronounced should the 3D effects be */
        perspective: 500;
        -webkit-perspective: 500;
        -moz-perspective: 500;
        -ms-perspective: 500;
        -o-perspective: 500;
        width:100%;
        height:245px;
        position:relative;
        /*Some UI */
        border-radius:6px;
        -webkit-border-radius:6px;
        -moz-border-radius:6px;
        -ms-border-radius:6px;
        -o-border-radius:6px;
        font-size:28px;
        line-height:150px;
        vertical-align:middle;
        cursor:pointer;
    }
    
    .box-front,.box-back{
     		/* Enable 3D transforms */
    		transform-style: preserve-3d;
    		-webkit-transform-style: preserve-3d;
    		-moz-transform-style: preserve-3d;
    		-ms-transform-style: preserve-3d;
    		-o-transform-style: preserve-3d;
    		transform-style: preserve-3d;
    		 backface-visibility: hidden;
    		-webkit-backface-visibility: hidden;
    		-moz-backface-visibility: hidden;
    		-ms-backface-visibility: hidden;
    		-o-backface-visibility: hidden;
    		 width:100%;
    		height:100%;
    		position:absolute;
    		background-color:#0090d9;
     		/* Animate the transitions */
    		-webkit-transition:0.8s; text-align:center;
    		-moz-transition:0.8s; text-align:center;
    		-ms-transition:0.8s; text-align:center;
    		-o-transition:0.8s; text-align:center;
    		transition:0.8s; text-align:center;
    		color:#FFF;
    		border-radius:5px;
    		-webkit-border-radius:6px;
    		-moz-border-radius:6px;
    		-ms-border-radius:6px;
    		-o-border-radius:6px;
    		}
    
    .box-back{
     		/* The back side is flipped 180 deg by default */
    		transform:rotateY(180deg);
    		-webkit-transform:rotateY(180deg);
    		-moz-transform:rotateY(180deg);
    		-ms-transform:rotateY(180deg);
    		-o-transform:rotateY(180deg);
    		background-color:#f35958;
    		}
    
    .mycont:hover .box-front{
    		/* When the mycont is hovered, flip the front side and hide it .. */
    		transform:rotateY(180deg);
    		-webkit-transform:rotateY(180deg);
    		-moz-transform:rotateY(180deg);
    		-ms-transform:rotateY(180deg);
    		-o-transform:rotateY(180deg);
    		}
    
    .mycont:hover .box-back{
    		/* .. at the same time flip the back side into visibility */
    		transform:rotateY(360deg);
    		-webkit-transform:rotateY(360deg);
    		-moz-transform:rotateY(360deg);
    		-ms-transform:rotateY(360deg);
    		-o-transform:rotateY(360deg);
            margin-left: -0%; 
            margin-top: -10%;
            width: 300px;
            height:430px;
    }
    <div style="width:300px; margin-top:100px; margin-left:100px;">
      <div class="mycont">
          <div class="box-front">Front :)</div>
          <div class="box-back">
              rtrtrtrt
          </div>   
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2019-11-11
      • 2014-08-16
      • 2019-11-03
      • 2015-11-06
      • 2015-12-23
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      相关资源
      最近更新 更多