9

我有一个背景图像,我想在其上覆盖一些特定位置的框消息。有没有办法将框粘贴到图像上,以便它随图像缩放并保持其确切位置?它可能也需要缩放框内文本的字体大小。

另一方面,如何在不影响其框的情况下更改背景不透明度。

这是 jsfiddle 的链接:http: //jsfiddle.net/zd3CA/

更新 我希望结果看起来像这样,在路径的特定部分带有框。当图像调整大小时,我的盒子会四处走动。好像没说清楚。

CSS

.back {
    height: 85em;
    margin-bottom: 5em;
    background: url(http://kpv.s3.amazonaws.com/static/img/film.jpg) no-repeat;
    background-size: contain;
    color: #ffffff;
    text-shadow: 1px 1px 1px #000000;
}
.box-message {
    max-width: 15em;
    min-height: 10em;
    box-sizing: border-box;
    box-shadow: 1px 0 3px rgba(0, 0, 0, 0.11), -1px 0 3px rgba(0, 0, 0, 0.11);
    background: #fff;
    color:#000;
    padding: 25px 25px 35px 25px;
    position: relative;
}
.flow_three {
    margin-top: 3em;
    margin-left: 5em;
}
.flow_two {
    margin-top: 3em;
    margin-left: 10em;
}
.flow_text h3 {
    color: #1BB366;
    font-size: 20px;
}
.flow_text p {
    font-size: 18px;
    line-height: 25px;
}
.back .container {
    position: relative;
    z-index: 2;
}
.container {
    width: 940px;
}

HTML

<div class="back">
    <div class="container">
        <div class="box-message flow_text flow_three">
             <h3>text</h3>

            <p>text txtegv dsf asd fsda f asdf f as df sadf .</p>
        </div>
        <div class="box-message flow_text flow_two">
             <h3>text</h3>

            <p>text txtegv dsf asd fsda f asdf f as df sadf .</p>
        </div>
    </div>
</div>
4

9 回答 9

2
.should_work_on_any_browser{ /*For Opacity*/
    min-height: 100px;
    margin: auto;
    -moz-opacity: 0.52;
    opacity: 0.52;
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha"(Opacity=52);
    position:relative;
}
于 2013-09-06T12:07:49.820 回答
1

更新

调整宽度以使框保持在不透明背景的位置

您可以将背景图像直接设置为元素并定义background-widthset 以cover使图像按比例覆盖背景。

但是,这不会投射任何固定到特定位置的子元素。

此外 - 不能为背景图像设置单独的不透明度。需要将它设置在图像附加到的元素上,这意味着它也会影响子元素。

要解决这个问题,可以:

  • 编辑图像本身并将所需的不透明度保存为 PNG。
  • 可以使用画布修改不透明度(请参阅下面的解决方案)
  • 或者可以使用图像元素作为该元素的子元素。后者对于这种情况足够有效(并且比使用画布更有效)。

这是一个简单但有效的解决方案(Firefox 中存在问题,因为该浏览器目前不支持必要的 CSS 属性,但提供了一个可能的选项)。

HTML:

html代码的一个小的重新结构:

<div id="container">
    <img src="http://kpv.s3.amazonaws.com/static/img/film.jpg" width="794" height="477" />
    <div class="box-message flow_one">Ipsum lorem dummy text.
        <br>Ipsum lorem dummy text.
        <br>Ipsum lorem dummy text.
        <br>
    </div>
    <div class="box-message flow_two">Ipsum lorem dummy text.
        <br>Ipsum lorem dummy text.
        <br>Ipsum lorem dummy text.
        <br>
    </div>
</div>

CSS:

html, body {
    width:100%;
    height:100%;
    margin:0;
    padding:0;
}

/* Important: use fixed width/height for container */
#container {
    position:relative;
    width:794;
    height:477;
}

/* Let image follow width 100% and height auto-adjusted */
#container > img {
    width:100%;
    height:auto;
    opacity:0.5;
}

.box-message {
    position:absolute;
    border:2px solid #000;
    border-radius:7px;
    background:rgba(255, 255, 255, 0.85);
    padding:7px;
}
.flow_one {
    left:12%;
    top:10%;
}
.flow_two {
    left:50%;
    top:42%;
}

(图像在这里分开的唯一原因是允许子元素具有不同的不透明度。)

附加提示:如果您需要相对于设备屏幕具有不同的字体大小,您可以覆盖font-sizeusing@media查询。

JavaScript:

我们需要一个带有 JavaScript 的小片段来计算与图像/背景宽度相比的宽度比。

然后,这个比率用于zoom我们设置的 CSS 规则的属性,因为这将缩放父级和其中的所有内容(对于 Firefox,我们需要使用transform:scale(f),但是这有问题,因为它似乎与当前背景宽度一起累积)。

/// init parent element at load
redraw();

/// call everytime we resize
window.onresize = redraw;

/// calc ratio for zoom
function redraw() {
    /// ratio f = window width / background width (hard-coded for demo)
    var f = window.innerWidth / 794;
    //FF: container.style.transform = 'scale(' + f + ')';
    container.style.zoom = f.toFixed(2);
}

在线演示(适用于 Chrome 和其他支持 CSS 的浏览器zoom

更新 2

因为无论你如何扭曲和转动事物,你都会遇到这种方法以及 CSS 方法的兼容性问题。一个浏览器支持一件事,另一个浏览器支持另一件事,但不是第一个等等。他们只是还没有。

更可靠的方法(远离旧的 IE 浏览器)是使用纯 JavaScript 或 jQuery 等包装器手动计算元素的位置、大小、字体等。

可以通过如下测试来检查浏览器是否能够使用例如缩放:

if (typeof container.style.zoom === 'undefined') {

    /// manually calculate relations

} else {
    container.style.zoom = factor;
}

当然,计算部分比此处显示的更繁琐,因为您需要以适合最终解决方案的方式迭代元素等。

不透明度

如果单独的图像元素或背景图像(预定义的不透明度)是一个选项,则此解决方案为此提供了一个选项。请注意,在这种调整大小的情况下,这不是很有效,但可以作为最后的解决方案。

这是使用画布的示例:

var img = document.createElement('img'),

    canvas = document.createElement('canvas'),
    ctx = canvas.getContext('2d'),

    opacity = 0.5;

/// when image has loaded and a resize event occured
img.onload = window.onresize = draw;

/// resize canvas and draw image at given opacity
function draw() {

    /// set canvas = window client size
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    /// set opacity of canvas
    ctx.globalAlpha = opacity;

    /// draw image to canvas size
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);

    /// set background to resized image
    container.style.background = 'url(' + canvas.toDataURL() + ') no-repeat left top';
}

/// request cross-origin sharing (if different domain than page)
img.crossOrigin = 'anonymous';

/// set image source and start loading image
img.src = 'http://i.imgur.com/Y77lhhL.jpg';

在线演示在这里

注意:为此,您需要满足 CORS 要求(跨域资源共享)。这意味着图像是从某个来源(域、路径)加载的 - 或者 - 如果从服务器允许跨域共享的另一个来源加载。

您可以在小提琴演示中看到您提供的原始图像链接不适用于这种方法,因此我将图像移至允许跨域共享的imgur.com并且它可以工作。

于 2013-09-02T02:43:50.053 回答
1

如果您说要为表单添加背景图片,只需通过 css 应用背景图片即可。没有理由在表单后面创建一个元素作为背景,除非您打算通过 js 应用动画。即便如此,我还是建议使用 AS2/AS3。

所以,正如我所说,通过 css 在表单上使用背景图像。

于 2013-09-01T00:39:00.353 回答
1

您只需将图像作为“背景图像”应用​​到您的相对定位(父)容器元素并将“背景大小”设置为“包含”(正如您已经完成的那样)。

现在,如果您使用百分比宽度值定义您的(子)框并将它们绝对定位,也使用百分比(相对)值作为它们的位置。整体应该完美地放大和缩小。

于 2013-09-01T12:57:11.373 回答
1

该解决方案可以使用 % 和 em 等流体单位并使用绝对位置。

样式是:

*,html{
margin:0px;
padding:0px;
}
.back {
   height: 51em;
   margin-bottom: 5em;
   background: url(http://kpv.s3.amazonaws.com/static/img/film.jpg) no-repeat;
   background-size: contain;
   color: #ffffff;
   text-shadow: 1px 1px 1px #000000;
   width: 60%;
  }
  .back .container {
   position: relative;
   z-index: 2;
  }
  .container {
    width: 100%;
    height: 100%;
   }
  .box-message {

    box-sizing: border-box;
    box-shadow: 1px 0 3px rgba(0, 0, 0, 0.11), -1px 0 3px rgba(0, 0, 0, 0.11);
    background: #fff;
    color: #000;
    padding: 1% 2%;
   width: 26.66%;
  }
  .flow_three {
    margin-top: 5%;
    margin-left: 5%;
    position: absolute;
  }
  .flow_two {
  margin-top: 1%;
  float: right;
  right: 2%;
  position: absolute;
  }
  .flow_center{
  position: absolute;
  margin-top: 26%;
  margin-left: 44%;
  }
  .flow_text h3 {
   color: #1BB366; 
   font-size: 1.25em;
   }
   .flow_text p {
     font-size: 1.125em;
     line-height: 95%;
    }

html是

<div class="back">
<div class="container">
    <div class="box-message flow_text flow_three">
         <h3>text</h3>

        <p>text txtegv dsf asd fsda f asdf f as df sadf .</p>
    </div>
    <div class="box-message flow_text flow_two">
         <h3>text</h3>

        <p>text txtegv dsf asd fsda f asdf f as df sadf .</p>
    </div>
    <div class="box-message flow_text flow_center">
         <h3>text</h3>

        <p>text txtegv dsf asd fsda f asdf f as df sadf .</p>
    </div>
    </div>
 </div>
于 2013-09-02T08:02:23.020 回答
0

为了达到您想要的结果,首先图像必须与.box-message元素共享相同的空间参考(例如坐标系)。这是通过将图像放置在标签中并使其成为.box-message元素的兄弟来实现的。我们还需要图像是定义父元素高度和宽度的唯一子元素。为此,将.box-message元素设置为相对于父元素的绝对定位,并将图像width设置为 100%。这允许图像与父容器一起调整大小并保持其纵横比。

现在在元素上使用绝对定位,我们使用和属性.box-message设置它们相对于图像的坐标。这会将元素的左上角锚定到图像上的特定点。要从元素的中心点锚定元素,您可以添加属性。您可以在元素上设置最小/最大宽度/高度属性,也可以让它们相对于图像调整大小。topleft.box-message.box-messagetransform: translate3d(-50%,-50%,0);.box-message

vw最后,可以通过以视口单位(和/或)指定大小来调整字体大小vh。查看本教程和示例:http ://css-tricks.com/examples/ViewportTypography/

每当调整视口时,字体大小应相应更改。

看看这个例子小提琴:http: //jsfiddle.net/3wq25/1/

于 2013-09-02T00:43:43.633 回答
0

我建议为此使用 svg。

我做了一个例子:

<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
 <!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->

 <g>
  <title>Layer 1</title>
  <image x="0" y="0" width="640" height="480" id="svg_1" xlink:href="http://kpv.s3.amazonaws.com/static/img/film.jpg"/>
  <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="92" y="77" width="130" height="79" id="svg_4"/>
  <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="91.5" y="213" width="130" height="79" id="svg_6"/>
  <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="91.5" y="338.5" width="130" height="79" id="svg_7"/>
  <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="281.5" y="195" width="186" height="113.03077" id="svg_8"/>
  <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="468.5" y="31" width="161" height="97.83847" id="svg_9"/>
  <rect fill="#FF0000" stroke="#000000" stroke-width="5" x="474.50001" y="306.99999" width="144.99999" height="88.11538" id="svg_10"/>
  <text xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" id="svg_2" y="124" x="157" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text>
  <text id="svg_3" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="262" x="155" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text>
  <text id="svg_5" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="387" x="157" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text>
  <text id="svg_11" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="261" x="374" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text>
  <text id="svg_12" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="88" x="553" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text>
  <text id="svg_14" xml:space="preserve" text-anchor="middle" font-family="serif" font-size="24" y="357" x="548" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="0" stroke="#000000" fill="#000000">Asd</text>
 </g>
</svg>

它是用这个工具创建的: http: //svg-edit.googlecode.com/svn/branches/2.6/editor/svg-editor.html

一旦你的 svg 完成,你可以简单地在你的 css 背景属性中链接到它,瞧你有它,它使用包含元素调整大小。

或者您可以计算每个元素相对于包含元素的位置和大小(以 % 为单位),并据此编写您的 css。

于 2013-09-01T01:21:32.340 回答
0

我会用 lettering.js 做到这一点。http://letteringjs.com/

设置默认文本大小,然后通过添加填充保持框的相对大小。

两者都应该很容易扩展。

于 2013-09-05T19:22:51.627 回答
-1

所以......没有人意识到这很容易吗?

CSS:

.img {
background-image:url('image.jpg');
width: 100%;
height: 200px;
padding: 200px;
margin: 0px;
}

HTML:

<table class="img"><tr><td>

<h1>Text Here</h1>

<h3>Text Here</h3>

</td></tr></table>
于 2013-09-03T13:25:26.753 回答