【问题标题】:how to get circular menu (incl. hover + link functionality) fully responsive?如何让圆形菜单(包括悬停+链接功能)完全响应?
【发布时间】:2019-09-29 22:37:50
【问题描述】:

我试图让一个圆形菜单(包括悬停+链接功能)表现得完全负责

  • 悬停和链接部分基本上工作正常。
  • 没有悬停部分,圆形菜单的行为是响应式的。
  • 但包含悬停部分后,调整显示/屏幕宽度时菜单会被挤压。

我尝试过@media 的方法,在.ch-grid li 中为不同的屏幕宽度设置不同的宽度和高度,但这并不是真正的解决方案,只是一种临时解决方法。

ul,
li {
  list-style: none;
  margin: 0;
  padding: 0;
}

#test1 {
  display: -webkit-flex;
  display: flex;
  text-align: center;
  justify-content: space-between;
  margin-bottom: 30px;
  margin-left: 35px;
  margin-right: 35px;
}

#test2 {
  width: 15%;
  text-align: center;
  position: relative;
  border-radius: 50%;
  overflow: hidden;
  background-color: #ff0082;
}

ul li img {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  object-fit: cover;
}

.ch-grid:after,
.ch-item:before {
  content: '';
  display: table;
}

.ch-grid:after {
  clear: both;
}

.ch-grid li {
  width: 220px;
  height: 90px;
  display: inline-block;
}

.ch-item {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  overflow: hidden;
  position: relative;
  cursor: default;
  box-shadow: inset 0 0 0 16px rgba(255, 255, 255, 0.6), 0 1px 2px rgba(0, 0, 0, 0.1);
  transition: all 0.4s ease-in-out;
}

.ch-info {
  position: absolute;
  background: rgba(63, 147, 147, 0.8);
  width: inherit;
  height: inherit;
  border-radius: 50%;
  overflow: hidden;
  opacity: 0;
  transition: all 0.4s ease-in-out;
  transform: scale(0);
}

.ch-info p {
  color: #fff;
  padding: 13px 0px;
  font-style: normal;
  font-size: 9px;
  border-top: 1px solid rgba(255, 255, 255, 0.5);
  opacity: 0;
  transition: all 1s ease-in-out 0.4s;
}

.ch-info p a {
  display: block;
  color: rgba(255, 255, 255, 0.7);
  font-style: normal;
  font-weight: 700;
  font-size: 140%;
  letter-spacing: 1px;
  padding-top: 4px;
  font-family: 'Open Sans', Arial, sans-serif;
}

.ch-info p a:hover {
  color: rgba(255, 242, 34, 0.8);
}

.ch-item:hover {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.1), 0 1px 2px rgba(0, 0, 0, 0.1);
}

.ch-item:hover .ch-info {
  transform: scale(1);
  opacity: 1;
}

.ch-item:hover .ch-info p {
  opacity: 1;
}
<ul id="test1" class="ch-grid">
  <li id="test2">
    <div class="ch-item">
      <img src="https://gespreksmakers.nl/images/1_hansie_hansumus.jpg" />
      <div class="ch-info">
        <p><a href="https://nkbv.nl">Hansie<BR>Hansumus</a></p>
      </div>
    </div>
  </li>
  <li id="test2">
    <div class="ch-item">
      <img src="https://gespreksmakers.nl/images/1_missie_marble.jpg" />
      <div class="ch-info">
        <p><a href="https://kakivi.de">Missie<BR>Marble</a></p>
      </div>
    </div>
  </li>
  <li id="test2">
    <div class="ch-item">
      <img src="https://gespreksmakers.nl/images/1_piotr_linski.jpg" />
      <div class="ch-info">
        <p><a href="https://telegraaf.nl">Piotr<BR>Linski</a></p>
      </div>
    </div>
  </li>
  <li id="test2">
    <div class="ch-item">
      <img src="https://gespreksmakers.nl/images/1_red.jpg" />
      <div class="ch-info">
        <p><a href="https://www.tukhut.nl">Mister<BR>Red</a></p>
      </div>
    </div>
  </li>
  <li id="test2">
    <div class="ch-item">
      <img src="https://gespreksmakers.nl/images/1_green.jpg" />
      <div class="ch-info">
        <p><a href="https://www.alumnei.nl">Miss<BR>Green</a></p>
      </div>
    </div>
  </li>
  <li id="test2">
    <div class="ch-item">
      <img src="https://gespreksmakers.nl/images/1_blue.jpg" />
      <div class="ch-info">
        <p><a href="https://www.astronieuws.nl">Mister<BR>Blue</a></p>
      </div>
    </div>
  </li>
</ul>

可以在此处找到显示当前 CSS 和 HTML 代码的小提琴:https://jsfiddle.net/piotrlinski/b3tL9v4h/8/

有什么解决办法的建议吗?

【问题讨论】:

  • 您可以随时使用伪将您的 li 塑造成正方形 jsfiddle.net/wzoxj8u3 还删除了 clear 和 table 伪规则对于 flex 布局完全无用。

标签: html css menu hover responsive


【解决方案1】:

class 属性可以与多个 HTML 元素/标签一起使用,并且 都会生效。其中 id 用于单个 元素/标签,被认为是唯一的。而且 id 有更高的 比类的特异性值。

响应式图片:

.img-responsive {
  display: block;
  max-width: 100%;
  height: auto;
}

无序列表:

<ul class="thumbnails">
  <li>Thumbnail 1</li>
  <li>Thumbnail 2</li>
  <li>...</li>
</ul>

将无样式列表与display: flex 放在一行中:

.thumbnails {
  display: flex;
  list-style: none;
  ...
}

缩略图的结构

<a href="https://nkbv.nl" class="thumbnail">
  <img src = "https://gespreksmakers.nl/images/1_hansie_hansumus.jpg"/>
  <div class="overlay">
    <p class="text">Mister White</p>
  </div>
</a>

其中.thumbnail 具有position: relative.overlay 具有position: absolute,因此.overlay.thumbnail 位于相同 区域。

使用vw 使字体大小响应。

修改后的清晰解决方案

.thumbnails {
  display: flex;
  list-style: none;
  padding: 0 2vw;
}

.thumbnails>li {
  flex: 1 0;
  margin: 0 5px;
  text-align: center;
  line-height: 0;
}

.thumbnail,
.thumbnail>img,
.thumbnail>.overlay {
  border-radius: 50%;
}

.thumbnail {
  position: relative;
  display: inline-block;
  cursor: pointer;
}


/* Responsive images */

.thumbnail>img {
  display: block;
  max-width: 100%;
  height: auto;
}

.thumbnail>.overlay {
  background: rgba(63, 147, 147, 0.8);
  opacity: 0.7;
  position: absolute;
  top: 0;
  height: 100%;
  width: 100%;
  transform: scale(0);
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
  transition: all 0.4s ease-in-out;
}

.thumbnail>.overlay>.text {
  color: white;
  width: 66.66%;
  line-height: 100%;
  margin: 0 auto;
  padding: 5px 0;
  border-bottom: 1px solid;
  border-top: 1px solid;
  font-size: 1.85vw;
  transition: opacity 1s ease-in-out 0.4s;
  opacity: 0;
}

.thumbnail:hover>.overlay {
  transform: scale(1);
}

.thumbnail:hover>.overlay>.text {
  opacity: 1;
}
<ul class="thumbnails">
  <li>
    <a href="https://nkbv.nl" class="thumbnail">
      <img src="https://gespreksmakers.nl/images/1_hansie_hansumus.jpg" />
      <div class="overlay">
        <p class="text">Mister White</p>
      </div>
    </a>
  </li>
  <li>
    <a href="https://kakivi.de" class="thumbnail">
      <img src="https://gespreksmakers.nl/images/1_missie_marble.jpg" />
      <div class="overlay">
        <p class="text">Mister White</p>
      </div>
    </a>
  </li>
  <li>
    <a href="https://telegraaf.nl" class="thumbnail">
      <img src="https://gespreksmakers.nl/images/1_piotr_linski.jpg" />
      <div class="overlay">
        <p class="text">Mister White</p>
      </div>
    </a>
  </li>
  <li>
    <a href="https://www.tukhut.nl" class="thumbnail">
      <img src="https://gespreksmakers.nl/images/1_red.jpg" />
      <div class="overlay">
        <p class="text">Mister White</p>
      </div>
    </a>
  </li>
  <li>
    <a href="https://www.alumnei.nl" class="thumbnail">
      <img src="https://gespreksmakers.nl/images/1_green.jpg" />
      <div class="overlay">
        <p class="text">Mister White</p>
      </div>
    </a>
  </li>
  <li>
    <a href="https://www.astronieuws.nl" class="thumbnail">
      <img src="https://gespreksmakers.nl/images/1_blue.jpg" />
      <div class="overlay">
        <p class="text">Mister White</p>
      </div>
    </a>
  </li>
</ul>

【讨论】:

    【解决方案2】:

    尽管我做了很多更改,但您可以查看下面的代码。

    一些提示:

    ID 在 HTML 文档中必须是唯一的,因此您不应多次使用 ID(例如 #test2)。

    此外,最好使用您创建的类来设置文档的样式,而不是使用 ID(例如,#test1、#test2 可以分别替换为 .ch-grid 和 .ch-grid li)。

    <style>
      ul,
      li {
        list-style: none;
        margin: 0;
        padding: 0;
      }
      .ch-grid {
        display: -webkit-flex;
        display: flex;
        flex-wrap: wrap;
        text-align: center;
      }
      .ch-grid:after,
      .ch-item:before {
        content: "";
        display: table;
      }
    
      .ch-grid:after {
        clear: both;
        flex: auto;
      }
      .ch-grid li {
        flex: 1;
        display: inline-block;
        min-width: calc(100% / 6);
        max-width: calc(100% / 6);
        position: relative;
        background-color: #ff0082;
        overflow: hidden;
        border-radius: 50%;
        text-align: center;
      }
      .ch-grid li img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        position: relative;
        z-index: 10;
      }
      .ch-item {
        position: relative;
        width: 100%;
        height: 100%;
        border-radius: 50%;
        overflow: hidden;
        cursor: default;
        box-shadow: inset 0 0 0 16px rgba(255, 255, 255, 0.6),
          0 1px 2px rgba(0, 0, 0, 0.1);
        transition: all 0.3s ease-in-out;
      }
      .ch-info {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        display: -webkit-flex;
        display: flex;
        flex-direction: column;
        justify-content: center;
        background: rgba(63, 147, 147, 0.8);
        border-radius: 50%;
        overflow: hidden;
        opacity: 0;
        transition: all 0.4s ease-in-out;
        transform: scale(0);
        z-index: 20;
      }
      .ch-info p {
        color: #fff;
        padding: 13px 0px;
        font-style: normal;
        font-size: 9px;
        border-top: 1px solid rgba(255, 255, 255, 0.5);
        opacity: 0;
        transition: all 1s ease-in-out 0.4s;
      }
      .ch-info p a {
        display: block;
        color: rgba(255, 255, 255, 0.7);
        font-style: normal;
        font-weight: 700;
        font-size: 140%;
        letter-spacing: 1px;
        padding-top: 4px;
        font-family: "Open Sans", Arial, sans-serif;
      }
      .ch-info p a:hover {
        color: rgba(255, 242, 34, 0.8);
      }
      .ch-item:hover {
        box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.1),
          0 1px 2px rgba(0, 0, 0, 0.1);
      }
      .ch-item:hover .ch-info {
        transform: scale(1);
        opacity: 1;
      }
      .ch-item:hover .ch-info p {
        opacity: 1;
      }
      @media (max-width: 767px) {
        .ch-grid li {
          min-width: calc(100% / 3);
          max-width: calc(100% / 3);
        }
      }
    </style>
    

    【讨论】:

      【解决方案3】:

      如果您将宽度设置为百分比,则列表项将不会显示为圆形,具体取决于屏幕宽度。由于这种情况,您应该为列表项设置固定宽度并将换行设置为父项,如下所示:

      #test1 {
          display: -webkit-flex;
          display: flex;
          text-align: center;
          justify-content: space-between;
          flex-wrap: wrap;
          margin-bottom: 30px;
          margin-left: 35px;
          margin-right: 35px;
      }
      
      #test2 {
          flex-basis: 85px;
          text-align: center;
          position: relative;
          border-radius: 50%;
          overflow: hidden;
          background-color: #ff0082;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-13
        • 2013-03-02
        • 2013-02-24
        • 1970-01-01
        • 2011-12-22
        相关资源
        最近更新 更多