jquery通过悬停在另一个元素上来动画一个元素

时间:2011-09-18 18:23:59

标签: jquery hover jquery-animate

我在jquery中有一个成功的(几乎)动画,但现在需要一个文字说“点击”。每当我将鼠标悬停在由过度事件变宽的div上时出现。该事件使得div在进入时变宽,在离开时变窄,这就有效。

所以,我想要的是这样的:

if($( this).width() >= "25") //  if the width is greater than 25
    $("#clickme").css('opacity', 0) // - effect a DIFFERENT id which is my "click.."

当然,在鼠标离开时,请做相反的事情。 到目前为止,我无法让这个工作

我在粘贴箱http://pastebin.com/AQwLeBbc

中有jquery部分

任何线索?

2 个答案:

答案 0 :(得分:0)

如果要将div放在另一个元素上,可以使用css的固定或绝对定位。 您可以通过以下方式获取当前元素的位置:

.offset()

http://api.jquery.com/offset/

然后使用offset()设置悬停div的位置。 如果你想让div悬停在所有东西上,最好把它作为身体的第一个元素。

答案 1 :(得分:0)

这可以实现你想要的。它使用.hover()函数为对象分配两个处理程序,一个在鼠标输入,一个在鼠标输出。

$('.selector').each(function() {
        var clicktext = null;
        $(this).hover(function() {
                var pos = $(this).offset();
                // add the click text and assign it to the proper variable
                // this code can be changed to what ever it takes for your code
                // just make sure to leave the zIndex at a greater than 0 value
                // and the position values as they are. Padding, etc. are optional.
                clicktext = $('<div>').css({
                        zIndex: 1000,
                        padding: "2px",
                        position: "absolute",
                        left: pos.left,
                        top: pos.top
                }).text("click").appendTo('body');
        }, function() {
            clicktext.remove();
        });
});

-Sunjay03