Home > Technology > 鼠标悬浮实现显示留言内容(下)

鼠标悬浮实现显示留言内容(下)

上一篇讲了些基础的东西.没有实质的内容,也许根本找不到你想要的办法,说白了就是扯淡.如果那些你都很明白,那我们可以继续了

关于clone
也许你也看到上一篇的实例了,clone的只是点击的本身的内容,而我们希望的却是克隆的另一个内容.怎么办?由此我们引出了一个新的东西,attr(name).
取得第一个匹配元素的属性值。通过这个方法可以方便地从第一个匹配元素中获取一个属性的值。如果元素没有相应属性,则返回 undefined .

Access a property on the first matched element. This method makes it easy to retrieve a property value from the first matched element. If the element does not have an attribute with such a name, undefined is returned

看完解释,你是否释然了呢,没有?继续一个实例.我们修改一下上一篇提到的第二个例子.改动的地方比较多,注意比较,文档中含有中文,所以建议保存成UTF-8

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script language="javascript">
$().ready(function(){
$('.myclass a').click(function(){
$($(this).attr("href")).clone().insertAfter('.h')});});
</script>
<style type="text/css">
</style>
</head>
<body>
<p id="test">这段文字将会被克隆!</p>
<br/>
<p class="myclass"><a href="#test">test clone </a> (点击克隆上面那段文字)</p>
<p class="h">clone to here</p>
</body>
</html>

好了,我们看到点击链接的时候,段落id为test的文字被克隆了.目的达到.

接下来,我直接把我的悬浮显示留言的代码贴上,你就不难理解.用到的东西其实非常少,不是吗?看代码:

1
2
3
4
5
6
7
8
9
//match and add class
var A=/^#comment-/;
var B=/^@/;
if($('#thecomments .info .content p a').text().match(B) && $('#thecomments .info .content p a').attr('href').match(A)){
$('#thecomments .info .content p').addClass('cool');
};
//clone
$('.cool a').hover(function(){
$($(this).attr("href")).clone().hide().attr("id","").insertAfter($(this).parents("li")).addClass('tip').show('normal');},function(){$(".tip").fadeOut("normal",function(){$(this).remove()})});

这下你明白,我为什么要写两篇,并举例子了吗?这个是循序渐进的,clone的代码一行写完了,我解释一下,用$选择那个我们指定好的链接,hover即鼠标移到它上面的时候执行下面的动作.

看看hover的解释:

一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法。这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态。
当鼠标移动到一个匹配的元素上面时,会触发指定的第一个函数。当鼠标移出这个元素时,会触发指定的第二个函数。而且,会伴随着对鼠标是否仍然处在特定元素中的检测(例如,处在div中的图像),如果是,则会继续保持“悬停”状态,而不触发移出事件(修正了使用mouseout事件的一个常见错误)。
Simulates hovering (moving the mouse on, and off, an object). This is a custom method which provides an ‘in’ to a frequent task.
Whenever the mouse cursor is moved over a matched element, the first specified function is fired. Whenever the mouse moves off of the element, the second specified function fires. Additionally, checks are in place to see if the mouse is still within the specified element itself (for example, an image inside of a div), and if it is, it will continue to ‘hover’, and not move out (a common error in using a mouseout event handler).

另外,对于parents

parents ([expr])
取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素).可以通过一个可选的表达式进行筛选.

hide show fadeOut控制出现和消失的,我还是建议你去看看文档去.也许你会得到更加满意的答案,也许,你还会创建更加有趣的效果.试试吧.有更好玩的记得和我们分享哦.

好了,到此为止,我们的克隆任务算是完成了,如果你还遇到了一些困难,去查查文档试试.不行到这里留言,我们一起讨论.

克隆完成,我们还需要它能跟着鼠标那就更加帅了.如何实现呢?这个我问过Shawn,我给了我看两篇官方的文档,终于我解决了这个.你也可以看看这篇文档.说明很好,还有实例.
好了,我贴出我的完整代码:

1
2
3
4
5
6
7
8
9
//match add class
var A=/^#comment-/;
var B=/^@/;
if($('#thecomments .info .content p a').text().match(B) && $('#thecomments .info .content p a').attr('href').match(A)){
$('#thecomments .info .content p').addClass('cool');
};
//clone
$('.cool a').hover(function(){
$($(this).attr("href")).clone().hide().attr("id","").insertAfter($(this).parents("li")).addClass('tip').show('normal');},function(){$(".tip").fadeOut("normal",function(){$(this).remove()})}).mousemove(function(e){$(".tip").css({left:(e.clientX+5),top:(e.pageY+8)})});

即,在后面加了个mousemove.

另外是对于css的处理(也许你有更好的):
我的代码是:

1
2
3
4
5
6
7
8
9
10
11
12
.tip{
position:absolute;
z-index:999;
border:1px solid #000000;
background-color:#ffffff;
padding:5px 5px;
-moz-border-radius-topleft:10px;
-moz-border-radius-topright:10px;
-moz-border-radius-bottomright:10px;
-moz-border-radius-bottomleft:10px;
width:606px;
}

说明: position:absolute;是关键,它让你复制的那块’浮’出来. -moz-border只是为了能在firefox浏览器上面有圆角的效果.width是为了解决在IE6下面宽度显示不正常而增加的.

后记:
我的实现方法,也可以说是我的学习的过程,当然中途我还看了其他的东西,走了些弯路,就不说了.快要考试,我学习也开始忙了,没有多少时间细细研究这个,权当是个人爱好.
这个所谓的’教程’也许写得很糟糕,也许你还一头雾水,不要急,慢慢来.你可以留言我们一起讨论.
最后感谢Shawn.他那里还有好多好玩的,不过这个学期没有时间研究了.

  1. yinheli Dec 16th, 2008 @ 22:07 | #26

    @kerby
    很不错的提议…不过这段时间没有空弄了.

  2. NetPuter Dec 17th, 2008 @ 13:04 | #27

    哇噻,很像实现这个功能,终于发现方法啦..

  3. 大学生乱弹琴 Dec 17th, 2008 @ 17:11 | #28

    yinheli,高手啊,佩服!佩服!
    预祝期末考出、好成绩~呵呵~

  4. yinheli Dec 17th, 2008 @ 17:51 | #29

    @NetPuter
    你的是嵌套回复的这个功能有点多余了

    @大学生乱弹琴
    谢谢.祝你也考试顺利.

  5. 一宅人生 Dec 17th, 2008 @ 19:28 | #30

    opera下无效,鉴定完毕!

  6. yinheli Dec 17th, 2008 @ 19:53 | #31

    @一宅人生
    由于opera下的问题还没有解决.我暂时特别针对禁用了.

  7. iColor Dec 17th, 2008 @ 20:08 | #32

    @yinheli
    @Shawn
    哦,理解,,,博主这有设置邮件回复吗?,我怎么没收到?

  8. yinheli Dec 17th, 2008 @ 20:12 | #33

    @iColor
    不好意思.我没有设置了.感觉发垃圾邮件…

  9. xiaorsz Dec 18th, 2008 @ 14:48 | #34

    刚发现,你这样是给所有的P都加上 cool 了!!那个判断和下面貌似没什么关系!! :razz:

  10. Zoll Dec 22nd, 2008 @ 17:08 | #35

    我改了下你的这段代码,看看~因为觉得前面加一个变量,似乎有点多余~

    1
    2
    
    $("#comments .body .content p a[href*='#comment-']").addClass('quickshow');
    $('.quickshow').hover(function(){$($(this).attr('href')).clone().hide().removeAttr('id').insertAfter($(this).parents('li')).addClass('tip').fadeIn('normal');},function(){$('.tip').fadeOut('normal',function(){$(this).remove()})}).mousemove(function(e){$('.tip').css({left:(e.clientX+5),top:(e.pageY+8)})});

    是不是简单了点 :grin:
    不过可能出现得问题我也考虑不到~。。

  11. Shawn Dec 22nd, 2008 @ 17:56 | #36

    @Zoll
    有点问题。因为评论中任何含有 #comment- 的链接都被加上 quickshow了,这样会提升 js 的资源占用。

  12. Zoll Dec 22nd, 2008 @ 18:44 | #37

    @Shawn
    事实上也很少出现其他 #comment- 的情况额,这其实是个缩小范围的过程吧。
    但是我的@并没有在链接内部。
    有没有什么别的好办法来缩小范围呢?

    我用yinheli的代码 总是在打开首页的时候 IE提醒我 有错误 attr(…) is null or no object。。进入单篇文章就没事了,我这样改就没有再碰到问题了。。

  13. yinheli Dec 22nd, 2008 @ 18:47 | #38

    @Zoll
    我的貌似没有这个问题?….发现原写的一点也不严谨啊..唉 :mad:
    @xiaorsz
    才发现…囧.刚刚在改呢.

  14. Zoll Dec 22nd, 2008 @ 19:02 | #39

    @yinheli
    我也发现你没有这个问题了。。不知道怎么回事儿!怪异了。。

  15. yinheli Dec 22nd, 2008 @ 19:32 | #40

    Zoll :
    @yinheli
    我也发现你没有这个问题了。。不知道怎么回事儿!怪异了。。

    test

  16. yinheli Dec 22nd, 2008 @ 19:33 | #41

    yinheli :

    Zoll :
    @yinheli
    我也发现你没有这个问题了。。不知道怎么回事儿!怪异了。。

    test

    test again

  17. Zoll Dec 22nd, 2008 @ 21:03 | #42

    晕。。可不可以说是拿我做实验~呵呵

  18. yinheli Dec 22nd, 2008 @ 22:46 | #43

    @Zoll
    我和mg12讨论他的js….我删掉了一些东西还是可以正常使用的…怎么说起来有点绕… 囧.

  19. Zoll Dec 23rd, 2008 @ 01:03 | #44

    @yinheli
    其实我都快把他的js删光了 只剩了一个回复引用还在。。

  20. 吖Bee Jan 2nd, 2009 @ 18:52 | #45

    :cry: 感觉这种方式不错!就是有点不太懂~~

  21. 王老板 Jan 16th, 2009 @ 00:23 | #46

    问个非常菜鸟的问题。 这段代码要加在哪儿?我创建了一个新的JS文件,然后用comments.php 引用的,可是不工作。还是我要直接加在comment.js 的后面?我用的也是INOVE的theme. 不确定正确的方法是什么,还请赐教。 :smile:

  22. yinheli Jan 16th, 2009 @ 13:06 | #47

    @王老板
    你好,这个功能是基于jQuery的。需要先加载框架.

  23. 王老板 Jan 16th, 2009 @ 20:12 | #48

    @yinheli
    谢谢回复!之前也加载了在comments.php里,后来换到了footer.php里。现在可以了! :grin: 似乎找到原因了!好像是和Quick Comments 插件有冲突!我把它关掉后就可以工作了!好奇怪。。。 :sad:

  24. 王老板 Jan 28th, 2009 @ 21:04 | #49

    @yinheli 谢谢!

  25. LiStyle Apr 27th, 2009 @ 01:29 | #50

    我也是使用INOVE的theme. 就是无法工作~纳闷啊~还请赐教。

Submitting Comment, Give me a second...
Comment pages 1 2 3

Leave a comment

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Trackbacks & Pingbacks ( 6 )
  1. Apr 30th, 2009 @ 22:40 | #1
    Pingback: 机会就在你面前-MT ONLINE » iNove 中鼠标悬浮显示 @ 评论
  2. Dec 22nd, 2009 @ 19:05 | #2
    Pingback: iNove 中鼠标悬浮显示 @ 评论 — HuāGuǒShān–老孙的博客
  3. Apr 9th, 2010 @ 12:14 | #3
    Pingback: WordPress 评论模板 – 网页制作1 – 随心笔记 – jQuery – WordPress – 评论 – 网络青年
  4. May 23rd, 2010 @ 19:37 | #4
    Pingback: WordPress 评论模板 – Yin
  5. Jun 15th, 2010 @ 09:08 | #5
    Pingback: 新主题Green living上线测试. | 阿邙’S Blog
  6. Aug 14th, 2010 @ 23:08 | #6
    Pingback: WordPress)非跨页悬浮显示你回复之人的评论内容 | MOPVHS's Blog