【问题标题】:scale a div properly on all devices在所有设备上正确缩放 div
【发布时间】:2021-07-13 21:54:49
【问题描述】:

如何正确缩放 div 元素以适应特定区域?我有一个 div 元素,我想在悬停时缩放以适应中间的圆圈(参见代码 sn-p)。现在我可以设置缩放值,但这在更小或更大的屏幕上都无法正常工作。珊瑚色的 div 应该有 100vw。 以下是我的 HTML,css 和 jquery:

  $(".contact-button").hover(function(){
    $(".holdeer").addClass('contact');
    }, function(){
    
    $(".holdeer").removeClass('contact');
    });
.getintouch {
  border-top-left-radius: 0px;
  border-top-right-radius: 0px;
  border-bottom-left-radius: 0px;
  border-bottom-right-radius: 0px;
  height: 20vh;
  width: 100vw;
}







.contact-us {
  position: relative;
  z-index: 0;
  display: flex;
  height: 700px;
  width: 100vw;
  flex-direction: column;
  justify-content: space-around;
  align-items: stretch;
  background-color: transparent;
}
.contact-button {
  position: absolute;
  z-index: 100;
  display: flex;
  width: 200px;
  height: 200px;
  margin-top: 0px;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  align-self: center;
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: auto;
  border-top-style: solid;
  border-top-width: 1px;
  border-right-style: solid;
  border-right-width: 1px;
  border-bottom-style: solid;
  border-bottom-width: 1px;
  border-left-style: solid;
  border-left-width: 1px;
  border-top-left-radius: 50%;
  border-top-right-radius: 50%;
  border-bottom-left-radius: 50%;
  border-bottom-right-radius: 50%;
  background-color: transparent;
  color: blue;
  font-size: 32px;
  font-weight: 700;
}

.holdeer {
  position: absolute;
  left: 0%;
  top: 0%;
  right: 0%;
  bottom: 0%;
  display: block;
  width: auto;
  height: auto;
  justify-content: center;
  align-items: center;
  align-self: auto;
  flex-grow: 0;
  flex-shrink: 1;
  flex-basis: auto;
  background-color: coral;
}

.contact {
    
  animation: shrink  1s ease forwards;
  transition: all 1s;
    

}
@keyframes shrink {
   from {
   border-radius: 100%;
        }
        
    to{ 
    
    border-radius: 100%;
 transform: scale(0.15, 0.28);
}
  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="getintouch">
<div class="contact-us">
<div class = "contact-button"></div>
<div class="holdeer"></div>


</div>
 </section>

【问题讨论】:

    标签: css css-animations scale


    【解决方案1】:

    您可以使用更简单、更简洁的代码来做到这一点,也可以只使用 css。

    .contact-us {
      overflow: hidden; /* to hide anything that goes outside */
      display: flex;
      height: 700px;
      width: 100vw;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      background-color: transparent;
    }
    .contact-button {
      position: relative; /* to make 'absolute' children relative to it */
      display: flex;
      width: 200px;
      height: 200px;
      flex-direction: row;
      justify-content: center;
      align-items: center;
      border: 1px solid;
      border-radius: 50%;
      transition: all 1s;
    }
    
    .holdeer {
      position: absolute;
      z-index: -1;
      display: block;
      width: 100%; /* takes full width of parent */
      height: 100%; /* takes full height of parent */
      border-radius: 50%;
      transform: scale(15); /* scale to very large value to cover .contact-us and overflow: hidden with cut off what goes outside */
      transition: all 1s;
      background-color: coral;
      pointer-events: none; /* to disable hover on this child */
    }
    .contact-button:hover .holdeer {
      transform: scale(1); /* normal scale to fit the parent circle */
    }
    <div class="contact-us">
      <div class="contact-button"><div class="holdeer"></div></div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2018-11-25
      相关资源
      最近更新 更多