比如: A 留言了, B 用 @ 回復了 A, 所以 B 的回復可能是這樣的:
@A
How much money do you have?
就是說, 當鼠標懸停在 @A 上面的時候, 就會將 A 的評論內容顯示在一個懸浮區域中.
實現步驟
在這里我們將以iNove主題為例進行講解。
1. 將以下代碼保存為commenttips.js:
jQuery(document).ready( function(){ var id=/^#comment-/; var at=/^@/; jQuery('#thecomments li p a').each( function() { if(jQuery(this).attr('href').match(id)&& jQuery(this).text().match(at)) { jQuery(this).addClass('atreply'); } } ); jQuery('.atreply').hover( function() { jQuery(jQuery(this).attr('href')).clone().hide().insertAfter(jQuery(this).parents('li')).attr('id','').addClass('tip').fadeIn(200); }, function() { jQuery('.tip').fadeOut(400, function(){jQuery(this).remove();}); } ); jQuery('.atreply').mousemove( function(e) { jQuery('.tip').css({left:(e.clientX+18),top:(e.pageY+18)}) } ); })
2. 將 commenttips.js 文件放置到 inove/js 目錄.
3. style.css 中追加樣式代碼如下:
#thecomments .tip { background:#FFF; border:1px solid #CCC; width:605px; padding:10px !important; padding:10px 10px 0; margin-top:0; position:absolute; z-index:3;}#thecomments .tip .act { display:none;}*+html #thecomments .tip { padding:10px 10px 0 !important;}
4. 在主題中添加代碼調用 JavaScript. 打開 templates/end.php, 在 </body> 前面一行添加以下代碼:
(如果你有其他插件或者自己已經添加了 jQuery 的庫, 那第一行代碼可以不必添加.)
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/commenttips.js"></script>
5. 好了, 刷新一下有 @ 回復的頁面, 等頁面加載完, 將鼠標懸停在 @ 回復上, 你會看到效果的.
為什么不能跨頁顯示?
因為其工作原理是, 當鼠標移動到 @{username} 時在本頁找到對應的評論, 并插入到評論列表中, 以絕對位置的方式顯示出來. 如果評論不在本頁, 找不到對象, 當然就沒有后面的處理了.
如何跨頁獲取評論信息?
如果本頁找不到對應的評論, 可以通過評論的 ID, 用 AJAX 將后臺查詢到的評論信息返回頁面. 當鼠標移動到 @ 評論上時, 向用戶懸浮顯示 'Loading...' 提示框, 如果操作成功將找到的評論插入評論列表的最后面, 并將該評論的內容置換到 'Loading...' 框.
也就是說, 被加載過的評論會一直保留在本頁中, 當鼠標再次移動到 @ 評論上不用重新加載.
下面我們來看一下針對跨頁評論的處理方法:
在當前頁面如何通過 @{username} 找到對應評論?
1. 每個評論都會有一個 ID, 結構如: comment-{commentId}, 這本是為了方便通過錨點找到評論, 同時也成為完成 @ 評論提示的必要條件.
2. 每個 @{username} 其實就是指向評論的錨點, 自然可以取得評論 ID.
所以其實很簡單, 如果評論 ID 是 _commentId, 那么在 JS 可以通過以下代碼找到對應的評論.
document.getElementById(_commentId);
如果能夠找到目標評論, 則創建一個隱藏的臨時評論, 并以目標評論作為其內容, 在 @{username} 附件將它顯示出來; 如果沒找到目標評論, 則通過 ID 到后臺查找對應的評論, 進行跨頁處理.
如何跨頁加載評論?
跨頁的實質是動態加載評論, 將獲取的評論追加到評論列表最后, 讓評論可以在本頁中找到, 不同的只是這些評論通過 CSS 加工并不會顯示出來.
可以參考下圖. 如果評論不在本頁, 會走紅色路徑, 在評論被加入當前頁面之后, 會有一個動作, 將提示框的 Loading 信息替換為評論內容. 當用戶在此將鼠標懸停在這個 @{username} 時, 評論已在當前頁面, 所以不需再次加載, 而是走綠色路徑, 直接將評論提示框調出.
注: 圖中藍色部分是后臺處理, 黃色部分是整個加載過程的重點.
在后臺中怎樣獲取評論并對其格式化?
這里可以自己寫個方法對評論信息進行格式化, 也可以通過評論的回調方法 (WordPress 2.7 或以上版本可以定義評論的回調方法) 來獲取格式化的 HTML.
$comment = get_comment($_GET['id']);custom_comments($comment, null,null);
注: custom_comments 是我的回調函數的方法名.
JavaScript 代碼
基于 jQuery 的 JS 代碼, 如果不使用或者使用其他 JS frame, 請根據處理思路自行改造. 建議將代碼放置于評論列表下方.
var id=/^#comment-/;var at=/^@/;jQuery('#thecomments li p a').each(function() { if(jQuery(this).attr('href').match(id)&& jQuery(this).text().match(at)) { jQuery(this).addClass('atreply'); }});jQuery('.atreply').hover(function() { var target = this; var _commentId = jQuery(this).attr('href'); if(jQuery(_commentId).is('.comment')) { jQuery('<li class="comment tip"></li>').hide().html(jQuery(_commentId).html()).appendTo(jQuery('#thecomments')); jQuery('#thecomments .tip').css({ left:jQuery().cumulativeOffset(this)[0] + jQuery(this).width() + 10, top:jQuery().cumulativeOffset(this)[1] - 22 }).fadeIn(); } else { var id = _commentId.slice(9); jQuery.ajax({ type: 'GET' ,url: '?action=load_comment&id=' + id ,cache: false ,dataType: 'html' ,contentType: 'application/json; charset=utf-8' ,beforeSend: function(){ jQuery('<li class="comment tip"></li>').hide().html('<p class="ajax-loader msg">Loading...</p>').appendTo(jQuery('#thecomments')); jQuery('#thecomments .tip').css({ left:jQuery().cumulativeOffset(target)[0] + jQuery(target).width() + 10, top:jQuery().cumulativeOffset(target)[1] - 22 }).fadeIn(); } ,success: function(data){ var addedComment = jQuery(data + '</li>'); addedComment.hide().appendTo(jQuery('#thecomments')); jQuery('#thecomments .tip').html(addedComment.html()); } ,error: function(){ jQuery('#thecomments .tip').html('<p class="msg">Oops, failed to load data.</p>'); } }); }}, function() { jQuery('#thecomments .tip').fadeOut(400, function(){ jQuery(this).remove(); });});
PHP 代碼
這段代碼來自PhilNa2 主題, 建議將代碼追加到 function.php.
function load_comment(){ if($_GET['action'] =='load_comment' && $_GET['id'] != ''){ $comment = get_comment($_GET['id']); if(!$comment) { fail(printf('Whoops! Can/'t find the comment with id %1$s', $_GET['id'])); } custom_comments($comment, null,null); die(); }}add_action('init', 'load_comment');