我有一个动态添加高度的div,名为.spacer
,现在我想改变.spacer
的高度,将40px
添加到当前动态高度,当切换时,将在动态.spacer
高度上添加和删除40px。
<div class="spacer"></div> -- Shows an empty div which is then populated with a dynamic height.
let spacer = $(".spacer");
$(".profile-header-wrapper .search-icon").on("click", function(){
fixedVideo.toggleClass("fixed-video-position");
spacer.css("height", (spacer + 50) + "px").toggle();
});
1条答案
按热度按时间mzaanser1#
您要将高度设置为
spacer + 50
,而spacer实际上是一个jQuery元素,您应该使用spacer.height() + 50
。您更正了摘录:
现在我假设您使用“toggle”,因为元素是隐藏的,在隐藏的元素上,
spacer.height()
可能返回空,您必须从CSS中获取它(使用parseInt(spacer.css("height").replace("px",""))
行中的内容)。