admin管理员组文章数量:1290991
I'm currently using the code below found on web tutorial to show/hide DIVs. It works great but don't like the effect. Would like the DIVs to fade in / fade out instead (or something smoother, for the moment the DIVs are growing from the top-right corner). How could I adapt the code to do this? Youc ans ee it here / Many thanks
function showonlyone(thechosenone) {
$('.textzone').each(function(index) {
if ($(this).attr("id") == thechosenone) {
$(this).show(2000);
}
else {
$(this).hide(2000);
}
});
}
I'm currently using the code below found on web tutorial to show/hide DIVs. It works great but don't like the effect. Would like the DIVs to fade in / fade out instead (or something smoother, for the moment the DIVs are growing from the top-right corner). How could I adapt the code to do this? Youc ans ee it here http://jsfiddle/Grek/w4HWn/1/ Many thanks
function showonlyone(thechosenone) {
$('.textzone').each(function(index) {
if ($(this).attr("id") == thechosenone) {
$(this).show(2000);
}
else {
$(this).hide(2000);
}
});
}
Share
Improve this question
edited Aug 12, 2012 at 20:57
Greg
asked Aug 12, 2012 at 20:46
GregGreg
3,06313 gold badges61 silver badges107 bronze badges
4
-
5
Change
.show() .hide()
to.fadeIn() .fadeOut()
– Michael Berkowski Commented Aug 12, 2012 at 20:49 - 1 Are you using an each loop just for finding an element with a specific id? – Ram Commented Aug 12, 2012 at 20:49
- try to get to know the documentation and the lovely search feature on the site api.jquery./fadeOut – Liviu T. Commented Aug 12, 2012 at 21:00
- Thanks, I tried but this is too technical for me at this stage, but will definitely try to improve my js skills – Greg Commented Aug 12, 2012 at 21:02
2 Answers
Reset to default 5Just change .hide()
to .fadeOut()
and .show()
to .fadeIn()
But looking at your example, you could do it much simpler by using data attributes.
Have a look at this example.
You may need absolute positioning or some other technique because the two divs stack up while fading in and out.
You can use fadeIn
and fadeOut
methods, you can also minify the code, try the following:
function showonlyone(thechosenone) {
$('.textzone').fadeOut();
$('#'+thechosenone).fadeIn();
}
As you are using jQuery you can use the jQuery
click
handler:
HTML:
<div class="source-title-box"><span class="activity-title"><a href="#source-region">Our region</a></span></div>
<div class="source-title-box"><span class="activity-title"><a href="#source-oursource">Our source</a></span></div>
jQuery:
$('.activity-title a').click(function(e){
e.preventDefault()
var thechosenone = $(this).attr('href');
$('.textzone').fadeOut(600, function(){
$(thechosenone).fadeIn(600);
});
})
DEMO
本文标签: javascriptHideshow DIVschange current effect to fadeStack Overflow
版权声明:本文标题:javascript - Hideshow DIVs - change current effect to fade - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741522020a2383244.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论