圖片依序顯示
HTML
<div id="wrapper">
<div id="mainmenu">
<img src="http://placehold.it/30x30">
<img src="http://placehold.it/30x30">
<img src="http://placehold.it/30x30">
<img src="http://placehold.it/30x30">
</div>
<img id="menuhover" src=http://placehold.it/5x20>
</div>
CSS
#wrapper {
position: relative;
float: right; /* 靠右,隨便靠 */
right: 10%; /* 隨心設定 */
width: 210px; /* 所有MENU小圖示都可容納顯示即可 */
height: 90px; /* 若沒設定高度,滑鼠移過時 MENU 會變高,造成下方區塊內容往下移 */
border: 1px solid black; /* 可省略 */
}
#mainmenu img {
position: relative; /* 方便你取得各 image 相對位置用 */
overflow: hidden;
}
#menuhover {
position: relative;
display: none;
}
JavaScript
var aryPosX = [];
$('#mainmenu img').each(function(){
aryPosX.push($(this).position().left);
});
$('#mainmenu img').mouseenter(function(){
var idx = $(this).index(); // 取得 img 在 #mainmenu 的位置順序,值為 0、1、2、3,配合從 aryPosX 陣列內取值
$('#menuhover')
.show()
.stop(true, false) // 注意這裡的 stop 設定
.animate({ 'left': aryPosX[idx] });
});