萬盛學電腦網

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

jQuery 實現圖片懸停顯示描述層動畫

效果

滑鼠移入圖片區塊,標題從上滑入、描述從下滑出;移出則反向滑出隱藏。可搭配 jQuery Easing 增加彈性動畫。

核心代碼

$('.item').hover(
    function() { // mouseenter
        $(this).children('div.title').stop().animate({top: 0}, 700, 'easeOutBounce');
        $(this).children('div.desc').stop().animate({bottom: 0}, 700, 'easeOutBounce');
    },
    function() { // mouseleave
        $(this).children('div.title').stop().animate({top: -60}, 500);
        $(this).children('div.desc').stop().animate({bottom: -40}, 400);
    }
);

HTML 結構範例

<div class="item">
    <img src="photo.jpg" alt="">
    <div class="title" style="position:absolute; top:-60px;">標題</div>
    <div class="desc" style="position:absolute; bottom:-40px;">描述內容</div>
</div>

CSS 關鍵點

  • .item { position: relative; overflow: hidden; }
  • 標題/描述絕對定位,初始隱藏在可視區外
  • .stop() 防止動畫排隊疊加
copyright © 萬盛學電腦網 all rights reserved