在 WordPress 評論列表中,回覆與引用按鈕可以只在滑鼠懸停時顯示,讓畫面更簡潔。核心做法是先用 CSS 隱藏按鈕,再用 jQuery 的 hover 控制顯示。
將回覆與引用按鈕放在評論區內,並設定 display:none。當滑鼠移入評論項目時淡入顯示,移出時淡出隱藏。
$('li.comment').hover(
function () {
$(this).find('div.comment-act').fadeIn(400);
},
function () {
$(this).find('div.comment-act').fadeOut(400);
}
);
若使用者快速來回移動滑鼠,動畫可能堆疊。可在淡出完成的回呼中停止動畫佇列,避免按鈕持續閃動。
$('li.comment').hover(
function () {
$(this).find('div.comment-act').fadeIn(400);
},
function () {
$(this).find('div.comment-act').fadeOut(400, function () {
$(this).stop(true);
});
}
);
若主題有評論者資訊區塊,也可用相同方式折疊,減少頁面冗長感。
var cmtinfo = jQuery('#cmtinfo');
if (cmtinfo.length > 0) {
var hideinfo = cmtinfo.find('#hide_author_info');
var showinfo = cmtinfo.find('#show_author_info');
var authorinfo = jQuery('#author_info');
authorinfo.hide();
showinfo.click(function () {
jQuery(this).fadeOut(function () {
hideinfo.fadeIn();
});
authorinfo.fadeIn();
});
hideinfo.click(function () {
jQuery(this).fadeOut(function () {
showinfo.fadeIn();
});
authorinfo.fadeOut();
});
}
這類互動能提升可讀性,但也要確保沒有 JavaScript 時,功能仍能以合理方式呈現。