问题
Need some help on a script I am doing... what I want to accomplish is when you hover over any a class of 'test' the #socializethis div will show. When you mouseout it will disappear. I have accomplished that, however, I need it to stay focused while actually over #socializethis as well so the box does not disappear.
Second thing I wish to do and have no idea how to go about is determine the position of the a class 'test' so I can show the box right beside it, under it, etc.
Im going to try and use this for a social sharing script.
index.html
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="share.js"></script>
<link rel="stylesheet" href="share.css" type="text/css" />
</head>
<body>
<div id="social">
<a class="test" href="#">share this</a>
</div>
</body>
</html>
share.css
img {border: none;}
#socializethis{
height:37px;
width:260px;
position:absolute;
bottom:500px;
right:900px;
overflow:hidden;
display:none;
visibility:hidden;
padding:3px 3px 3px 3px;
/* CSS3 */
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-moz-box-shadow: 0 0 3px 3px #555;
-webkit-box-shadow: 0 0 3px 3px #555;
box-shadow: 0 0 3px 3px #555;
}
#socializethis a{
float:left;
width:32px;
margin:3px 3px 3px 3px;
padding:0;
}
#socializethis span{
float:left;
margin:3px 3px 3px 3px;
text-shadow: 1px 1px 1px #FFF;
color:#444;
font-size:12px;
}
share.js
(function( $ ) {
$(document).ready(function() {
var twit = 'http://twitter.com/home?status='+title+'%20'+url;
var facebook = 'http://www.facebook.com/sharer.php?u='+url
var digg = 'http://digg.com/submit?phase=2&url='+url+'&title='+title;
var stumbleupon = 'http://stumbleupon.com/submit?url='+url+'&title='+title;
var buzz = 'http://www.google.com/reader/link?url='+url+'&title='+title+'&srcURL='+host;
var delicious = 'http://del.icio.us/post?url='+url+'&title='+title;
var tbar = '<div id="socializethis"><span>Share<br /><a href="#min" id="minimize" title="Minimize"><img src="images/minimize.png" /> </a></span><div id="sicons">';
tbar += '<a href="'+twit+'" id="twit" title="Share on twitter"><img src="images/twitter.png" alt="Share on Twitter" width="32" height="32" /></a>';
tbar += '<a href="'+facebook+'" id="facebook" title="Share on Facebook"><img src="images/facebook.png" alt="Share on facebook" width="32" height="32" /></a>';
tbar += '<a href="'+digg+'" id="digg" title="Share on Digg"><img src="images/digg.png" alt="Share on Digg" width="32" height="32" /></a>';
tbar += '<a href="'+stumbleupon+'" id="stumbleupon" title="Share on Stumbleupon"><img src="images/stumbleupon.png" alt="Share on Stumbleupon" width="32" height="32" /></a>';
tbar += '<a href="'+delicious+'" id="delicious" title="Share on Del.icio.us"><img src="images/delicious.png" alt="Share on Delicious" width="32" height="32" /></a>';
tbar += '<a href="'+buzz+'" id="buzz" title="Share on Buzz"><img src="images/google-buzz.png" alt="Share on Buzz" width="32" height="32" /></a>';
tbar += '</div></div>';
// Add the share tool bar.
$('#social').append(tbar);
$('#socializethis').css({opacity: 1});
// hover
$('a.test').bind('mouseover',function(){
$('#socializethis').animate({opacity: 1}, 600);
$('#socializethis').css({display:'inline', visibility: 'visible'});
});
$('#socializethis').bind('mouseover',function(){
$('#socializethis').animate({opacity: 1}, 600);
$('#socializethis').css({display:'inline', visibility: 'visible'});
});
//leave
$('a.test').bind('mouseout',function(){
$('#socializethis').animate({opacity: 0}, 600, function(){
$('#socializethis').css({display:'none', visibility: 'hidden'});
});
}, 600);
});
})(jQuery);
回答1:
To answer your first question - use jQuery's mouseenter and mouseleave instead of mouseover and mouseout. Mouseenter will allow children to trigger their parent's mouseenter event. The jQuery page http://api.jquery.com/mouseenter/ has a good demo at the bottom showing the difference. Then put both test and socialize elements in a parent div, which while socialize is hidden, will look only like test. This way mouseleave will not be called unless the user leaves both elements.
来源:https://stackoverflow.com/questions/7006504/need-assistance-on-script-jquery