2013-12-15 105 views
-2

嗨,即时通讯使我的背景图像随机出现。但是现在我希望它每30或40秒更换一次图像。现在我的代码如下所示:如何让功能每30秒运行一次?

#background{ 
    position: fixed; 
    height: 100%; 
    width: 100%; 
    z-index: -100px; 
<?php 
    $count = rand(1, 8); 
    $hello = "background: url('img/backImg/". $count ."') center center;"; 
    echo ($hello); 
?> 
    background-repeat:no-repeat; 
    background-color: #d8d8d8; 
    background-size: cover; 
} 

所以我想$count串采取了随机数每30秒。

+1

你需要ajax:http://www.w3schools.com/ajax/或者与php无关的东西。 – bozdoz

+0

您需要ajax或jquery的javascript –

+1

您无法在服务器上使用PHP来做到这一点。您需要发送在浏览器内运行的JavaScript代码。 –

回答

1

jQuery Cycle插件是一个幻灯片插件,支持许多不同类型的过渡效果。它支持暂停悬停,自动停止,自动调整,回调之前/之后,点击触发器等等。

您可以将timeoutdelay设置为30秒。

0

你可以在javascript的帮助下做到这一点。它会调用这个函数每30秒

var myVar=setInterval(function(){myTimer()},30000); 
function myTimer(){ 
    // your code to change the background image url 
    // you can do that with $('#background').attr('style',"your css") or 
    // $('#background').css('background-url',"your path") 
} 
0
<pre> 
<script type="text/javascript"> 
    $(document).ready(function counter() { 
    randomNumber=Math.floor(Math.random() * 8) + 1; //make random number between 1-8 
    $(this).css("background-image", "url('img/backImg/" + randomNumber + ".png')"); 
    setTimeout("counter()",30000); play function counter after 30s 
    });//end the $(document).ready 
</script> 
</pre> 

试试这个代码,我希望它帮你。

这个函数在你的页面准备工作并开始产生一个1-8之间的随机数时启动,然后改变你的背景图像并在30秒后做一个增益 你只需设置你的就可以了。