![](/img/trans.png)
[英]How can I make a javascript function work for many expandable cards in html and css
[英]How do I make an expandable box with CSS and javascript?
编辑:我应该指定我希望它是动画的和可伸缩的。 那么,为什么+号不随盒子的其余部分一起移动呢? 为什么它不动画? 我有过渡CSS。
我知道这已经被问过了,但是我尝试编写自己的,但这只是...行不通。 检查jsfiddle以了解我的意思。
我不是想使用太多的Javascript。 我只用它来切换菜单状态。 所有动画都必须是CSS。 现在,唯一让我感到困惑的是为什么这不起作用?
jsfiddle: http : //jsfiddle.net/bjhph51u/1/
HTML:
<div id="expandbox" expanded="false">
<input name="username" type="text" placeholder="Username" disabled></input>
<input name="password" type="password" placeholder="Password" disabled></input>
<button type="submit" disabled>go</button>
</div>
<a href="#" id="plus" onclick="expandBox()">
+
</a>
Javascript:
//expand top box
var box = document.getElementById("expandbox");
function expandBox(){
if(box.getAttribute("expanded")=="false"){
box.setAttribute("expanded","true");
box.style.height="100px";
box.style.display="block";
}else{
box.setAttribute("expanded","false");
box.style.height="0px";
box.style.display="none";
}
}
CSS:
#expandbox {
display:none;
position:fixed;
height:0px;
-webkit-transition:height 0.5s;
transition:height 0.5s;
-o-transition:height 0.5s;
-moz-transition:height 0.5s;
}
请帮助我,在此先感谢!
问题是,您的函数是分配给window.onload
的匿名函数中的局部变量。 在以下位置更改您的提琴选项:Frameworks&Extensions到No wrap - in <body>
。 这样,函数将变为全局函数,您可以很好地调用它。
我已经按如下所示编辑了HTML和JS代码,现在可以正常工作了。 试试看!!
HTML代码:
<div id="expandbox" expanded="false">
<input name="username" type="text" placeholder="Username" disabled></input>
<input name="password" type="password" placeholder="Password" disabled></input>
<button type="submit" disabled>go</button>
</div>
<a href="#" id="plus">+</a>
JS代码:
//expand top box
var box = document.getElementById("expandbox");
function expandBox(){
if(box.getAttribute("expanded")=="false"){
box.setAttribute("expanded","true");
box.style.height="100px";
box.style.display="block";
}else{
box.setAttribute("expanded","false");
box.style.height="0px";
box.style.display="none";
}
}
document.getElementById("plus").onclick = expandBox;
您可以尝试这样的事情:
$('#plus').click(function() {
$('#expandbox').toggle("slow");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.