Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jQuery: Problems with Hover and Image Map

Posted on 2011-09-27
1
Medium Priority
?
508 Views
Last Modified: 2012-05-12
In the example below, a flower is displayed when you hover over the red bar.

The problem is that if you hover over the flower, the image slides up so you can no longer see the flower.

I only want the image to slide up if you stop hovering over the red bar or flower.
<!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>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

    $('#selection p a').hover(function() {
        $('#selection, #selection p').animate({
            height: '200px'
        }, 500);

    });

    $('#selection p').hover(function() {}, function(e) {

        $('#selection, #selection p').animate({
            height: '50px'
        }, 200);
    });

});
</script>

<style type="text/css">

#selection {
height: 50px;
overflow: hidden;
position: relative;
padding: 0; 
magin: 0;
}

#selection p {
height: 50px;
overflow: hidden;
position: relative;
padding: 0; 
magin: 0;
}

#selection p a {
display: block;
border: 2px solid green;
position: absolute;
left: 5px;
top: 5px;
height: 20px;
width: 240px;
background: red;
padding: 0; 
magin: 0;
cursor: pointer;
}

</style>

</head>
<body>

<div id="selection">
 <p>
  <a></a>
  <img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Lithospermum_arvense_W.jpg/250px-Lithospermum_arvense_W.jpg" usemap="#xyz" alt=""/>
 </p>
</div>

<map name="xyz" id="xyz">
  <area shape="rect" coords="0,51,250,100" href="a.htm" alt="a" />
  <area shape="rect" coords="101,51,250,200" href="a.htm" alt="b" />
</map>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
1 Comment
 
LVL 1

Accepted Solution

by:
danbaugher earned 2000 total points
ID: 36714262
The problem is that you're using the hover event which is actually two different events mouseenter and mouseleave. Because the image is not a child of the anchor tag you actually mouseleave the red bar which causes the image to shrink back down. Instead of using hover bind to the mouseenter only for the red bar to grow the image and bind mouseleave to the wrapper div to shrink it back down.

<!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>Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {

    $('#selection a').mouseenter(function() {
        $('#selection').animate({
            height: '200px'
        }, 500);

    });

   $('#selection').mouseleave(function(e) {

        $('#selection').animate({
            height: '50px'
        }, 200);
    });

});
</script>

<style type="text/css">

#selection {
height: 50px;
width:250px;
overflow: hidden;
position: relative;
padding: 0; 
magin: 0;
}

#selection a {
display: block;
border: 2px solid green;
position: absolute;
left: 5px;
top: 5px;
height: 20px;
width: 235px;
background: red;
padding: 0; 
magin: 0;
cursor: pointer;
color:#ddd;
text-align:center;
font:normal 12px/20px arial;
text-decoration:none;
}
#selection a:hover{
	color:#fff;
	background:lightcoral;
}


</style>

</head>
<body>

<div id="selection">
  <a title="dummy link" href="#">Text Here</a>
  <img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Lithospermum_arvense_W.jpg/250px-Lithospermum_arvense_W.jpg" usemap="#xyz" alt=""/>
  <map name="xyz" id="xyz">
    <area shape="rect" coords="0,51,250,100" href="a.htm" alt="a" />
    <area shape="rect" coords="101,51,250,200" href="a.htm" alt="b" />
  </map>
</div>



</body>
</html>

Open in new window

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question