每5秒重新加载背景图片

时间:2014-03-12 19:51:58

标签: javascript php jquery html css

我需要每隔5秒重新加载背景图片,从php文件中提取img-url。像这样:

PHP(我知道如何做php部分):

<?
//mysql connection
//mysql query
echo ('$picurl')
?>

HTML:

<td height="282" style= "background:url(img/frontpage/johana.jpg) no-repeat">

<-- html content -->

</td> 

4 个答案:

答案 0 :(得分:1)

未经测试,但应该采用诀窍(使用JQuery):

   setInterval(fetchImage(), 5000);

    function fetchImage() {

        $.get("/path/to/your.php", function(data){ // "data" is whatever your php script returns
            //console.log(data); // for debugging your php response
            var imageString = 'url(' + data + ') no-repeat'; // Build your background css string
            $('td#yourID').css('background',imageString); // assign the value of imageString to the td's background css property
        });

    }

您的php文件必须返回单个图像路径。

答案 1 :(得分:0)

希望这个帮助:编辑:试试这个:D

<script>
        var slideShowNum = 0;
        var slideShowSources = ["<? echo ('$picurl') ?>", "<? echo ('$picur2') ?>", "<? echo ('$picur3') ?>", "<? echo ('$picur4') ?>"];

        function SlideShowImageChanger() {

            slideShowNum++;
            if (slideShowNum == 4) {
                slideShowNum = 0;
            }

            var imageSrc = slideShowSources[slideShowNum];


            var imageElm = document.getElementById('YourTdId');
            imageElm.style.backgroundImage = "url(" + imageSrc + ")";
        }
</script>


<td height="282" id="YourTdId" style= "background:url(img/frontpage/johana.jpg) no-repeat">

<-- html content -->

</td> 

<script>
setInterval(SlideShowImageChanger, 8000);
</script>

答案 2 :(得分:0)

向运输署提供您可以定位的身份证或类别,然后这样的话会让您继续前进:

setInterval(function() {
    $.ajax({
        url: 'path/to/server/script.php',
        success: function(response) {
            // This assumes the output of your server script is simply
            // an image url
            $('#td-id').css('background-image', response);
        }
    });
}, 5000);

答案 3 :(得分:-1)

你使用的whit jquery:

setInterval(function() {
      // Do something every 2 seconds
}, 2000);

on setinterval执行php页面的异步HTTP(Ajax)请求并使用结果 用http://api.jquery.com/css/设置页面的CSS。