简体   繁体   中英

how to toggle visibility of div on mouseover of another a tag

I will try to be as simple as possible, i am trying to achieve a simple visibility toggle on a div when someone mouseover an a tag, kind of like this the four buttons on this link:

http://www.bt.com/help/home/

now the problem is i want it to appear or want it to be visible on mouseover of a tag, but when once i hide the div it never comes back, i have tried multiple things, some are

$("#function").on("mouseover",this, function () {
            $(this).addClass("show");
        })
        $("#function").on("mouseout",this, function () {
            $(this).removeClass("show");
            $(this).addClass("hide");
        })

Another is:

$("#function").hover(
                  function () {
                    $(this).addClass("hide");
                  },
                  function () {
                    $(this).removeClass("hide");
                  }

        );

and also

$("#butt").on("mouseover", this, function(){
                $(this).find("div#function").show();
                //$("#function").toggleClass("visible");
            });
            $("#butt").on("mouseout", this, function(){
                $(this).find("div#function").hide();
                //$("#function").toggleClass("visible");
            });

You should use mouseenter instead of mouseover. It is because mouseover event will be triggered when you move within the element. Go here and scroll to the bottom to check the different between mouseover and mouseenter. http://api.jquery.com/mouseenter

mouseenter event will be fired only when you entering the element but not move within element.

This is the solution you want. It is almost similar to the site you provided.
JavaScript

<script>
$(document).ready(function()
{
    $("#function").mouseenter(function(event)
    {
        event.stopPropagation()
        $(this).addClass("show");
    }).mouseleave(function(event)
    {
        event.stopPropagation()
        $(this).removeClass("show");
    })
});
</script>

Style

<style>    
.functionBlock { width:200px; height:200px; border:1px solid #CCC; padding:15px;}
.functionBlock ul { visibility: hidden}
.functionBlock.show ul { visibility: visible;}
</style>

HTML

<div id="function" class="functionBlock">
    <h5>Demo </h5>
    <ul>
        <li>APPLE</li>
        <li>SAMSUNG</li>
    </ul>
</div>

Example on jsFiddle http://jsfiddle.net/TAZmt/1/

I got it, slight changes in selectors

$("#butt")
  .mouseover(function () {
    $("#function").show();
  })
  .mouseout(function () {
    $("#function").hide();
  });

This should do it. Check the jsfiddle. The basic idea here is to add a class (.shown) to your root-div on the mouseenter event, this class then makes the hidden <ul> in the div show up due to.

.shown ul{
    display: block !important;
}

http://jsfiddle.net/28bb8/2/

EDIT:

Made some minor css changes, to better reflect the behaviour you're looking for, but you have to change the css to accommodate your own code basically. I hope this helps.

$("document").ready(function(){
    $(".wrap").hover(
        function () {
            $(this).addClass("shown");
       },
       function () {
           $(this).removeClass("shown");
       }
    );
});
$("#link").hover(function(){
    $("#DIV").slideToggle();
});

and the html is

<a href="#" id="link">LINK</a>
<div id="DIV" style="display:none">Your content in it</div>

You don't need Javascript here. This is possible with CSS alone

HTML:

<div class="panel">
    <div class="teaser"><img src="http://lorempixel.com/300/400"/></div>
    <div class="info">
        <ul>
            <li>Go here ...</li>
            <li>Or there ...</li>
        </ul>
    </div>
</div>

CSS:

.panel {
    width: 300px;
    height: 400px;
}

.info {
    display: none;
}

.panel:hover .teaser {
    display: none;
}

.panel:hover .info {
    display: block;
}

And JSFiddle for playing.

i hope this is the solution you're seaching for.

Just place the following code below your <body> tag:

<script type="text/javascript">
function toggle(id) {
   var e = document.getElementById(id);
   if(e.style.display == 'block')
      e.style.display = 'none';
   else
      e.style.display = 'block';
}
</script>

And here is a link and the div which is toggled:

<a href="javascript: return false();" onmouseover="toggle('toggleme');">
    Mouseover this link to toggle the visibility of #toggleme
</a>
<div id="toggleme">This div is "toggled"</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM