iT邦幫忙

2025 iThome 鐵人賽

DAY 28
0
Vue.js

從零開始的Vue之旅系列 第 28

vue小專案-調整天氣元件樣式

  • 分享至 

  • xImage
  •  

好的今天來調整一下天氣元件在box1的顯示效果
昨天的畫面是這樣
https://ithelp.ithome.com.tw/upload/images/20251010/20178690FYN7aJVFyL.png
我想讓天氣元件有外框陰影增加立體感,且有自己的背景色讓他能更明顯
先到WeatherBox.vue<style scoped>區域定義一個.weather-box

<style scoped>
.weather-box {
  width: 85%;
  height: auto;
  background-color: #E6DDD3;
  border-radius: 12px;
  box-shadow: 0px 4px 8px #C9B7A6;
  padding: 15px;
  margin-top: 15px;
}
</style>

再來要將.weather-box套到元件中,昨天的程式中可以看到已經有使用class來設定樣式了,今天定義的.weather-box只要寫到其中一個區域就好,我們直接套到天氣元件最外層的區域,在最外層的<div>標籤加入.weather-box
這裡附上<template>的程式碼

<template>
  <div class=" weather-box flex flex-col items-center justify-center min-h-screen bg-blue-50 text-gray-800">
    <div class="bg-white shadow-lg rounded-2xl p-6 w-80 text-center">
      <h1 class="text-2xl font-bold mb-4">即時天氣</h1>

      <div v-if="loading" class="text-gray-500">正在取得天氣資料...</div>

      <div v-else-if="error" class="text-red-500">
        {{ error }}
      </div>

      <div v-else>
        <h2 class="text-xl font-semibold mb-2">{{ weather.name }}</h2>
        <img
          :src="`https://openweathermap.org/img/wn/${weather.weather[0].icon}@2x.png`"
          :alt="weather.weather[0].description"
          class="mx-auto"
        />
        <p class="text-lg">{{ weather.weather[0].description }}</p>
        <p class="text-3xl font-bold mt-2">{{ weather.main.temp }}°C</p>
        <p class="text-sm text-gray-500 mt-3">更新於 {{ updateTime }}</p>

      </div>
    </div>
  </div>
</template>

好的用完WeatherBox.vue後,然後到App.vue來處理box1的樣式設定

.box1{
   width: 25%;
   height: 90vh;
   background-color: #EBD8C3;
   border-radius: 12px;
   box-shadow: 0px 4px 8px #C9B7A6;
   display: flex;              /* 使用 flex */
   flex-direction: column;     /* 垂直排列內容 */
   align-items: center;        /* 水平置中 */
}

讓包含的子元素可以垂直的方式排列
align-items: center; 讓元素可以水平置中
目前的畫面
https://ithelp.ithome.com.tw/upload/images/20251010/20178690L6WVt7trPl.png

然後之前有提到的調色設計網頁,我想試試區域的漸層效果,到ColorSpace網頁,點擊上方的gradient,選擇想要的兩個顏色及漸層方向,點擊generate,就會顯示漸層效果及程式碼
將下方程式碼貼到對應的區域就行了
https://ithelp.ithome.com.tw/upload/images/20251010/20178690iHiQqwWaf6.png

.weather-box {
  width: 85%;
  height: auto;
  color: #271306;
  background-image: linear-gradient(to bottom, #d5c6b5, #cfbdad, #b89e89, #a28067, #8c6247);
  border-radius: 12px;
  box-shadow: 0px 4px 8px #C9B7A6;
  padding: 15px;
  margin-top: 15px;
}

https://ithelp.ithome.com.tw/upload/images/20251010/20178690yUklvxDhxg.png

還剩下一些空間,剩下的區域明天來處理
各位明天見~


上一篇
vue小專案-結合OpenWeatherMap API
下一篇
vue小專案-調整天氣元件樣式
系列文
從零開始的Vue之旅30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言