2009-12-23 67 views
1

我如何在asp.net中使用JavaScript更改圖像?我希望這些在一段時間(比如15秒)之後會自動改變(沒有點擊刷新/ F5按鈕),只需在asp.net中使用java腳本。有沒有任何JS可以幫助我做到這一點。如何在使用Javascript的橫幅中旋轉圖像?

感謝

回答

1

嘗試這樣的事情,這樣的例子設置了頁面的背景圖像:

var gSlideshowInterval = 10; /* the interval in seconds */ 
var gImageCapableBrowser; 
var gCurrentImage = 0; /* the default image in the array of images */ 
var randombgs=["images/body/clarinet.jpg", "images/body/jazzsax.jpg", "images/body/composing.jpg", "images/body/classical-music.jpg"]; 


function canManipulateImages() { 
    if (document.images) 
     return true; 
    else 
     return false; 
} 
function loadSlide(imageURL) { 
    document.body.style.backgroundImage='url(\''+ imageURL + '\')'; /* change this line to be the actual image you want to change i.e. document.getElementbyId('MyImage').src or something similar */ 
    return true; 
} 
function nextSlide() { 
    gCurrentImage = Math.floor(Math.random()*randombgs.length); 
    loadSlide(randombgs[gCurrentImage]); 
} 

gImageCapableBrowser = canManipulateImages(); 
setInterval("nextSlide()",gSlideshowInterval * 1000); 
+0

感謝您的及時答覆,但我應該在哪裏調用從這個JS? – 2009-12-23 09:41:41

+0

+1用於添加代碼 – 2009-12-23 10:18:41

+0

如果在頭部添加

1

以前的答案正確的,但我應該做財產以後它其中:

通話方法在身體上,然後

讓函數調用它自我,然後你有一個無限循環的旋轉

var gSlideshowInterval = 10; /* the interval in seconds */ 
var gImageCapableBrowser; 
var gCurrentImage = 0; /* the default image in the array of images */ 
var randombgs=["images/body/clarinet.jpg", "images/body/jazzsax.jpg", "images/body/composing.jpg", "images/body/classical-music.jpg"]; 

    function canManipulateImages() { 
    if (document.images) 
     return true; 
    else 
     return false; 
} 
function loadSlide(imageURL) { 
    document.body.style.backgroundImage='url(\''+ imageURL + '\')'; /* change this line to be the actual image you want to change i.e. document.getElementbyId('MyImage').src or something similar */ 
    return true; 
} 
function nextSlide() { 
    gCurrentImage = Math.floor(Math.random()*randombgs.length); 
    loadSlide(randombgs[gCurrentImage]); 

    // the code changed here 
    setInterval("nextSlide()",gSlideshowInterval * 1000); 

} 

//and this will be call once a page load 
function onBodyLoad() 
{ 
    gImageCapableBrowser = canManipulateImages(); 
    nextSlide(); 
} 


</script> 

,這裏是HTML源代碼

<body onload="onBodyLoad"> 
</body> 
+0

+1用於自動化實際解決方案:) – 2009-12-23 10:19:30