展开子菜单条件(jQuery)
Unfold Submenu conditions (jQuery)
我遇到了一个无法解决的jQuery问题。我创建了一个带有子菜单元素的菜单。我想通过点击菜单项来切换内容的高度。问题是,当我点击其他项目时,内容会崩溃。解释起来有点棘手,我用了两个网站来做这项工作http://www.polerstuff.com/->当你点击"商店",然后在"信息",子菜单保持打开。同样的技巧在这里也可以看到http://topodesigns.com/
我猜这两个网站都在用Shopify。
$(document).ready(function() {
$(".button").on("click", function() {
if($(".content").height() == 0) {
$(".content").animate({height: "300px"});
}
else if($(".content").height() == 300) {
$(".content").animate({height: "0px"});
}
});
});
这是我的护照->提前感谢。 这是您的fiddle的版本,它使用data属性来瞄准具有所需内容的div,以及另一个包含所需高度的数据标签来动画(但还有许多其他方法)。单击相同的按钮将其关闭,这是通过添加指示性类来实现的。"隐藏"div可以根据需要包含更多带有类和布局的div。
$(document).ready(function (){
$(".b").on("click", function (){
var $this = $(this),
target = $this.data('target'),
tall = $this.data('tall'),
content = $(".content");
target = $('.'+target).html(); // get the html content of target divs
content.html(target); // insert said content
if (!$this.hasClass('on')) { // if it hasn't been clicked yet..
$(".b").removeClass('on'); // say that none have been clicked
$this.addClass('on'); // say that just this one has been clicked
content.animate({height: tall}, 200); // animate to the height specified in this buttons data attribute
} else {
content.animate({height: "0px"});
$this.removeClass('on');
}
});
});
.content {
background: coral;
width: 100%;
height: 0px;
overflow:hidden;
}
.hiding{
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button class="b" data-target="alpha" data-tall="4em">Button</button>
<button class="b" data-target="bravo" data-tall="7em">Button</button>
<button class="b" data-target="charlie" data-tall="5em">Button</button>
<div class="content">Le contenu</div>
<div class="hiding alpha"> some stuff </div>
<div class="hiding bravo"> other things </div>
<div class="hiding charlie"> bits and pieces </div>
相关文章:
- jQuery条件为true,但它'it’’我没有犯
- jQuery条件选择问题
- text()的jQuery条件语句(三元)
- 当满足javascript/jquery条件时显示DIV(包括jsFiddle链接)
- JQuery 条件 preventDefault() 在未调用时触发
- jQuery 条件,用于检查类,如果它包含该类,则执行
- 奇怪的Javascript/JQuery条件语句行为
- 检查是否所有输入字段都具有值 jQuery 条件
- Javascript / JQuery 条件替换
- 使用 jQuery 条件更改 CSS 背景
- 将jquery条件is(':checked')转换为布尔数组
- Rails基于父类生成选项,用于jQuery条件隐藏
- 循环多维数组以满足jQuery条件
- jQuery条件差异解析
- jQuery条件延迟
- if的Jquery条件不是鼠标输入
- Jquery条件是鼠标离开还是鼠标进入
- jQuery条件添加和删除CSS属性
- Jquery -条件不工作
- jQuery条件通过