【问题标题】:Partial "X" shape over image on hover悬停时图像上的部分“X”形状
【发布时间】:2015-05-12 08:36:42
【问题描述】:

我需要一种悬停时图像上方的 X 形状。但不是整个“X”,只是外部位。这是我设计师的图片:

我有一个很好的“X”形状悬停在这里:

#xdiv {
  width: 200px;
  height: 200px;
  border: 1px solid #999;
  background-image: url('http://placehold.it/200x200');
}
#xdiv:hover {
  cursor: pointer;
  opacity: .4;
}
#xdiv:hover .xdiv1 {
  height: 200px;
  width: 1px;
  margin-left: 100px;
  background-color: #333;
  transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  /* IE 9 */
  -webkit-transform: rotate(45deg);
  /* Safari and Chrome */
  z-index: 1;
}
#xdiv:hover .xdiv2 {
  height: 200px;
  width: 1px;
  background-color: #333;
  transform: rotate(90deg);
  -ms-transform: rotate(90deg);
  /* IE 9 */
  -webkit-transform: rotate(90deg);
  /* Safari and Chrome */
  z-index: 2;
}
<div id="xdiv">
  <div class="xdiv1">
    <div class="xdiv2"></div>
  </div>
</div>

我从this answer修改的。

按照我的设计师的例子,这是否可以使用 CSS 而不是在悬停时使用另一个图像?

【问题讨论】:

    标签: html css hover css-shapes


    【解决方案1】:

    使用 box-shadow 的替代方法

    div{
      background-image: url('http://placehold.it/200x200');
      width: 200px; 
      height: 200px;
      position: relative;
      margin: 40px auto;
      z-index: 0;
    }
    div:before, div:after{
      content: '';
      position: absolute;
      width: 33.3333333%;
      height: 2px;
      background: black;
      z-index: 1;
    }
    div:before{
      transform: rotate(45deg);
      top: 26px;
      left: -6px;
      box-shadow: 13em 0 0;
    }
    div:after{
      transform: rotate(-45deg);
      right: -6px;
      top: 26px;
      box-shadow: -13em 0 0;
    }
    &lt;div&gt;&lt;/div&gt;

    div{
      background-image: url('http://placehold.it/200x200');
      width: 200px; 
      height: 200px;
      position: relative;
      margin: 40px auto;
      z-index: 0;
    }
    div:before, div:after{
      content: '';
      position: absolute;
      width: 33.3333333%;
      height: 2px;
      background: black;
      z-index: 1;
      top: 48px;
    }
    div:before{
      transform: rotate(45deg);
      left: 14px;
      box-shadow: 10em 0 0;
    }
    div:after{
      transform: rotate(-45deg);
      right: 14px;
      box-shadow: -10em 0 0;
    }
    &lt;div&gt;&lt;/div&gt;

    【讨论】:

      【解决方案2】:

      使用渐变的方法:

      1. 使用 div 和背景颜色,我创建了方形 div
      +-------------+
      |             |
      |             |
      |             |
      |             |
      |             |
      |             |
      |             |
      +-------------+
      
      1. 然后,使用一个伪元素旋转 45 度,另一个旋转 -45 度, 您可以生成“X”形状(当前设置纯色背景色以帮助定位):
      +-------------+
      |\           /|
      |  \       /  |
      |    \   /    |
      |      \      |
      |     /  \    |
      |   /      \  |
      | /          \|
      +-------------+
      
      1. 现在,您可以使用渐变背景,而不是使用纯黑色,并使用“色标”使中心透明。 (注意:对于渐变,我会推荐this generator

      2. 这会留下如下内容:

      +-------------+
      |\           /|
      |  \       /  |
      |             |
      |             |
      |             |
      |   /      \  |
      | /          \|
      +-------------+
      

      取决于您对色标的定位。

      演示

      div {
        height: 300px;
        width: 300px;
        background: url(http://placekitten.com/g/300/300);
        position: relative;
        transform-origin: center center;
        overflow: hidden;
      }
      div:before {
        content: "";
        position: absolute;
        top: -50%;
        left: calc(50% - 1px);
        height: 200%;
        width: 2px;
        transform: rotate(45deg);
      }
      div:after {
        content: "";
        position: absolute;
        top: -50%;
        left: calc(50% - 1px);
        height: 200%;
        width: 2px;
        transform: rotate(-45deg);
      }
      div:hover:before, div:hover:after{
       background: -moz-linear-gradient(top,  rgba(0,0,0,1) 0%, rgba(0,0,0,1) 30%, rgba(0,0,0,0) 31%, rgba(0,0,0,0) 69%, rgba(0,0,0,1) 70%, rgba(0,0,0,1) 100%); /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,1)), color-stop(30%,rgba(0,0,0,1)), color-stop(31%,rgba(0,0,0,0)), color-stop(69%,rgba(0,0,0,0)), color-stop(70%,rgba(0,0,0,1)), color-stop(100%,rgba(0,0,0,1))); /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top,  rgba(0,0,0,1) 0%,rgba(0,0,0,1) 30%,rgba(0,0,0,0) 31%,rgba(0,0,0,0) 69%,rgba(0,0,0,1) 70%,rgba(0,0,0,1) 100%); /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top,  rgba(0,0,0,1) 0%,rgba(0,0,0,1) 30%,rgba(0,0,0,0) 31%,rgba(0,0,0,0) 69%,rgba(0,0,0,1) 70%,rgba(0,0,0,1) 100%); /* Opera 11.10+ */
      background: -ms-linear-gradient(top,  rgba(0,0,0,1) 0%,rgba(0,0,0,1) 30%,rgba(0,0,0,0) 31%,rgba(0,0,0,0) 69%,rgba(0,0,0,1) 70%,rgba(0,0,0,1) 100%); /* IE10+ */
      background: linear-gradient(to bottom,  rgba(0,0,0,1) 0%,rgba(0,0,0,1) 30%,rgba(0,0,0,0) 31%,rgba(0,0,0,0) 69%,rgba(0,0,0,1) 70%,rgba(0,0,0,1) 100%); /* W3C */
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#000000',GradientType=0 ); /* IE6-9 */
      
      
        
      &lt;div&gt;&lt;/div&gt;

      【讨论】:

      • @jeannie:我只是发布了一种不同的方法(顺便说一句,还有其他方法)。 ut 对于您的解决方案/案例,我个人会选择 web-tiki 的解决方案
      • 明白了,感谢您更好地解释此解决方案。
      【解决方案3】:

      编辑:

      您可以通过以下方式实现部分 X 形状:

      • 2个伪元素代替div来减少标记
      • 顶部/底部边框且无背景,因此形状的中间部分是透明的

      DEMO

      注意:您还应该在前缀属性之后声明非前缀属性(在您的 sn-p 中进行转换)

      #xdiv {
        position: relative;
        width: 200px;
        height: 200px;
        border: 1px solid #999;
        background-image: url('http://placehold.it/200x200');
      }
      #xdiv:hover {
        cursor: pointer;
        opacity: .4;
      }
      #xdiv:before, #xdiv:after {
        content: '';
        position: absolute;
        display: none;
        left: 50%; top: 0;
        width: 1px; height: 100px;
        border-top: 50px solid #333;
        border-bottom: 50px solid #333;
        -ms-transform: rotate(45deg);
        -webkit-transform: rotate(45deg);
        transform: rotate(45deg);
      }
      #xdiv:hover:before {
        display: block;
      }
      #xdiv:hover:after {
        display: block;
        -ms-transform: rotate(-45deg);
        -webkit-transform: rotate(-45deg);
        transform: rotate(-45deg);
      }
      &lt;div id="xdiv"&gt;&lt;/div&gt;

      上一个答案:

      DEMO

      #xdiv {
          width: 200px;
          height: 200px;
          border: 1px solid #999;
          background-image: url('http://placehold.it/200x200');
      }
      #xdiv:hover {
          cursor: pointer;
          opacity: .4;
      }
      #xdiv:hover .xdiv1 {
          height: 100px;
          width: 1px;
          margin-left: 100px;
          border-top: 50px solid #333;
          border-bottom: 50px solid #333;
          transform: rotate(45deg);
          -ms-transform: rotate(45deg); /* IE 9 */
          -webkit-transform: rotate(45deg); /* Safari and Chrome */
          z-index: 1;
      }
      #xdiv:hover .xdiv2 {
          position:relative;
          top:-50px;
          height: 100px;
          width: 1px;
          border-top: 50px solid #333;
          border-bottom: 50px solid #333;
          transform: rotate(90deg);
          -ms-transform: rotate(90deg); /* IE 9 */
          -webkit-transform: rotate(90deg); /* Safari and Chrome */
          z-index: 2;
      }
      <div id="xdiv" >
      <div class="xdiv1">
          <div class="xdiv2"></div>
      </div>
      </div>

      【讨论】:

      • 我遇到了一个小问题。我正在使用 Bootstrap 并添加 Bootstrap CSS 完全破坏了这种“X”悬停样式。我试图准确指出是什么杀死了它,但这并不是很明显:(
      • @Jeannie 你把它放在小提琴里还是我可以看的地方?
      • 是的,这是添加了 Bootstrap CDN 的代码JSFiddle
      • @Jeannie 好的,因为引导程序将 box-sizing:border-box; 属性添加到伪元素。您可以在这些伪元素上将该属性更改为content-box,也可以将它们的高度更改为200px,例如jsfiddle.net/webtiki/z2vsLdzn/2
      • 不错!我认为这就是正在发生的事情,但在开发工具中将其关闭并没有解决它。非常感谢 :D Plus 现在我更好地了解了您的解决方案是如何工作的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 2018-02-13
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多