這是一個很實用的小效果:滑鼠移入列表項時,右側縮圖往左挪動幾像素;移出後自動復位。位移幅度不大,卻能給使用者明確的「這裡可以點」的回饋。
兩個關鍵點。第一,事件改用 jQuery 1.7 之後推薦的 on() 綁定,並用鏈式寫法一次掛上 mouseenter 與 mouseleave。第二,動畫前先呼叫 stop(true),避免快速連續滑動時動畫堆疊、出現縮圖「追不上」的錯亂。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>jQuery實現圖片列表鼠標移入微動</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body, div, li, p, img, a { margin: 0; padding: 0; }
.hovertreecontainer { width: 370px; margin: 50px auto; }
.hovertreecontainer a { text-decoration: none; }
.hovertree-item-box {
height: 120px; width: 185px; position: relative;
padding: 10px; box-sizing: border-box; float: left;
}
.hovertree-item-box .title { width: 80px; height: 100%; color: #4998a1; font-size: 14px; }
.hovertree-item-box.odd { border-bottom: 1px solid #CCC; border-right: 1px solid #CCC; }
.hovertree-item-box.even { border-bottom: 1px solid #CCC; }
.hovertree-item-box.nobottom { border-bottom: none; }
.hovertree-item-box .hovertree-img-box {
width: 80px; height: 80px; overflow: hidden;
position: absolute; right: 10px; bottom: 5px;
}
.hovertree-img-box img { width: 100%; height: 100%; }
.hovertreecontainer:after { content: ""; display: block; clear: both; }
</style>
</head>
<body>
<div class="hovertreecontainer">
<h2>jQuery實現圖片列表鼠標移入微動</h2>
<a href="https://hovertree.com/texiao/css/20/">
<div class="hovertree-item-box odd">
<div class="title">春節對聯</div>
<div class="hovertree-img-box"><img src="https://hovertree.com/hvtimg/201512/o9qashmi.gif"></div>
</div>
</a>
<a href="https://hovertree.com/hvtart/bjae/a6w6e2qg.htm">
<div class="hovertree-item-box even">
<div class="title">下雨天</div>
<div class="hovertree-img-box"><img src="https://hovertree.com/hvtimg/201512/f748s0ko.jpg"></div>
</div>
</a>
<a href="https://hovertree.com/h/bjae/0st5ww13.htm">
<div class="hovertree-item-box odd">
<div class="title">磨砂玻璃</div>
<div class="hovertree-img-box"><img src="https://hovertree.com/hvtimg/201512/agagq0or.jpg"></div>
</div>
</a>
<a href="https://hovertree.com/menu/texiao/">
<div class="hovertree-item-box even">
<div class="title">網頁特效</div>
<div class="hovertree-img-box"><img src="https://hovertree.com/hvtimg/201512/r51a22uy.gif"></div>
</div>
</a>
<a href="https://hovertree.com/h/bjaf/hwqtjwjs.htm">
<div class="hovertree-item-box odd nobottom">
<div class="title">何問起統計文件數</div>
<div class="hovertree-img-box"><img src="https://hovertree.com/hvtimg/bjafjd/iofopnro.png"></div>
</div>
</a>
<a href="https://hovertree.com/h/bjaf/hovertreeimg.htm">
<div class="hovertree-item-box even nobottom">
<div class="title">HovertreeImg</div>
<div class="hovertree-img-box"><img src="https://hovertree.com/hvtimg/201601/p3t2ldyr.png"></div>
</div>
</a>
</div>
<script type="text/javascript" src="https://down.hovertree.com/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(function () {
$('.hovertreecontainer .hovertree-item-box')
.on('mouseenter', function () {
var oImgBox = $(this).find('.hovertree-img-box');
$(oImgBox).stop(true).animate({ right: '20px' }, "normal");
})
.on('mouseleave', function () {
var oImgBox = $(this).find('.hovertree-img-box');
$(oImgBox).stop(true).animate({ right: '10px' }, "normal");
});
});
</script>
</body>
</html>
縮圖容器用絕對定位(position: absolute)配合 right 值,動畫本體就是在改這個 right。列表清一色浮動,因此容器最後用 :after 偽元素清除浮動,避免高度塌陷。
邊框的處理用 odd / even / nobottom 三個 class 控制,讓網格看起來是完整的表格線而不會出現缺角。若列表項數量固定,這種寫法比 nth-child 更保險。
最後,animate 的第二個參數用字串 "normal" 也可以,若想統一節奏,改成具體毫秒數(例如 200)會更好維護。