jQuery hide one div show another

﹥>﹥吖頭↗ 提交于 2019-12-06 17:17:56

问题


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);
});


来源:https://stackoverflow.com/questions/4467276/jquery-hide-one-div-show-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!