jQuery hide one div show another

2019-04-17 09:52发布

问题:

I have the following website: http://driz.co.uk/taschen/

I'm wanting to make it so when a user clicks the toggle div the currently shown ipad is faded out and the other ipad is faded in. And vice-versa.

How do I do this using jQuery? Thanks.

回答1:

You can do it by using .toggle() to alternate between functions, like this:

$("#toggle").toggle(function() {
  $("#ipad-portrait").fadeOut(function() { $("#ipad-landscape").fadeIn(); });
}, function() {
  $("#ipad-landscape").fadeOut(function() { $("#ipad-portrait").fadeIn(); });
});

Or, using .click() (there are a few ways, this is one of them):

$("#toggle").click(function() {
  var ipads = $("#ipad-portrait, #ipad-landscape"), 
      current = ipads.filter(":visible").fadeOut(function() {
                  ipads.not(current).fadeIn();
                });
});


回答2:

$("#toggle").click(function() {
    $("#ipad-landscape, #ipad-portrait").toggle();
});

Aha, you want fading...



回答3:

You can use the fadeToggle() function added in jQuery 1.4.4 (You need to update to this version from the version 1.4.2 script you're using on that page):

$('#toggle').click(function(){
    $('#ipad-portrait, #ipad-landscape').fadeToggle(300);
});


标签: jquery toggle