萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML 腳本教學

使用 iframe 嵌入天氣小工具:完整代碼與樣式示範

透過 <iframe> 嵌入第三方天氣服務是快速添加動態資訊的常用手法。本文提供可直接套用的 CSS 與 HTML 範例,涵蓋多種尺寸與樣式。

基礎 CSS 重置

* { margin:0; padding:0; list-style-type:none; }
a, img { border:0; }
body { font:12px/180% Arial, Helvetica, sans-serif, "新宋體"; }
.demo { width:800px; margin:0 auto; }
.demo div { margin:40px 0 0 0; }

多種天氣樣式 iframe 代碼

來源:https://i.tianqi.com,參數 id 控制樣式,num 控制天數。

<div class="demo">
  <div>
    <iframe name="weather_inc" src="https://i.tianqi.com/index.php?c=code&id=2&num=5"
      width="650" height="70" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
  </div>
  <div>
    <iframe name="weather_inc" src="https://i.tianqi.com/index.php?c=code&id=7"
      style="border:solid 1px red" width="225" height="90" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
  </div>
  <div>
    <iframe name="weather_inc" src="https://i.tianqi.com/index.php?c=code&id=9"
      width="800" height="60" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
  </div>
  <div>
    <iframe name="weather_inc" src="https://i.tianqi.com/index.php?c=code&id=11"
      width="500" height="15" frameborder="0"></iframe>
  </div>
  <div>
    <iframe name="weather_inc" src="https://i.tianqi.com/index.php?c=code&id=13"
      width="650" height="221" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
  </div>
  <div>
    <iframe name="weather_inc" src="https://i.tianqi.com/index.php?c=code&id=19"
      width="800" height="120" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
  </div>
</div>

效果預覽

天氣組件效果圖1

專案實際應用效果

使用提醒

  • 第三方服務可能隨時變更 API 或停止維護,建議自備備援方案。
  • iframe 會阻塞父頁面 onload,考慮加 loading="lazy" 或 JS 動態插入。
  • 跨域限制下無法自定義內部樣式,僅能控制邊框、尺寸等外層屬性。
copyright © 萬盛學電腦網 all rights reserved