admin管理员组文章数量:1415673
I've recently implemented a small box on my website at the bottom of the page to expand when the mouse hovers over it... This is the code and it all works great.
CSS
#box{
position:absolute;
width:300px;
height:20px;
left: 33%;
right: 33%;
min-width: 32%;
bottom:0;
background-color: #353535;
}
javascript
$('#box').hover(function() {
$(this).animate({
height: '220px'
}, 150);
},function() {
$(this).animate({
height: '20px'
}, 500);
});
But I'm curious about how I would go about changing this to open and close on a click rather than the mouse hovering over it?
I've edited it to...
$('#box').click(function() {
$(this).animate({
height: '220px'
}, 150);
},function() {
$(this).animate({
height: '20px'
}, 500);
});
And this works to open the box. But I can't get it to close again with another click.
So close yet so far! :P
I've recently implemented a small box on my website at the bottom of the page to expand when the mouse hovers over it... This is the code and it all works great.
CSS
#box{
position:absolute;
width:300px;
height:20px;
left: 33%;
right: 33%;
min-width: 32%;
bottom:0;
background-color: #353535;
}
javascript
$('#box').hover(function() {
$(this).animate({
height: '220px'
}, 150);
},function() {
$(this).animate({
height: '20px'
}, 500);
});
But I'm curious about how I would go about changing this to open and close on a click rather than the mouse hovering over it?
I've edited it to...
$('#box').click(function() {
$(this).animate({
height: '220px'
}, 150);
},function() {
$(this).animate({
height: '20px'
}, 500);
});
And this works to open the box. But I can't get it to close again with another click.
So close yet so far! :P
Share Improve this question edited Feb 9, 2011 at 12:58 000 2801 gold badge2 silver badges11 bronze badges asked Feb 9, 2011 at 10:55 FlickdrawFlickdraw 6837 gold badges14 silver badges25 bronze badges 2- What library are you using? Is it possible that the hover function expects two parameters and the click function expects only one? – awm Commented Feb 9, 2011 at 11:00
- I see you've now added the jQuery tag... good! – awm Commented Feb 10, 2011 at 7:25
3 Answers
Reset to default 2this should work
$('#box').toggle(function() {
$(this).animate({
height: '220px'
}, 150);
},function() {
$(this).animate({
height: '20px'
}, 500);
});
You can probably just use the toggle event handler instead of the click event like so:
$('#box').toggle(function() {...}, function() {...});
You can do:
$('#box').click(function() {
if ($(this).is(':visible')) {
$(this).hide();
return;
}
$(this).animate({height: '220px'}, 150);
});
On the click, it will hide the element if visible else animate it.
本文标签: javascriptChanging from hover to clickStack Overflow
版权声明:本文标题:javascript - Changing from hover to click? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745195418a2647110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论