简体   繁体   English

图片上的标题文字:不再起作用

[英]Caption text over an image: not working anymore

This question is related to a previous post where the problem was temporarily solved: Caption text over an image . 这个问题与先前解决问题的先前文章有关: 图片上方的字幕文字

I had to move my website to a new server where the script doesn't work anymore. 我不得不将我的网站移动到该脚本不再起作用的新服务器上。 The purpose is to load a random image from a folder on my website and to display over the image a related paired text (image1.jpg & text1.txt). 目的是从我网站上的文件夹中加载随机图像,并在图像上显示相关的配对文本(image1.jpg和text1.txt)。

After the migration of the site, nor the images or the text would display. 迁移网站后,将不会显示图像或文本。 I could get the image displayed again by changing this line of code: 通过更改以下代码行,我可以再次显示图像:

xmlhttp.open("GET", caption, false);

->

xmlhttp.open("GET", caption, true);

This has fixed the problem of the images but the attached text is still not displayed. 这已解决了图像问题,但仍未显示附件文本。

The updated JSfiddle is there: http://jsfiddle.net/Totoleheros/ES22a/7/ . 更新的JSfiddle在那里: http : //jsfiddle.net/Totoleheros/ES22a/7/

html: HTML:

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

javascript: JavaScript的:

function fixImage( image )
{
    // change calculations to match your needs
    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() );
var url ="http://www.lvts.fr/Images/RandomPictures/" + rn + ".jpg";
var caption ="http://www.lvts.fr/Images/RandomPictures/" + rn + ".txt";

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

window.onload = function() {    jq('.theCaption').html(xmlhttp.responseText); jq('.fullSize').prop('src',url); };

$mirage(document).ready(function(){
    $mirage('body').unbind('mouseenter');
    var mirageMenuConfig = { sensitivity: 3, interval: 30, over: revealMainMenuChildren, timeout: 500, out: hideMainMenuChildren };
    function revealMainMenuChildren(){  $mirage(this).children("ul").css('opacity','1').slideDown(300); }
    function hideMainMenuChildren(){     $mirage(this).children("ul").fadeTo(300, 0).slideUp(300); }    
    $mirage("#nav ul ul").parent().addClass("ddarrow");
    $mirage("#nav ul ul").parent().append("<span></span>");
    $mirage("#nav ul ul").css({ display: "none" }); 
    $mirage("#nav ul li").hoverIntent(mirageMenuConfig);
});

CSS: CSS:

#showImage {
     display: block;
    height: 331px; width: 200px; 
    visibility: hidden; 
    z-index: 1;
}

.HOLDER { 
    position: relative;
    width:200px;
    margin:0 auto;
}

.theCaption {
    position: absolute; 

    width: 196px; 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: 2;
font-size: 10px;
line-height: 0.9em;
padding-top: 2px;
padding-right: 2px;
padding-bottom: 1px;
padding-left: 2px;
    display: none;
}

.HOLDER:hover .theCaption {display:block;}

.fullSize {
    position: absolute; 
    top: 0px; left: 0px; 
    visibility: hidden;
}

I don't understand why moving the website has generated this issue. 我不明白为什么移动网站会产生此问题。 Any help would be very much appreciated... 任何帮助将不胜感激...

That's because the speed of the connection to the server changed - it got slower. 这是因为与服务器的连接速度发生了变化-速度变慢了。

AJAX request are asynchronous. AJAX请求是异步的。 What happens is that you send the request, read the result and then the response from the server comes in. Use this code instead: 发生的情况是您发送了请求,读取了结果,然后来自服务器的响应进入了。使用以下代码代替:

var caption ="http://www.lvts.fr/Images/RandomPictures/" + rn + ".txt";

$( document ).ready(function() {
    $.ajax(caption, {
        dataType: 'html',
        success: function(data, textStatus, jqXHR) {
            console.log('data', data);
            $('.theCaption').html(data);
        }
    });

    $('.fullSize').prop('src',url);
});

jQuery will prepare everything to call the success callback when the server response comes in. See http://learn.jquery.com/ajax/ 当服务器响应进入时,jQuery将准备一切以调用success回调。请参阅http://learn.jquery.com/ajax/

And you should use $( document ).ready(); 并且您应该使用$( document ).ready(); instead of window.onload Note: For this to work, the document must be served from www.lvts.fr as well or you'll get this error: 而不是window.onload注:对于这项工作,文档必须从担任www.lvts.fr一样好,甚至你会得到这样的错误:

XMLHttpRequest cannot load http://www.lvts.fr/Images/RandomPictures/79.txt. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

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

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