利用jQuery實現(xiàn)WordPress中@的ID懸浮顯示評論內(nèi)容
比如: A 留言了, B 用 @ 回復(fù)了 A, 所以 B 的回復(fù)可能是這樣的:
@A
How much money do you have?
就是說, 當(dāng)鼠標(biāo)懸停在 @A 上面的時候, 就會將 A 的評論內(nèi)容顯示在一個懸浮區(qū)域中.
實現(xiàn)步驟
在這里我們將以iNove主題為例進(jìn)行講解。
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. 在主題中添加代碼調(diào)用 JavaScript. 打開 templates/end.php, 在 </body> 前面一行添加以下代碼:
(如果你有其他插件或者自己已經(jīng)添加了 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. 好了, 刷新一下有 @ 回復(fù)的頁面, 等頁面加載完, 將鼠標(biāo)懸停在 @ 回復(fù)上, 你會看到效果的.
為什么不能跨頁顯示?
因為其工作原理是, 當(dāng)鼠標(biāo)移動到 @{username} 時在本頁找到對應(yīng)的評論, 并插入到評論列表中, 以絕對位置的方式顯示出來. 如果評論不在本頁, 找不到對象, 當(dāng)然就沒有后面的處理了.
如何跨頁獲取評論信息?
如果本頁找不到對應(yīng)的評論, 可以通過評論的 ID, 用 AJAX 將后臺查詢到的評論信息返回頁面. 當(dāng)鼠標(biāo)移動到 @ 評論上時, 向用戶懸浮顯示 'Loading...' 提示框, 如果操作成功將找到的評論插入評論列表的最后面, 并將該評論的內(nèi)容置換到 'Loading...' 框.
也就是說, 被加載過的評論會一直保留在本頁中, 當(dāng)鼠標(biāo)再次移動到 @ 評論上不用重新加載.
下面我們來看一下針對跨頁評論的處理方法:
在當(dāng)前頁面如何通過 @{username} 找到對應(yīng)評論?
1. 每個評論都會有一個 ID, 結(jié)構(gòu)如: comment-{commentId}, 這本是為了方便通過錨點找到評論, 同時也成為完成 @ 評論提示的必要條件.
2. 每個 @{username} 其實就是指向評論的錨點, 自然可以取得評論 ID.
所以其實很簡單, 如果評論 ID 是 _commentId, 那么在 JS 可以通過以下代碼找到對應(yīng)的評論.
document.getElementById(_commentId);
如果能夠找到目標(biāo)評論, 則創(chuàng)建一個隱藏的臨時評論, 并以目標(biāo)評論作為其內(nèi)容, 在 @{username} 附件將它顯示出來; 如果沒找到目標(biāo)評論, 則通過 ID 到后臺查找對應(yīng)的評論, 進(jìn)行跨頁處理.
如何跨頁加載評論?
跨頁的實質(zhì)是動態(tài)加載評論, 將獲取的評論追加到評論列表最后, 讓評論可以在本頁中找到, 不同的只是這些評論通過 CSS 加工并不會顯示出來.
可以參考下圖. 如果評論不在本頁, 會走紅色路徑, 在評論被加入當(dāng)前頁面之后, 會有一個動作, 將提示框的 Loading 信息替換為評論內(nèi)容. 當(dāng)用戶在此將鼠標(biāo)懸停在這個 @{username} 時, 評論已在當(dāng)前頁面, 所以不需再次加載, 而是走綠色路徑, 直接將評論提示框調(diào)出.
注: 圖中藍(lán)色部分是后臺處理, 黃色部分是整個加載過程的重點.
在后臺中怎樣獲取評論并對其格式化?
這里可以自己寫個方法對評論信息進(jìn)行格式化, 也可以通過評論的回調(diào)方法 (WordPress 2.7 或以上版本可以定義評論的回調(diào)方法) 來獲取格式化的 HTML.
$comment = get_comment($_GET['id']); custom_comments($comment, null,null);
注: custom_comments 是我的回調(diào)函數(shù)的方法名.
JavaScript 代碼
基于 jQuery 的 JS 代碼, 如果不使用或者使用其他 JS frame, 請根據(jù)處理思路自行改造. 建議將代碼放置于評論列表下方.
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');
相關(guān)文章
EasyUI 數(shù)據(jù)表格datagrid列自適應(yīng)內(nèi)容寬度的實現(xiàn)
這篇文章主要介紹了EasyUI 數(shù)據(jù)表格datagrid列自適應(yīng)內(nèi)容寬度的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07jQuery中slideUp 和 slideDown 的點擊事件
這篇文章主要介紹了jQuery中slideUp 和 slideDown 的點擊事件的相關(guān)資料,需要的朋友可以參考下2015-02-02淺談jQuery綁定事件會疊加的解決方法和心得總結(jié)
下面小編就為大家?guī)硪黄獪\談jQuery綁定事件會疊加的解決方法和心得總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10JQuery制作的放大效果的popup對話框(未添加任何jquery plugin)分享
分享一個JQuery制作的放大效果的popup對話框(未添加任何jquery plugin)項目中可以根據(jù)自己的需求來寫css,感興趣的朋友可以了解下哈2013-04-04jQuery中extend函數(shù)的實現(xiàn)原理詳解
這篇文章主要介紹了jQuery中extend函數(shù)的實現(xiàn)原理詳解,extend()函數(shù)用于jQuery插件的開發(fā),本文主要分析它的實現(xiàn)原理,需要的朋友可以參考下2015-02-02