最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery: hide div on hover, stay visible on click - Stack Overflow

programmeradmin0浏览0评论

I have a couple of position: absolute; "buttons" display:block; <a>'s and a couple of position: absolute; div's with text in them. The div's are hidden by display:none; set to the default.

When you *hover*hover over a button, the div next to it (in the code) should appear (with some kind of fade/scroll effect) and then fade/scroll out again if you move the cursor away from the button.

When you click on a button, the div next to it should stay visible (i.e. display:block;). It should only disappear again if you click on the button or the div itself (hovering over the button or the div shouldn't change anything).

I thought this would be straightforward, but I can't get it to work.

I have a couple of position: absolute; "buttons" display:block; <a>'s and a couple of position: absolute; div's with text in them. The div's are hidden by display:none; set to the default.

When you *hover*hover over a button, the div next to it (in the code) should appear (with some kind of fade/scroll effect) and then fade/scroll out again if you move the cursor away from the button.

When you click on a button, the div next to it should stay visible (i.e. display:block;). It should only disappear again if you click on the button or the div itself (hovering over the button or the div shouldn't change anything).

I thought this would be straightforward, but I can't get it to work.

Share Improve this question edited Aug 19, 2012 at 16:05 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Nov 25, 2010 at 3:40 user367217user367217 4992 gold badges6 silver badges20 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 5

with a little knowledge of your html, here's how I got it.

html

<a href="#" class="mybutton">button</a>
<div class="mydiv">some text in it.</div>

jQuery

$('.mydiv').addClass('hover').click(function(){
     $(this).addClass('hover').fadeOut();
});

$('a.mybutton').click(function() {
    $('.mydiv').toggleClass('hover').show();
    // $('.mydiv').removeClass('hover').show();
}).hover(function() {
    $('.mydiv.hover').fadeIn();
}, function() {
    $('.mydiv.hover').fadeOut();
});

Crazy demo

发布评论

评论列表(0)

  1. 暂无评论