簡體   English   中英

使用javascript在頁面加載時加載css隱藏圖像作為背景圖像

[英]Using javascript to load a css hidden image as a background image on page loading

Noob在javascript並且沒有運氣搜索。 是否可以使用javascript加載已經在html(隱藏css可見性)主體中的圖像,以便在頁面加載時作為div中的背景圖像可見?

是。

只是喜歡

<script type="text/javascript">

      window.onload = function() {

          // get the image url from the src attribute of image
          imgurl =  document.getElementById("yourImgID").src;
          // set it as background to the div you want
          document.getElementById("yourDivID").style.background = "url("+imgurl+") no-repeat";

      }

</script>

你可以在這里看到一個樣本小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM