change background image in body

后端 未结 4 944
情话喂你
情话喂你 2020-11-29 09:22

How do i change the background image in CSS at run time? I have the following background image in body and can the image be changed at run time?

body {
hei         


        
4条回答
  •  天涯浪人
    2020-11-29 09:56

    Just set an onload function on the body:

    
    

    Then do something like this in javascript:

    function init() {
      var someimage = 'changableBackgroudImage';
      document.body.style.background = 'url(img/'+someimage+'.png) no-repeat center center'
    }
    

    You can change the 'someimage' variable to whatever you want depending on some conditions, such as the time of day or something, and that image will be set as the background image.

提交回复
热议问题