需要帮助,通过相同的点击链接点击和关闭动画。这是我的Javascript:
$(function() {
// global functions
var dash = $('#Dashboard');
var dashBtn = $('#dashClick');
var state = dash.css({
"top":600
});
var clicked = dashBtn.click(function(e) {
e.preventDefault();
if(clicked) {
dash.animate({"top":0});
}
if(state > 0 && clicked) {
dash.animate({"top":600});
}
});
//make it height of document
dash.height($(document).height());
});
和下面的HtML显示了对带有ID的javascript的引用:
<a id="dashClick" href="#">Dashboard</a>
<div id="Dashboard">
<h2 class="dashTitle">Project Information</h2>
<div class="dashInnerAdd">
<p>
Project name: CSS3 Effects N' Stuff
My github is: https://github.com/Amechi101/css3effects
</p>
</div>
</div>
</main> <!-- end grid main-->
</div>
<!--end wrap -->
答案 0 :(得分:1)
除其他外(参见所有更改的代码):
如果你想使用top
,你可能希望它的行为与绝对定位一样。要做到这一点,您需要一个相对定位在#Dashboard
附近的容器。此外,您的javascript动画需要px值。 "top":600
应该是top:"600px"
。
HTML:
<a id="dashClick" href="#">Dashboard</a>
<div class="container">
<div id="Dashboard">
<h2 class="dashTitle">Project Information</h2>
<div class="dashInnerAdd">
<p>
Project name: CSS3 Effects N' Stuff
My github is: https://github.com/Amechi101/css3effects
</p>
</div>
</div>
</div>
JS:
$(function() {
// global functions
var dash = $('#Dashboard');
var dashBtn = $('#dashClick');
var clicked = false;
dashBtn.click(function(e) {
e.preventDefault();
if(clicked === true) {
clicked = false;
dash.animate({top:"0px"});
} else {
clicked = true;
dash.animate({top:"600px"});
}
});
//make it height of document
dash.height($(document).height());
});
和一些css:
.container {
position: relative;
}
#Dashboard {
position: absolute;
top: 0;
}
小提琴:http://jsfiddle.net/SQK78/2/
如果您不需要绝对定位,您只需将top
更改为marginTop
,就可以删除container
包装器以及那个小提琴中的所有css。
答案 1 :(得分:0)
您的主要JavaScript只执行一次。您需要从单击处理程序中跟踪状态。试试这个:
$(function() {
// global functions
var dash = $('#Dashboard');
var dashBtn = $('#dashClick');
var state = true;
dashBtn.click(function(e) {
state = !state;
e.preventDefault();
if(state) {
dash.animate({"top":20});
}
if(!state) {
dash.animate({"top":600});
}
});
//make it height of document
dash.height($(document).height());
});