首页 \ 问答 \ 图片上的标题文字(Caption text over an image)

图片上的标题文字(Caption text over an image)

对不起,这是一个新手问题。 我想显示从我的服务器目录中获取的随机图片,我想显示与每个图像相关的文本(同一目录中的txt文件。例如:1.jpg - > 1.txt; 2.jpg - > 2。文本...)。 我希望文本显示在图像上方,放在图像顶部的框架中,只有鼠标悬停在图像上。

可以在此处找到javascript代码,css和html代码:

http://jsfiddle.net/Totoleheros/ES22a/

HTML代码:

<img id="fullSize" onload="fixImage(this)" />
<div id="HOLDER">
<div id="theCaption"></div>
</div>
<img id="showImage" alt="random image" />

CSS代码:

/* The first style is to hide the text*/
#theCaption {
position: absolute;
width: 200px;
height: 331px;
filter:alpha(opacity=0);
-moz-opacity:0;
-khtml-opacity: 0;
opacity: 0;
z-index: 3;
}
/*The send style is to show the text on hover*/
#theCaption:hover, #theCaption:active {
position: absolute;
width: 200px;
height: 40px;
background-color: #eeeeee;
color: #000000;
overflow: hidden;
font-family: arial;
font-weight: normal;
filter:alpha(opacity=80);
-moz-opacity:0.8;
-khtml-opacity: 0.8;
opacity: 0.8;
z-index: 3;
font-size: 10px;
line-height: 0.9em;
padding-top: 2px;
padding-right: 2px;
padding-bottom: 1px;
padding-left: 2px;
}

javascript代码:

function fixImage(image) {
// I want an to display an image of 200x331px taken from a directory of image of various dimensions
var show = document.getElementById("showImage");
if (image.height > image.width) {
    show.style.height = "331px";
    show.style.width = Math.round((image.width / image.height) * 331) + "px";
} else {
    show.style.width = "200px";
    show.style.height = Math.round((image.height / image.width) * 200) + "px";
}

show.src = image.src;
show.style.visibility = "visible";
}

var MAXPICTURENUMBER = 166; // or whatever you choose
var rn = 1 + Math.floor(MAXPICTURENUMBER * Math.random());
// Here I point to the directory containing the images and the caption
var url = "http://www.test.fr/Images/RandomPictures/" + rn + ".jpg";
var caption = "http://www.test.fr/Images/RandomPictures/" + rn + ".txt";

var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", caption, false);
xmlhttp.send();
captionText = xmlhttp.responseText;

window.onload = function () {
document.getElementById("theCaption").innerHTML = captionText;
document.getElementById("fullSize").src = url;
}

我确信有一个比我使用的更清洁/更智能的解决方案,但请记住,我是一个新手。

我的问题:这几乎是有效的,除了文本有时会根据鼠标位置消失。 我该如何解决这个问题?

Thkx提前为您提供帮助!!


I am sorry, this a newbie question. I want to display random pictures taken from a directory of my server and I want to display a text associated to each image (txt files in the same directory. Example: 1.jpg -> 1.txt; 2.jpg -> 2.txt...). I want that the text appear above the image, in a frame placed on the top of the image, only upon mouse hover on the image.

The javascript code, css and html codes can be found here:

http://jsfiddle.net/Totoleheros/ES22a/

html code:

<img id="fullSize" onload="fixImage(this)" />
<div id="HOLDER">
<div id="theCaption"></div>
</div>
<img id="showImage" alt="random image" />

CSS code:

/* The first style is to hide the text*/
#theCaption {
position: absolute;
width: 200px;
height: 331px;
filter:alpha(opacity=0);
-moz-opacity:0;
-khtml-opacity: 0;
opacity: 0;
z-index: 3;
}
/*The send style is to show the text on hover*/
#theCaption:hover, #theCaption:active {
position: absolute;
width: 200px;
height: 40px;
background-color: #eeeeee;
color: #000000;
overflow: hidden;
font-family: arial;
font-weight: normal;
filter:alpha(opacity=80);
-moz-opacity:0.8;
-khtml-opacity: 0.8;
opacity: 0.8;
z-index: 3;
font-size: 10px;
line-height: 0.9em;
padding-top: 2px;
padding-right: 2px;
padding-bottom: 1px;
padding-left: 2px;
}

javascript code:

function fixImage(image) {
// I want an to display an image of 200x331px taken from a directory of image of various dimensions
var show = document.getElementById("showImage");
if (image.height > image.width) {
    show.style.height = "331px";
    show.style.width = Math.round((image.width / image.height) * 331) + "px";
} else {
    show.style.width = "200px";
    show.style.height = Math.round((image.height / image.width) * 200) + "px";
}

show.src = image.src;
show.style.visibility = "visible";
}

var MAXPICTURENUMBER = 166; // or whatever you choose
var rn = 1 + Math.floor(MAXPICTURENUMBER * Math.random());
// Here I point to the directory containing the images and the caption
var url = "http://www.test.fr/Images/RandomPictures/" + rn + ".jpg";
var caption = "http://www.test.fr/Images/RandomPictures/" + rn + ".txt";

var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", caption, false);
xmlhttp.send();
captionText = xmlhttp.responseText;

window.onload = function () {
document.getElementById("theCaption").innerHTML = captionText;
document.getElementById("fullSize").src = url;
}

I am sure that there is a much cleaner/smarter solution than the one I am using, but remember, I'm a newbie.

My issue: this is almost working except that the text sometimes disappear according to the mouse position. How can I fix that?

Thkx in advance for your help!!


原文:https://stackoverflow.com/questions/21120486
更新时间:2023-05-11 06:05

最满意答案

f2通过引用来引用它的参数,这实质上是你传递的参数的别名 。 指针和引用的区别在于引用不能为NULL。 使用f你需要传递你传递给指针的参数的地址 (使用&运算符),当你通过引用传递时,只需传递参数并创建别名。

如果不打算更改函数内部的参数,并且在打算更改它们时,使用非const引用,则通过const引用( const double& ref )传递是首选。

当你需要能够将NULL传递给你的参数时,主要使用指针,显然你需要在函数内部检查指针是否为NULL然后再使用它。


f2 is taking it's arguments by reference, which is essentially an alias for the arguments you pass. The difference between pointer and reference is that a reference cannot be NULL. With the f you need to pass the address (using & operator) of the parameters you're passing to the pointer, where when you pass by reference you just pass the parameters and the alias is created.

Passing by const reference (const double& ref) is preferred when you are not going to change the arguments inside the function, and when you are going to change them, use non-const reference.

Pointers are mostly used when you need to be able to pass NULL to your parameters, obviously you'd need to check then inside your function if the pointer was not NULL before using it.

相关问答

更多
  • f2通过引用来引用它的参数,这实质上是你传递的参数的别名 。 指针和引用的区别在于引用不能为NULL。 使用f你需要传递你传递给指针的参数的地址 (使用&运算符),当你通过引用传递时,只需传递参数并创建别名。 如果不打算更改函数内部的参数,并且在打算更改它们时,使用非const引用,则通过const引用( const double& ref )传递是首选。 当你需要能够将NULL传递给你的参数时,主要使用指针,显然你需要在函数内部检查指针是否为NULL然后再使用它。 f2 is taking it's ar ...
  • 这看起来很明显,但是您是否仔细检查了是否实际上在ISBN.cpp包含了头文件? 也许,你不小心使用了与两个头文件包括相同的预处理常量,导致文件的ISBN声明被有效忽略? 你发布的片段对我来说看起来很好...... This might seem obvious, but have you double-checked that you actually include the header file in ISBN.cpp? Maybe, you have accidentally used the sam ...
  • 功能Tower *createTower(string name, Tower *link); 返回指向Tower对象的指针。 函数名前面的星号只是指定返回类型是指针。 你也可以用这种方式编写函数定义,但是(在我看来)更清楚: Tower* createTower(string name, Tower* link); The function Tower *createTower(string name, Tower *link); returns a pointer to a Tower object. ...
  • 尝试: static ifstream myfile(input_file.c_str()); 由于某种原因, ifstream构造函数不接受std::string 。 Try: static ifstream myfile(input_file.c_str()); For some reason, the ifstream constructor doesn't accept an std::string.
  • 实现glade_project_get_type的代码位于库libgladeui ,该库是单独编译的并与glade可执行文件链接。 libgladeui的源代码与glade的源代码libgladeui发布。 函数glade_project_get_type的定义位于文件glade-project.c 。 您将在该文件中找不到字符串glade_project_get_type ,因为该函数的实际代码是复杂宏扩展的结果,来自以下glade_project_get_type行: G_DEFINE_TYPE_WIT ...
  • 这是旧式的K&R C.不确定它是否适用于C ++。 This is old style K&R C. Not sure it will work for C++.
  • put std::vector computeFailFunction(const string& pattern); 在int KMPmatch(const string& text, const string& pattern) 。 或者将函数声明放入头文件并包含在源文件中,这就是具有多个源文件的项目。 Put std::vector computeFailFunction(const string& pattern); ahead of int KMPmatch(const str ...
  • 这是需要细节才能准确回答的问题之一。 应该有很小的区别,特别是如果编译器能够内联func1或func2以避免额外的调用开销(也就是说,函数以这样的方式声明它们可以被编译器从调用代码“看到”) 。 唯一一次存在较大差异的是,如果你可以避免构造/破坏intermediate_vec - 例如你在循环中重复使用相同的那个(但很可能你在这种情况下“重置”向量,这就是破坏矢量的主要部分)。 差异如何,作为总时间的一部分,取决于func1实际上做了什么。 但是,与所有性能问题一样,有必要通过性能基准来比较两者。 确保使 ...
  • 我对同一类型的许多论点的经验是,当我试图记住他们的顺序时,它很快变成令人沮丧和难以理解的混乱。 其他语言(如Python)可以选择在将参数传递给函数时命名参数,但不幸的是我们在C ++中没有这样的快乐。 一个很好的解决方法是参数结构。 使用公共变量(或等效的getter / setter函数,但我通常不会发现它是必要的),最终用户可以在将结构传递给函数之前以清晰的方式设置所需的字段: 例如: struct calc_args { string name; string arguments; ...
  • 虽然您可以使用可变参数模板或类似的东西,但我建议只使用变量库,例如boost::variant ,保持简单: #include #include #include int Testing(std::initializer_list args) { for(const auto &arg : args) { std::cout << "Arg type: " << ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。