0

I want to make a text box or an area on my website, and I have about 9 buttons on the page which I don't want to redirect to other pages and open on the same page and in the same Text Box or Area that I create.

So, if someone clicks on Button 1, then opens Text 1 and its picture(s) with fades in animation, and when clicks on Button 2, Text 1 Box fades out and Text 2 with picture(s) fades in and etc. How can I do this?

I have tried some codes like this

$('.one').hide();
$('.two').hide();
$('.three').hide();

$('.btn1').click(function () {
    $('.one').fadeIn();
});

$('.btn2').click(function () {
    $('.one').fadeOut();
    $('.two').fadeIn();

});

$('.btn3').click(function () {
    $('.two').fadeOut();
    $('.three').fadeIn();
});

Fiddle

but when clicking the buttons, the Buttons' position is not fixed and moves with the text animation.

Is there any way to do the same with other codes?

Ahmad Adibzad
  • 501
  • 2
  • 6
  • 14
Aria
  • 75
  • 1
  • 9
  • http://jsfiddle.net/91jfvqwm/2/ – Aria May 12 '15 at 07:33
  • You can use something like this: https://jqueryui.com/tabs/ – Brane May 12 '15 at 07:40
  • possible duplicate of [jQuery hide element while preserving its space in page layout](http://stackoverflow.com/questions/6393632/jquery-hide-element-while-preserving-its-space-in-page-layout) – ozil May 12 '15 at 07:47

3 Answers3

2
$('.one').hide();
$('.two').hide();
$('.three').hide();
$('.btnall').hide();
$('.btn1').click(function () {
    $.when($('.btnall').fadeOut()).done(function(){
    $('.one').fadeIn();
   });
});

$('.btn2').click(function () {
    $.when($('.btnall').fadeOut()).done(function(){
         $('.two').fadeIn();
    });


});

$('.btn3').click(function () {
   $.when($('.btnall').fadeOut()).done(function(){
    $('.three').fadeIn();
   });
});

I think this is what you need. Updated Fiddle

Tuhin
  • 3,335
  • 2
  • 16
  • 27
1

More scalable and dynamic solution:

$('button').on('click', function () {
    var targetEl = $(this).data('target');
    $.when($('.' + targetEl).siblings('input').fadeOut()).done(function () {
        $('.' + targetEl).fadeIn();
    });
});

HTML:

<input class="one" value="Text 1">
<input class="two" value="Text 2">
<input class="three" value="Text 3">
<br>
<button data-target="one" class="btn1">Button 1</button>
<br>
<button data-target="two" class="btn2">Button 2</button>
<br>
<button data-target="three" class="btn3">Button 3</button>

Demo: https://jsfiddle.net/tusharj/91jfvqwm/8/

Tushar
  • 85,780
  • 21
  • 159
  • 179
0

You can wait fadeOut to complete before fading in new text field. for example

$('.btn1').click(function () {
    $('.btnall').fadeOut('slow', function() {
    $('.one').fadeIn();
    });
});
miltos
  • 1,009
  • 1
  • 6
  • 10