萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

jQuery 仿騰訊微博滑出式每日天氣卡片效果

效果描述

固定寬度的天氣主按鈕,滑鼠懸停時右側內容面板從左向右滑出(像抽屜),移開滑回隱藏,動畫流暢,類似騰訊微博側邊欄互動。

結構與樣式

<div class="weather">
  <div class="weather_main"><strong><a>深圳</a></strong></div>
  <div class="alert">
    <p>如果雲知道,多想涼風來做伴~</p>
    <p><a>提醒好友</a></p>
  </div>
</div>

<style>
.weather { width:500px; height:100px; margin:0 auto; position:relative; overflow:hidden; color:#fff; text-align:center; }
.weather_main { width:99px; border-right:1px solid #fff; float:left; background:#F03; position:relative; z-index:2; }
.weather_main strong { line-height:100px; }
.alert { width:400px; height:100px; position:absolute; left:-400px; top:0; background:#F03; }
</style>

jQuery 動畫核心

$(function() {
  $('.weather_main').hover(
    function() { $('.alert').animate({ left: '100px' }, 1000); },
    function() { $('.alert').animate({ left: '-400px' }, 1000); }
  );
});
  • .animate({left: '100px'}, 1000):1 秒內從 left:-400px 滑至 left:100px(緊貼主按鈕右側)。
  • hover(enter, leave):進入/離開分別觸發展開/收合。
  • stop() 可防止快速進出導致動畫排隊(建議加上)。

擴充應用

  • 內容區載入即時天氣 API 資料(溫度、濕度、風向、未來 3 天預報)。
  • 加入「設為預設城市」Cookie 記憶。
  • 響應式:螢幕寬度 < 600px 時改為點擊切換、全寬彈出。
copyright © 萬盛學電腦網 all rights reserved