I have made some changes to the code:
Removed transition: all .4s ease;
and replaced it with
transition: max-height 0.4s ease-in-out;
Added the following to the CSS:
.tree .node.active > .subitems {
max-height: 1000px;
transition: max-height 0.4s ease-in-out;
}
Note: In the demo below, I have added a setTimeout
in your JavaScript to create an animation effect for the toggler icons. Feel free to remove it if you prefer using the default JavaScript behavior.
Demo
$(function() {
$('#tree .node .toggler').on('click', function() {
var obj = this;
$(this).closest('.node').toggleClass('active').find('.subitems').slideToggle();
if ($(obj).closest('.node').hasClass('active')) {
$(obj).html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm6 13h-5v5h-2v-5h-5v-2h5v-5h-2v5h-5v-2z"/></svg>');
} else {
$(obj).html('<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M12 2c5.514 0 10 4.486 10 10s-4.486 10-10 10-10-4.486-10-10 4.486-10 10-10zm0-2c-6.627 0-12 5.373-12 12s5.373 12 12 12 12-5.373 12-12-5.373-12-12-12zm6 13h-5v5h-2v-5h-5v-2h5v-5h-2v5h-5v-2z"/></svg>');
}
});
});
@import url(https://fonts.googleapis.com/css?family=Roboto);
/* Your existing CSS styles are here */
/* I've only included additional CSS rules that were added above */
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Tree structure</p>
<!-- Rest of the HTML code from your original snippet -->