用CSS实现图像说明

读者: 1554    发布时间: 2007

原文: Styled Images with Caption

Here is an interesting problem that keeps cropping up. How do you balance the need for easy update by web editors with the desire to make a site as visually appealing as possible? Take for example the images that website owners inevitably want to add to their site via a content management system.

Don't forget to check out Headscape first! ;)

The problem is a simple one. A client wants to add an image to their site via the content management system. They want it to look attractive, not appear too boxy (let us say they want a nice rounded corner, as this is all the rage) and have a nice caption underneath it. However, they do not know how to use an image editor (beyond basic resizing) or how to edit HTML.

What would be great is if they could just add a normal everyday image using the img tag, add a title tag including the caption and then it magically styled itself. Well by combining CSS and DOM scripting, I have managed to get this working.

Of course, I am not the best scripter in the world so if you can improve on the code below then please let me know by posting a comment.

Step One: The HTML

The website owner adds the image resized to the appropriate dimensions. Notice they have added img tag contains a caption in the form of a title tag and a class name of "imgRight" (something easy to add with a WYSIWYG editor like Xstandard or contribute). They have also set the width and height of the image. This is important from a styling point of view later.

<img src="/images/foo.jpg" alt="Description of picture" width="200" height="147" class="imgRight" title="The caption would go here" />

Step Two: The DOM Script

The script I have created does the following:

  • Finds all images with the class "imgRight" or "imgLeft"
  • Loops through each one extracting the title tag and inserting it into a new p tag it has created
  • It then effectively wraps the img tag in a div and inserts the p caption after the image
  • It removes the class name from the img and adds it instead to the div.
  • It also uses the width of the image as the width of the div. This prevents the caption expanding beyond the width of the image.
  • Finally it adds an additional span tag that we are going to use later to create the rounded corner.

Just to keep the code a little more streamlined I use the getElementsByClassName function created by Robert Nyman so don't forget to include that in your javascript file.

function addCaption(xClass) {
var allImages = getElementsByClassName(document, "img", xClass);
for ( var i=0; i < allImages.length; i++) {
var imageCaption = document.createTextNode(allImages[i].title);
var imageContainer = document.createElement("div");
var imagePara = document.createElement("p");
var imageWidth = allImages[i].getAttribute("width");
var spareSpan = document.createElement("span");
imagePara.appendChild(imageCaption);
allImages[i].parentNode.insertBefore(imageContainer, allImages[i]);
imageContainer.appendChild(allImages[i]);
if ( allImages[i].title != "" ) {
imageContainer.appendChild(imagePara);
}
imageContainer.appendChild(spareSpan);
imageContainer.className = xClass
spareSpan.className = "spareSpan"
allImages[i].className = "img"
imageContainer.style.width = imageWidth + "px";

}
}

// Runs all the listed functions on the loading of the window

window.onload=function(){
addCaption("imgLeft");
addCaption("imgRight");
}

Step Three: Add the styling

Once the Javacript has run it should output the following HTML which we can now style:

<div style="width: 200px;" class="imgRight">
<img src="/images/foo.jpg" alt="Description of picture" width="200" height="147" class="img" title="The caption would go here" />
<p>The caption would go here</p>
<span class="spareSpan"></span>
</div>

Obviously, you can style this in whatever way you want but some basic styling might look like this:

.imgRight {
float:right;
margin:0.5em 0 1em 1em;
position:relative;
}

.imgLeft {
float:left;
margin:0.5em 1em 1em 0;
position:relative;
}

.imgRight p, .imgLeft p {
font-size:0.9em;
color:#FFFFFF;
margin:0;
background-color:#4D6D80;
padding:0.5em;
}

.spareSpan {
position:absolute;
top:0;
right:0;
display:block;
width:17px;
height:17px;
background:url(/images/curvedCorner.png);
}

This styling basically absolutely positions the sparespan in the top right corner and adds a nice curve to it while at the same time applying some styling to the caption.

So there you have it. Still very much a work in progress but I would very much like the feedback of the coders out there who are more knowledgeable about such things.


Click here for a very basic working example

译文: 用CSS实现图像说明

我们经常会遇到一个有趣的问题:网站内容要丰富,而网站的版面又要新颖,那么怎样才能很好的将二者平衡呢?如果网站需要通过内容管理系统来增加图象。那么一定要先Headscape来看一下,很有帮助呢!

其实很简单。网站想要通过内管理系统来增加图像,通常他们想要的是一个吸引的,看起来并不呆板的图像(就是说他们想要圆角图像,因为这样比较流行),还要在图像的下面加上一些说明。可是,他们并不知道如何使用图像编辑工具,或者如何编辑HTML。

如果可以使用img 标签来添加图像,那该多好!添加一个包含文字说明的小标签,然后让它神奇地自动编辑。
只要把CSS和DOM 脚本结合起来就可以实现!当然,所以如果你可以改进下面的代码,请给我写一下注释评论。

步骤一:HTML

网站编辑先把图像调整到适当大小。添加一个包含文字说明的img标签,命名为"imgRight"(它可以利用类似Xstandard或者  contribute的WYSIWYG 编辑器进行添加)。还可以设置图像的宽度和高度,这点对于图像样式很重要。

步骤二:DOM脚本

以下就是我创建的脚本:
利用"imgRight" 或者 "imgLeft"找到所有要编辑的图像。
提取标签并把它嵌入到另外一个新建立的p 标签里。
然后将img 标签隐藏到div里,在图像的后面嵌入p 标签的文字说明。
将类别名从img里移除,把它添加到div里。
图像的宽度与div的宽度保持一致。这样可以避免文字说明部分超出图像的范围。
最后添加一个span 标签,用来创建圆角。

为了改进这份代码我使用了Robert Nyman 创建的getElementsByClassName功能,所以千万不用忘记在javascript文档里把它们加进去哦!

function addCaption(xClass) {
var allImages = getElementsByClassName(document, "img", xClass);
for ( var i=0; i < allImages.length; i++) {
var imageCaption = document.createTextNode(allImages[i].title);
var imageContainer = document.createElement("div");
var imagePara = document.createElement("p");
var imageWidth = allImages[i].getAttribute("width");
var spareSpan = document.createElement("span");
imagePara.appendChild(imageCaption);
allImages[i].parentNode.insertBefore(imageContainer, allImages[i]);
imageContainer.appendChild(allImages[i]);
if ( allImages[i].title != "" ) {
imageContainer.appendChild(imagePara);
}
imageContainer.appendChild(spareSpan);
imageContainer.className = xClass
spareSpan.className = "spareSpan"
allImages[i].className = "img"
imageContainer.style.width = imageWidth + "px";

}
}
// Runs all the listed functions on the loading of the window
window.onload=function(){
addCaption("imgLeft");
addCaption("imgRight");
}

步骤三:添加样式

如果通过Javacript可以输出以下的HTML:
<div style="width: 200px;" class="imgRight">
<img src="/images/foo.jpg" alt="Description of picture" width="200" height="147" class="img" title="The caption would go here" />
<p>The caption would go here</p>
<span class="spareSpan"></span>
</div>
你可以任意设置样式,但是还是需要有一些基本的样式,如下:
.imgRight {
float:right;
margin:0.5em 0 1em 1em;
position:relative;
}
.imgLeft {
float:left;
margin:0.5em 1em 1em 0;
position:relative;
}
.imgRight p, .imgLeft p {
font-size:0.9em;
color:#FFFFFF;
margin:0;
background-color:#4D6D80;
padding:0.5em;
}
.spareSpan {
position:absolute;
top:0;
right:0;
display:block;
width:17px;
height:17px;
background:url(/images/curvedCorner.png);
}

这种样式主要试固定右上角的位置,在上面添加一个漂亮的曲线,并且给文字说明添加一些样式。
大概就这些啦。非常希望那些更专业的人士能够给我提供一些建议。

到这里看一下操作过程吧!