简体   繁体   中英

jQuery slideToggle to a specific height using .on

I need to do a simple slide toggle when an element is clicked, however the event needs to use the .on event and I also need to only collapse the element to a specific height, whilst allowing for any height when expanded.

In other words I can't figure out how to implement this: https://stackoverflow.com/a/4965021/162642

Into this:

$(document).on("click",'.SomeElement',function(e)
{
    e.preventDefault();
    // toggle here
}

Any help would be greatly appreciated!

Try this:

$(document).on("click", ".SomeElement", function() {
  if($(this).hasClass("toggled")) {
    $(this).animate({"height": "100px"}).removeClass("toggled");
  } else {
    $(this).animate({"height": "50px"}).addClass("toggled");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM