【问题标题】:Is it possible to mirror content using CSS?是否可以使用 CSS 镜像内容?
【发布时间】:2014-09-13 03:55:53
【问题描述】:

基本上,我正在尝试使用 CSS 来创建图像的反射,如下所示(和/或 jQuery,但不会真正影响性能)。这也需要响应式地工作。

我的想法是会有一个 iMac PNG(为网站截图切出一个洞),然后使用内容管理系统上传网站截图,并将其放置在屏幕内部和后面以显示....这一切都很好,但我希望屏幕和网站截图反映在下面。我知道这只是微妙的,但如果可能的话,我想这样做。

这是我打算使用的代码结构,请随意在任何示例中使用它:)

演示:http://codepen.io/anon/pen/gKnry

<div class="screen_contain">
    <img src="imac-image.png" class="imac">
    <img src="website_image.png" class="website">
</div>
.screen_contain {
  display:inline-block;
  position: relative;
  width:33%;
}

.imac {
  width: 100%;
  position: relative;
  z-index: 10;
}

.website {
  position: absolute;
  left: 5.0%;
  top: 5%;
  width: 90%;
  z-index: 5;
}

注意事项:

  • 我显然可以合并 iMac 图像,使其具有主 iMac 并且它反映在一个图像中...节省请求和混乱的代码。
  • 这将由客户填充,因此他们所能做的就是上传网站的屏幕截图。

谢谢!

【问题讨论】:

  • css-tricks.com/snippets/css/flip-an-image 不过,您需要两张图片。 css 不能“克隆”一个 dom 元素。那不是它的工作。这只是样式规则。如果您想要原始版本和翻转版本,则必须将图像嵌入两次。
  • 嗯,它只是 WebKit,但有这个:CSS Reflections。诚然,自从我上次查看以来,这可能已从 WebKit 中删除,或者成为跨浏览器。
  • @DavidThomas:我不认为它已从 WebKit 中删除,而是 I don’t think it’s gone cross-browser either
  • @Paul:我怀疑当前的“扁平化”设计是对早期智能手机时代流行的闪亮/拟物设计的回应;既然如此(恕我直言,等等),我想有光泽的 UI 将会回归(尽管与上一代有所不同)以响应扁平化设计。

标签: jquery css


【解决方案1】:

这是一个显示 pure CSS 实时反射的小提琴(如果您更改源元素,反射也会改变):http://jsfiddle.net/scvprc2r/

这仅适用于 Webkit 和 Gecko 浏览器(仅在 Chrome 和 Firefox 中测试)。如果您想要更好的浏览器支持,请使用 SVG 或 JS 解决方案。

修改后的结构:

<div class="screen_contain">
    <div class="computer" id="one">
        <img src="http://ferncroft.co.uk/files/2014/08/ethernal-demo-imac.png" class="imac" />
        <img src="http://danzambonini.com/wp-content/uploads/2010/07/stack_overflow_initial.jpg" class="website" id="web1" />
    </div>
    <div class="reflection"></div>
</div>

我在图像周围添加了一个额外的包装器 div,向站点图像和包装器 div 添加了唯一 ID。我还添加了另一个 div 来保存 Firefox 中的反射和两个浏览器中的渐变淡入淡出效果。

CSS:

.computer {
    -webkit-box-reflect: below 0px linear-gradient(to top, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 0) 100%);
}

这会使用-webkit-box-reflect 在元素下方创建元素的反射。添加渐变以产生更平滑的效果。

.reflection {
    transform: scaleY(-1);
}

我们的基本反射样式。我们翻转它,使 mozilla element() 背景处于正确的方向

#one + .reflection {
        background: linear-gradient(to bottom, rgba(255, 255, 255, 1) 0%, rgba(255, 255, 255, 1) 32%, rgba(255, 255, 255, 0) 100%) 100% 100%,
                    -moz-element(#web1) no-repeat 50% -5%,
                    -moz-element(#one) no-repeat 100% 100%;
}

这就是在 Firefox 中进行反射的方式。这利用了element() 函数,该函数创建元素的实时图像。元素背景设置为div上的第二和第三个背景。

然后为每个额外的重复 div 重复此代码。

在 Firefox 中,反射背景的大小和位置取决于它所在的 div 的高度。下面是一些 jQuery,用于将反射的大小动态更改为原始元素的大小:

function resizeReflection() {
    $('.reflection').each(function () {
        $(this).height($(this).prev().height());
    });
}

resizeReflection();

$(window).resize(function () {
    resizeReflection();
});

如果您的图片是固定大小的,则不需要这样做。

这是什么样子的:

火狐 铬合金

如果您想要 SVG 解决方案而不是 CSS 解决方案,请查看以下内容(更简单,并且具有更好的浏览器支持):http://jsfiddle.net/JeremiePat/bGD3J/

【讨论】:

    【解决方案2】:

    使用 CSS 变换来翻转图像。然后,使用 CSS 掩码创建图像的 alpha 掩码 (http://www.html5rocks.com/en/tutorials/masking/adobe/#toc-alpha-mask)。

    不幸的是,CSS alpha 掩码似乎没有得到广泛支持http://caniuse.com/#search=mask。如果您有纯色背景,另一种方法是将 iMac 图像放置在元素中(可能作为背景)并在顶部放置一个渐变为背景颜色的 PNG-24 渐变图像。

    【讨论】:

      【解决方案3】:

      jsFiddle Example

      CSS

      .reflection {
          transform: rotate(180deg); 
          width: 100%;
      }
      
      .gradient {
          position: absolute;
          top: 0;
          left: 0;
          height: 100%;
          width: 100%;
          z-index: 999;
          background: -webkit-gradient(linear, left bottom, left top,
             color-stop(0%,rgba(255,255,255,.5)), 
             color-stop(60%,rgba(255,255,255,1)));
      }
      

      要避免使用 jquery:只需复制 HTML 即可避免使用 jquery

      <div class="screen_contain">
          <img src="http://ferncroft.co.uk/files/2014/08/ethernal-demo-imac.png" class="imac" />
          <img src="http://danzambonini.com/wp-content/uploads/2010/07/stack_overflow_initial.jpg" class="website" />
          <div class="screen_contain reflection">
              <img src="http://ferncroft.co.uk/files/2014/08/ethernal-demo-imac.png" class="imac" />
              <img src="http://danzambonini.com/wp-content/uploads/2010/07/stack_overflow_initial.jpg" class="website" />
              <div class="gradient"></div>
          </div>
      </div>
      

      使用 Jquery jsFiddle Example

        var screen_captain = $('.screen_contain');
      
        $.each(screen_captain, function(){
           $(this).append($(this).clone().addClass('reflection').append('<div class="gradient"></div>'));
        })
      

      【讨论】:

        【解决方案4】:

        这是一个重复问题,已在 StackOverflow 主题中得到解答:

        CSS property box-reflect compatibility?

        不仅可以使用 webkit(最新的 chrome 或 safari),还可以使用最新的 firefox。

        示例如下:http://codepen.io/jonathan/pen/pgioE

        感谢大卫:

        http://satreth.blogspot.com/2012/05/css3-reflection.html

        HTML:

        <div id="someid">
           <img src="image url" />
        <div/>
        

        CSS (webkit):

        #someid {
                /* need some space for the reflection */
                margin-bottom: 120px;
                /* the gradient makes the reflection fade out */
                -webkit-box-reflect: below 0px -webkit-linear-gradient(bottom, rgba(255,255,255,0.3) 0%, transparent 40%, transparent 100%);
        }
        

        CSS (Firefox - Gecko):

        #someid {
                position: relative;
                /* need some space for the reflection */
                margin-bottom: 120px;
        }
        
        #someid:before {
                content:""; /* needed or nothing will be shown */
                background: -moz-linear-gradient(top, white, white 30%, rgba(255,255,255,0.9) 65%, rgba(255,255,255,0.7)) 0px 0px, -moz-element(#someid) 0px -127px no-repeat;
                -moz-transform: scaleY(-1); /* flip the image vertically */
                position:relative;
                height:140px;
                width: 360px; /* should be > image width + margin + shadow */
                top: 247px;
                left:0px;
         }
        

        Firefox 使用 -moz-element 进行反射 (https://developer.mozilla.org/en-US/docs/CSS/element),而 webkit 使用专有的供应商前缀进行反射。

        其他例子:

        http://lea.verou.me/2011/06/css-reflections-for-firefox-with-moz-element-and-svg-masks/

        干杯!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-24
          • 1970-01-01
          • 2014-08-07
          • 2015-11-28
          • 2017-12-02
          • 1970-01-01
          • 2022-11-09
          相关资源
          最近更新 更多