2012-11-03 23 views
0

好吧,我在網上看到過很多這方面的問題,但似乎很多問題都在使用PHP,ASP,JQUERY和其他語言。對於我來說,我只對CSS和Java Script有點熟悉,那是我只知道的語言。那麼有人可以向我展示一些關於CSS和Java Script的代碼嗎?如何每次點擊每個鏈接或網址時更改我網站的背景圖片?

無論如何,我也有興趣在網站上的背景,將改變每一個刷新!

在此先感謝!

嘿,我發現了示例代碼,它是完美的工作!

學分這個版面:http://www.daniweb.com/web-development/web-design-html-and-css/threads/228324/load-different-background-image-when-user-refresh

<html> 
<head> 
<title>Untitled</title> 
<script type="text/javascript"> 
function changeImg(imgNumber) { 
var myImages = ["Background/Background.jpg", "Background/Background2.jpg", "Background/Background3.jpg", "Background/Background4.jpg", "Background/Background5", "Background/Background6", "Background/Background7"]; 
var imgShown = document.body.style.backgroundImage; 
var newImgNumber =Math.floor(Math.random()*myImages.length); 
document.body.style.backgroundImage = 'url('+myImages[newImgNumber]+')'; 
} 
window.onload=changeImg; 
</script> 
<style type="text/css"> 
.bg {background-attachment:fixed; background-repeat: no-repeat; background-position:top right;} 
</style> 
</head> 
<body class="bg"> 
<p>Some text</p> 
<!-- put a lot text lines here to see that the background stays fixed. --> 
<p>Some text</p> 
</body> 
</html> 
+1

[你嘗試過什麼?](http://www.whathaveyoutried.com/) –

+1

如果使用刷新一個新的形象,每次,比你殺死你的網站美+你討厭你的用戶通過增加頁面加載時間 –

+0

嗨! Alien先生我的網站通過局域網運行,我的意思是它在共享網絡或計算機內運行。 :D所以基本上頁面加載不是一個問題。這就是我對這項工作感興趣的原因。 – Jayseer

回答

0

大量的解決方案:

存儲所有文件夾中的文件。

  1. 使用索引號索引數據庫表中的圖像。生成一個具有特定範圍的隨機數並顯示對應於隨機數的圖像

  2. 重命名文件夾中的所有圖像(1 - n)。生成隨機數並附加/。

一旦你得到了隨機圖像,你可以輕鬆地在寫背景或類似

document.getElementByTag('body').setAttribute('style','background-image:url('+<your image>+')); 

與JavaScript的任何其他DOM組件您可以用jQuery簡化它。

0

您可以輕鬆地使用JQuery做 - 只是定義click事件每個環節 -

$(function(){ 
    $("link1").click(function(){ 
     $("body").css({background-image:"url('link1_image.gif')"}); 
    }); 
});​ 

有關各隨機圖像負荷刷新就可以直接調用的onload功能,並設置隨機圖像。

$(function(){   
    var images = ['image1.jpg', 'image2.jpg', 'image3.jpg']; 
    $('body').css({'background-image': 'url(images/' + images[Math.floor(Math.random() * images.length)] + ')'}); 
});​ 

希望它能起作用。謝謝。