jQuery hide one div show another

2019-04-17 09:38发布

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.

标签: jquery toggle
3条回答
放荡不羁爱自由
2楼-- · 2019-04-17 09:43

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();
                });
});
查看更多
我命由我不由天
3楼-- · 2019-04-17 09:56
$("#toggle").click(function() {
    $("#ipad-landscape, #ipad-portrait").toggle();
});

Aha, you want fading...

查看更多
相关推荐>>
4楼-- · 2019-04-17 09:58

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);
});
查看更多
登录 后发表回答