2017-03-21 69 views
1

我有一个网站的钻石固定在页面的顶部。每个菱形是一个使用CSS变换属性旋转和定位的div盒。使元素响应CSS变换属性

钻石没有反应,网站缩小到一个较小的尺寸,我有麻烦让它响应,同时仍然保持在正确的位置。

我试过使用媒体查询来设置钻石和钻石容器的宽度/高度,但是这破坏了每颗钻石的位置。

您可以查看这里的网站:http://amberrein.wpengine.com/

这里是我的HTML:

  <div id="diamonds"> 
      <div class="diamond diamond-1"> 
       <div class="fill blank"></div> 
      </div> 
      <div class="diamond diamond-2"> 
       <div class="fill blank"></div> 
      </div> 
      <hr> 
      <div class="diamond diamond-3"> 
       <div class="fill logo"> 
       <a href="#"><img src="https://amberrein.wpengine.com/wp-content/uploads/2017/03/Logo.png"></a> 
       </div> 
      </div> 
      <div class="diamond diamond-4"> 
       <div class="fill blank"></div> 
      </div> 
      <div class="diamond diamond-5"> 
       <div class="fill blank"></div> 
      </div> 
      <div class="diamond diamond-6"> 
       <div class="fill blank"></div> 
      </div> 
      <div class="diamond diamond-7"> 
       <div class="fill blank"></div> 
      </div> 
      <div class="diamond diamond-8"> 
       <div class="fill book-now">Book Now</div> 
      </div> 
      <div class="social-diamonds"> 
       <div class="facebook"> 
       <a href="#"><i class="fa fa-facebook fa-fw fa-lg"></i></a> 
       </div> 
       <div class="twitter"> 
       <a href="#"><i class="fa fa-twitter fa-fw fa-lg"></i></a> 
       </div> 
       <div class="instagram"> 
       <a href="#"><i class="fa fa-instagram fa-fw fa-lg"></i></a> 
       </div> 
       <div class="google"> 
       <a href="#"><i class="fa fa-google-plus fa-fw fa-lg"></i></a> 
       </div> 
      </div> 
      </div> 

这里是我的CSS:

#diamonds { 
    background: transparent; 
    width: 100%; 
    max-width: 340px; 
    height: 455px; 
    position: fixed; 
    left: 0; 
    top: 0; 
    animation: 1.5s fadeInTop ease-in-out; 
} 

.admin-bar #diamonds { 
    top: 32px; 
} 
.diamond { 
    width: 150px; 
    height: 150px; 

} 
.diamond.diamond-7 { 
    width: 160px; 
    height: 160px; 
} 
.diamond.diamond-8 { 
    width: 75px; 
    height: 75px; 
} 
.fill { 
    width: 100%; 
    height: 100%; 
    box-sizing: border-box; 
    position: absolute; 
    display: block; 
} 
.logo img { 
    max-width: 150px; 
    width: 100%; 
    transform: rotate(-45deg) translate(-26px,10px); 

} 
.diamond-1 { 
    background: #eee; 
    transform: scale(1,1) rotate(45deg) translate(-106px, 0); 
} 
.diamond-2 { 
    background: #888; 
    transform: scale(1,1) rotate(45deg) translate(-52px, -266px); 
} 
.diamond-3 { 
    background: #ddd; 
    transform: scale(1,1) rotate(45deg) translate(-28px, -82px); 
} 
.diamond-4 { 
    background: rgba(85,85,85,0.9); 
    transform: scale(1,1) rotate(45deg) translate(-134px, -28px); 
} 
.diamond-5 { 
    background: rgba(187,187,187,.7); 
    transform: scale(1,1) rotate(45deg) translate(-80px, -294px); 
} 
.diamond-6 { 
    background: rgba(238,238,238,.5); 
    transform: scale(1,1) rotate(45deg) translate(-186px, -240px); 
} 
.diamond-7 { 
    background: transparent; 
    border: 1px solid #444; 
    transform: scale(1,1) rotate(45deg) translate(-220px, -427px); 
} 
.diamond-8 { 
    background: rgba(0,0,0,.05); 
    transform: scale(1,1) rotate(45deg) translate(-393px, -743px); 
} 
.fill.book-now { 
    transform: scale(1,1) rotate(-45deg) translate(0px,18px); 
    font-family: 'Cardo'; 
    font-size: 18px; 
    color: #000; 
    text-align: center; 
} 
#diamonds hr { 
    margin-top: -193px; 
    visibility: hidden; 
} 

.social-diamonds { 
    color: #fff; 
    z-index: 99999; 
    position: fixed; 
    top:-32px; 
    left:0; 
} 

.admin-bar .social-diamonds { 
    top: 0; 
} 

.social-diamonds a { 
    color: #fff; 
} 

.facebook { 
    color: #fff; 
    background: #ddd; 
    position: absolute; 
    display: block; 
    width: 50px; 
    height: 50px; 
    margin: 0 auto; 
    text-align: center; 
    line-height: 50px; 
    transform: scale(1,1) rotate(45deg) translate(347px,-15px); 
    transition: all 300ms ease-in-out; 
} 
i.fa-facebook { 
    transform: scale(1,1) rotate(-45deg); 
    transition: 300ms linear; 
} 

.twitter { 
    color: #fff; 
    background: #ddd; 
    position: absolute; 
    display: block; 
    width: 50px; 
    height: 50px; 
    margin: 0 auto; 
    text-align: center; 
    line-height: 50px; 
    transform: scale(1,1) rotate(45deg) translate(347px,60px); 
    transition: all 300ms ease-in-out; 
} 
i.fa-twitter { 
    transform: scale(1,1) rotate(-45deg); 
    transition: 300ms linear; 
} 

.instagram { 
    color: #fff; 
    background: #ddd; 
    position: absolute; 
    display: block; 
    width: 50px; 
    height: 50px; 
    margin: 0 auto; 
    text-align: center; 
    line-height: 50px; 
    transform: scale(1,1) rotate(45deg) translate(347px,144px); 
    transition: all 300ms ease-in-out; 
} 
i.fa-instagram { 
    transform: scale(1,1) rotate(-45deg); 
    transition: 300ms linear; 
} 

.google { 
    color: #fff; 
    background: #ddd; 
    position: absolute; 
    display: block; 
    width: 50px; 
    height: 50px; 
    margin: 0 auto; 
    text-align: center; 
    line-height: 50px; 
    transform: scale(1,1) rotate(45deg) translate(347px,218px); 
    transition: all 300ms ease-in-out; 
} 
i.fa-google-plus { 
    transform: scale(1,1) rotate(-45deg); 
    transition: 300ms linear; 
} 



.facebook:hover, .twitter:hover, .instagram:hover, .google:hover { 
    background: #aaa; 
     transition: all 300ms ease-in-out; 
    } 
+0

哪里是你的媒体查询? – scoopzilla

+1

据我所知,_responsiveness_意味着网页会根据您的屏幕尺寸进行调整。这并没有说动画的任何内容。获取响应式用户界面以重新定位动画看起来非常困难,并且实际上不是非常有用。你能澄清你需要什么吗? – Halcyon

+0

当将值设置为%或vw/vh时,由于为每个单独的div设置了transform -translate属性值,所以元素仍然处于“卡住”位置,我试图让它们在窗口正确定位时被调整大小或有人浏览移动/平板电脑等网站 – Trisha

回答

3

我建议的第一件事就是改变所有基于像素的尺寸设置为相对长度单位集,如vw

vw表示视口,最大值为100(最小值为最小正数)占用当前屏幕/分辨率的全部大小,并适用于高度和宽度。

尝试将.diamond类的宽度和高度更改为vw单元,然后查看它是否适合您。

由于它是一个相对单位集,它将调整大小以适应新的调整大小的视口,而不是在那里修复。

+0

我已经尝试将.diamond类设置为一个vw单元,但是这是不正常工作,元素仍然牢固地植入到位(与变换属性) 。 – Trisha

+0

我经历了将PX的所有东西都换成了大众 - 这似乎确实有帮助,它使所有的反应都很灵敏,所以谢谢! http://codepen.io/anon/pen/LWdLbJ - 现在我只需要使文本响应并添加媒体查询以更改徽标和图书现在和社交图标定位,以便它们不再处于钻石中! – Trisha

1

尝试使用视口单位设置钻石类相对于屏幕尺寸的宽度/高度。因此,例如:

diamond { 
    width: 14vw; 
    height: 14vw; 
} 

其中VW = 1 /第100次的视口宽度

我与此一点,并调整相当很好地固定金刚石NAV播放。

+0

你能提供一个codepen或jsfiddle,所以我可以看到这个吗?我已经在codepen.io中试过了,它不是正确的。 – Trisha

+0

我最好不使用transform-transnslate,而使用top/left/right/bottom位置属性? – Trisha

+0

@Trisha我只是将每个钻石类别(例如diamond-1,-2等)的固定宽度/高度更改为14vw。您可能需要对容器进行其他调整,也可能需要进行其他调整,但我认为这应该适用于您 – Mark

1

使用不同设备的媒体查询更改变换值。

/* Large desktop */ 
@media (min-width: 1200px) { 
/* your code */ 
} 

/* Portrait tablet to landscape and desktop */ 
@media (min-width: 768px) and (max-width: 979px) { 
/* your code */ 
} 

/* Landscape phone to portrait tablet */ 
@media (max-width: 767px) { 
/* your code */ 
} 

/* Landscape phones and down */ 
@media (max-width: 480px) { 
/* your code */ 
} 
+0

我知道我可以使用媒体查询更改转换值,但我还没有这样做,因为我不必为8000个代码行查询每个单独的钻石div。 – Trisha

+0

好吧,那么你可以尝试使用jquery使用resize()方法来获取设备并使用css()方法为所有钻石div设置内联css。 https://api.jquery.com/resize/&https://api.jquery.com/css/ –

0

这使用CSS网格和分数单位(fr)。全屏测试。

.kontener { 
 
    display: grid; 
 
    grid-gap: 5px; 
 
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); 
 
    grid-template-rows: repeat(2, 100px); 
 
} 
 
.a {color: white; font-size:1.75rem; font-weight:600; background-color: red;text-align: center;line-height: 100px;} 
 
.b {color: white; font-size:1.75rem; font-weight:600; background-color: green;text-align: center;line-height: 100px;} 
 
.c {color: white; font-size:1.75rem; font-weight:600; background-color: blue;text-align: center;line-height: 100px;} 
 
.d {color: white; font-size:1.75rem; font-weight:600; background-color: yellow;text-align: center;line-height: 100px;} 
 
.e {color: white; font-size:1.75rem; font-weight:600; background-color: purple;text-align: center;line-height: 100px;} 
 
.f {color: white; font-size:1.75rem; font-weight:600; background-color: grey;text-align: center;line-height: 100px;}
<div class="kontener"> 
 
    <div class="a">1</div> 
 
    <div class="b">2</div> 
 
    <div class="c">3</div> 
 
    <div class="d">4</div> 
 
    <div class="e">5</div> 
 
    <div class="f">6</div> 
 
</div>