2016-11-15 192 views
3

每当我点击“click hier”时,类“cirkel1”的div和类“bewegendetekst”的div显示出来。但是当我想点击“点击”时,它们不会消失。jQuery在点击时隐藏多个div

我试着使用切换功能,但这只会让事情变得更糟。

所以我的问题是我怎么做带班“cirkel1”与类格“bewegendetekst”股利消失,当我点击“点击后退”

$(document).ready(function() { 
 
    $(".blok1").click(function() { 
 
    $(".blok2").toggle("slow"); 
 
    $(".blok3").toggle("slow"); 
 
    $(".blok4").toggle("slow"); 
 
    }); 
 
    $(".blok1").click(function() { 
 
    if ($(this).width() != 500) 
 
     $(this).animate({ 
 
     width: 500 
 
     }, 1000); 
 
    else 
 
     $(this).animate({ 
 
     width: 250 
 
     }, 1000); 
 
    }); 
 
    $(".blok1").click(function() { 
 
    if ($(this).height() != 500) 
 
     $(this).animate({ 
 
     height: 500 
 
     }, 1000); 
 
    else 
 
     $(this).animate({ 
 
     height: 250 
 
     }, 1000); 
 
    }); 
 
}); 
 

 
$(document).ready(function() { 
 
    $("p").on("click", function() { 
 
    var el = $(this); 
 
    setTimeout(function() { 
 
     if (el.text() == el.data("text-swap")) { 
 
     el.text(el.data("text-original")); 
 
     } else { 
 
     el.data("text-original", el.text()); 
 
     el.text(el.data("text-swap")); 
 
     } 
 
    }, 1000); 
 
    }); 
 
}); 
 

 

 
$(document).ready(function() { 
 
    $(".cirkel1").click(function() { 
 
    $(".bewegendetekst").show("slow"); 
 
    }); 
 
}); 
 

 
$(document).ready(function() { 
 
    $(".witte-tekst").click(function() { 
 
    $(".cirkel1").show("slow"); 
 
    }); 
 
    $(".cirkel1").click(function(e) { 
 
    e.stopPropagation(); 
 
    $(".cirkel1").hide("slow"); 
 
    }); 
 
});
.rij1 { 
 
    display: flex; 
 
    width: 500px; 
 
} 
 
.rij2 { 
 
    display: flex; 
 
    width: 500px; 
 
} 
 
.blok1 { 
 
    background-color: cadetblue; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.blok2 { 
 
    background-color: palevioletred; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.blok3 { 
 
    background-color: darkseagreen; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.blok4 { 
 
    background-color: coral; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.witte-tekst { 
 
    color: #fff; 
 
} 
 
.cirkel1 { 
 
    border-radius: 50%; 
 
    background-color: #000; 
 
    height: 125px; 
 
    width: 125px; 
 
    position: absolute; 
 
} 
 
.bewegendetekst { 
 
    color: #fff; 
 
    height: 125px; 
 
    width: 250px; 
 
    background-color: #000; 
 
    padding: 30px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div class='rij1'> 
 
    <div class='blok1'> 
 
    <p class='witte-tekst' data-text-swap="Click Back">Click hier</p> 
 
    <div class='cirkel1' style="display:none"></div> 
 
    <p class='bewegendetekst' style="display:none">Met Jquery is het ook mogelijk om verschillende animaties toetepassen kijk maar naar deze cirkel</p> 
 
    </div> 
 
    <div class='blok2'></div> 
 
</div> 
 

 
<div class='rij2'> 
 
    <div class='blok3'></div> 
 
    <div class='blok4'></div> 
 
</div>

+1

为什么有这么多'$(文件) .ready(function(){'? – Monasha

+0

你在滥用像id这样的类,你应该参考演员你所有的CSS和JS。 “点击”是什么意思? – sandrooco

+0

@KevinKloet它确实,它只是第一个没有的重载。 – sandrooco

回答

1

使用jQuery的toggle()方法show/hide圈当你点击Click Back按钮:

$(".witte-tekst").click(function() { 
    $(".cirkel1").toggle("slow"); 
}) 

注意:一个就绪功能就足够了。

希望这会有所帮助。

$(document).ready(function() { 
 
    $(".blok1").click(function() { 
 
    $(".blok2").toggle("slow"); 
 
    $(".blok3").toggle("slow"); 
 
    $(".blok4").toggle("slow"); 
 
    }); 
 
    $(".blok1").click(function() { 
 
    if ($(this).width() != 500) 
 
     $(this).animate({ 
 
     width: 500 
 
     }, 1000); 
 
    else 
 
     $(this).animate({ 
 
     width: 250 
 
     }, 1000); 
 
    }); 
 
    $(".blok1").click(function() { 
 
    if ($(this).height() != 500) 
 
     $(this).animate({ 
 
     height: 500 
 
     }, 1000); 
 
    else 
 
     $(this).animate({ 
 
     height: 250 
 
     }, 1000); 
 
    }); 
 

 
    $("p").on("click", function() { 
 
    var el = $(this); 
 
    setTimeout(function() { 
 
     if (el.text() == el.data("text-swap")) { 
 
     el.text(el.data("text-original")); 
 
     } else { 
 
     el.data("text-original", el.text()); 
 
     el.text(el.data("text-swap")); 
 
     } 
 
    }, 1000); 
 
    }); 
 

 

 
    $(".cirkel1").click(function() { 
 
    $(".bewegendetekst").show("slow"); 
 
    }); 
 

 
    $(".witte-tekst").click(function() { 
 
    $(".cirkel1").toggle("slow"); 
 
    }); 
 
    $(".cirkel1").click(function(e) { 
 
    e.stopPropagation(); 
 
    $(".cirkel1").hide("slow"); 
 
    }); 
 
});
.rij1 { 
 
    display: flex; 
 
    width: 500px; 
 
} 
 
.rij2 { 
 
    display: flex; 
 
    width: 500px; 
 
} 
 
.blok1 { 
 
    background-color: cadetblue; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.blok2 { 
 
    background-color: palevioletred; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.blok3 { 
 
    background-color: darkseagreen; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.blok4 { 
 
    background-color: coral; 
 
    height: 250px; 
 
    width: 250px; 
 
} 
 
.witte-tekst { 
 
    color: #fff; 
 
} 
 
.cirkel1 { 
 
    border-radius: 50%; 
 
    background-color: #000; 
 
    height: 125px; 
 
    width: 125px; 
 
    position: absolute; 
 
} 
 
.bewegendetekst { 
 
    color: #fff; 
 
    height: 125px; 
 
    width: 250px; 
 
    background-color: #000; 
 
    padding: 30px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div class='rij1'> 
 
    <div class='blok1'> 
 
    <p class='witte-tekst' data-text-swap="Click Back">Click hier</p> 
 
    <div class='cirkel1' style="display:none"></div> 
 
    <p class='bewegendetekst' style="display:none">Met Jquery is het ook mogelijk om verschillende animaties toetepassen kijk maar naar deze cirkel</p> 
 
    </div> 
 
    <div class='blok2'></div> 
 
</div> 
 

 
<div class='rij2'> 
 
    <div class='blok3'></div> 
 
    <div class='blok4'></div> 
 
</div>

+0

这不是你要找的答案吗? –

0

隐藏()函数使用隐藏像

$('#yourdiv').hide(); 
0

它会再次隐藏点击

$(".witte-tekst").click(function() { 
     $(".cirkel1").toggle("slow"); 
});