单击Jquery而不是悬停
Jquery on click instead of hover
我有一个jQuery代码,它可以在悬停时打开一个手风琴,但我需要在单击时使它工作。相反,我试图将"悬停"更改为"单击",但没有成功,这里有人能帮我吗?
提前谢谢。
$(function() {
$('#accordion > li').hover(function() {
var $this = $(this);
$this.stop().animate({'width':'480px'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
}, function() {
var $this = $(this);
$this.stop().animate({'width':'115px'},1000);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
});
});
Tushar Gupta的想法是唯一一个部分有效的想法,它在点击时打开手风琴,但如果用户在打开选项卡时点击另一个选项卡,就会出现错误。。。
我篡改了整个代码。
http://jsfiddle.net/C8Kp8/<--Tushar Gupta的解决方案
http://jsfiddle.net/SHmuc/<--原始代码
谢谢大家的帮助,真的很感激。
您可以使用.thoggle()或这个
$(function() {
$('#accordion > li').click(function() {
var $this = $(this);
if ($this.hasClass('open')) {
$this.stop().animate({'width':'480px'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
$this.removeClass('open');
}
else {
$this.stop().animate({'width':'115px'},1000);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
$this.addClass('open');
}
});
});
看看这个@Alex的回答很好,但忽略了第一次单击,并且在单击关闭的元素时不会关闭打开的元素FIDDLE。在这个版本中,手风琴元素中的more
链接也起作用。
$(function() {
$('#accordion li').click(function() {
var $this = $(this);
if (!$this.hasClass('open')) {
// open clicked accordion element
$this.stop().animate({'width':'480px'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
// close other open accordion element
$("#accordion li.open").stop().animate({'width':'115px'},1000);
$("#accordion li.open .heading").stop(true, true).fadeIn();
$("#accordion li.open .description, #accordion li.open .bgDescription").stop(true, true).fadeOut();
$("#accordion li.open").removeClass("open");
$this.addClass('open');
}
else {
// close this accordion element
$this.stop().animate({'width':'115px'},1000);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
$this.removeClass('open');
}
});
$('#accordion > li a').click(function(e){
e.stopPropagation();
});
});
相关文章:
- css(或jQuery)悬停时淡入淡出
- jquery快速悬停问题
- jQuery:当屏幕大小改变时,如何更改默认图像和悬停图像
- jQuery悬停在没有鼠标悬停的情况下启动
- 如何使jQuery中的悬停函数单独应用于数据库映像
- 为什么jquery悬停在jsfiddle中可以工作,而在我的html布局中却不能
- jquery悬停和while循环
- 在悬停Jquery/cs3时通过翻转动画更改文本
- 悬停在悬停jquery/javascript中
- 更改元素父元素的背景颜色时停止在
上闪烁鼠标悬停?jQuery/JS. - 在悬停jQuery上保持下拉列表打开
- 使用.on()关闭悬停jquery
- 如何设置属性='数据fancybox组'在滑动悬停jquery
- Wordpress不接受.悬停(jquery)
- 我如何添加文本到悬停jQuery元素
- Img SRC路径给第一个子只在悬停jquery
- 我如何跟踪一个:悬停jquery源代码
- 显示通知直到超时除非悬停(jQuery)
- 改变悬停jquery的一部分Href
- 改变背景位置的悬停- jquery