简体   繁体   English

HTML / CSS图像叠加

[英]HTML/CSS image overlay

Implementing a "play video" function on a web site. 在网站上实现“播放视频”功能。 Each video content item can have a different image. 每个视频内容项可以具有不同的图像。 Each of these images will have the same width, but potentially differing heights (they are resized on upload to maintain aspect ratio to meet standard width requirements). 这些图像中的每个图像都将具有相同的宽度,但是高度可能不同(在上载时会调整它们的大小,以保持宽高比以满足标准宽度要求)。

The plan was to display another transparent "play button" image over top of the content image using markup like this: 计划是使用如下标记在内容图像上方显示另一个透明的“播放按钮”图像:

            <div class="media">
                <a class="videoLink" href="#" style="background-image: url(http://cloud.github.com/downloads/malsup/cycle/beach2.jpg);" >
                    <img src="PlayButton.png" alt="Click to Play" height="200" width="300" />
                </a>
            </div>

This is very similar to how channel 9 does it on their home page. 这非常类似于渠道9在其主页上执行此操作的方式。 This, however, appears to assume any image is of standard height and width. 但是,这似乎是假设任何图像的高度和宽度都是标准的。 Are there alternative ways of tackling this? 有其他解决方法吗?

Forgot to mention originally. 本来忘了提。 We have a predefined width that things will fit into, however, each image may have a different height. 我们有一个可以适应的预定义宽度,但是每个图像可能具有不同的高度。 For example, the same markup needs to be used to support the following images: 例如,需要使用相同的标记来支持以下图像:

W x H 400 x 200 400 X 300 400 X 400 宽x高400 x 200400 X 300400 X 400

The Play button needs to be centered in each image. 播放按钮需要在每个图像中居中。

Instead of the inner element being an <img> , you could make it a <div> , styled with the playbutton as the background image, positioned in the center. 代替内部元件作为一个的<img>则可以使它一个<div>与PLAYBUTTON作为背景图像,位于中央的样式。

<div class="media">
  <a class="videoLink" href="#" style="background-image: url(http://cloud.github.com/downloads/malsup/cycle/beach2.jpg);" >
    <div style='background:url(PlayButton.png) center center;' alt="Click to Play" height="200" width="300" />
  </a>
</div>

You'll still need to know the size of the thumbnail image, as you'll still need to supply height and width for the div - since you're displaying the thumbnail as a background image, you won't be able to have the box scale to the right size automatically. 您仍然需要知道缩略图的大小,因为您仍然需要为div提供高度和宽度-由于要将缩略图显示为背景图像,因此无法框会自动缩放到合适的大小。 But at least now your code can set the values for height and width without worrying about the shape of the play button getting distorted. 但是至少现在您的代码可以设置高度和宽度的值,而不必担心播放按钮的形状变形。

(note: the play button as a background image should probably be in a separate stylesheet rather than being declared inline as per my example; I did it like that to demonstrate how it differs from your original code, rather than to show best practice) (注意:播放按钮作为背景图片可能应该在单独的样式表中,而不是按照我的示例声明为内联;我这样做是为了演示它与原始代码的不同之处,而不是为了展示最佳做法)

Need some your CSS to make sure things work, but this may help you: 需要一些CSS来确保一切正常,但这可以帮助您:

.media { 
    display: table; 
}
.media img { 
    display: table-cell; 
    vertical-align: middle; 
    display: block; 
    margin: 0 auto; 
}

If not, please add you CSS so I can Fiddle it and make it happen. 如果没有,请添加您的CSS,这样我就可以摆弄它并实现它。

I'd do it like this. 我会这样

<div class="media">
    <a class="videoLink" href="#"></a>
    <img class="thumbnail" src="http://cloud.github.com/downloads/malsup/cycle/beach2.jpg"/>
</div>

Separate the thumbnail image from the link. 将缩略图与链接分开。 We want the link to appear on top of the image, and the image to stretch the height of the <div class="media"> . 我们希望链接显示在图像的顶部,并且图像要拉伸<div class="media">的高度。

The CSS: CSS:

.media {
    position: relative;
}

.videoLink {
    display: block;
    height: 100%;
    width: 100%;

    background-image: url(PlayButton.png);
    background-position: center center;

    position: absolute;
    z-index: 2;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM