РЕДАКТИРОВАТЬ: извините за это, я неправильно понял ваш вопрос.
Это должно сделать это, хотя:
ОРИГИНАЛЬНЫЙ ОТВЕТ:
$("#cons").children().each((index, item) => {
let itemToggler = new Toggler(item, 400, '100px', 'no-cursor');
$(item).on('click', event => itemToggler.toggle())
});
function Toggler(item, duration, maxToggleHeight, noCursorClass) {
this.item = item;
this.toggled = false;
this.noCursorClass = noCursorClass;
this.maxToggleHeight = maxToggleHeight;
this.duration = duration;
this.originalHeight = $(item).height();
this.toggle = () => {
$(this.item).addClass(this.noCursorClass);
$(this.item).animate({
height: this.toggled ? this.originalHeight : this.maxToggleHeight
}, {
duration: this.duration,
complete: () => {
$(this.item).removeClass(this.noCursorClass);
this.toggled = !this.toggled;
}
});
}
}
li {
cursor: pointer;
}
.no-cursor {
cursor: not-allowed;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Click on an item</p>
<ul id="cons">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
ОБНОВЛЕННЫЙ ОТВЕТ : в качестве плагина jQuery:
MakePlugin();
$("#cons").children().each((ndx, itm) => {
$(itm).clickToggler({
cursorClass: "cursor-pointer",
noCursorClass: "cursor-not-allowed",
maxToggleHeight: '100px',
duration: 400
});
});
// The plugin is only wrapped in this MakePlugin() function
// so I could put it below the 'main logic' above.
function MakePlugin() {
// Main 'clickToggler' plugin:
(function($) {
$.fn.clickToggler = function(options) {
let settings = $.extend({
toggled: false,
cursorClass: "",
noCursorClass: "",
maxToggleHeight: '10px',
duration: 100
}, {
originalHeight: $(this).height(),
...options
}
);
$(this).on("click", event => {
if(!$(this).is(':animated')) {
$(this).addClass(settings.noCursorClass);
$(this).animate({
height: settings.toggled ? settings.originalHeight : settings.maxToggleHeight
}, {
duration: settings.duration,
complete: () => {
$(this).removeClass(settings.noCursorClass);
settings.toggled = !settings.toggled;
}
});
}
});
return settings.cursorClass
? $(this).addClass(settings.cursorClass)
: this;
};
})(jQuery);
}
.cursor-pointer {
cursor: pointer;
}
.cursor-not-allowed {
cursor: not-allowed;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Click on an item</p>
<ul id="cons">
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>