如何重置为原始值
How to reset to original values?
看起来每次点击都会添加一个新的newHeight和一个newDistance,我试图在顶部保存一个全局变量的原始高度,并使用数据来保存,但我得到了奇怪的结果,基本上,我应该能够将newDistance和newHeight重置为之前的第一个原始值,以点击运行批量,但它没有,每次点击都会得到新的添加值,从而破坏我的布局:
talents = $(".talenti");
filter = $(".filtra");
genHeight = $("#container").data($("#container").height());
filter.click(function(e) {
e.preventDefault();
if (talents.hasClass("opened")) {
$(".nasco").slideToggle();
$("#wrapNav").slideToggle("10", "linear");
talents.removeClass('opened');
filter.addClass('opened');
$("#container").css("height", genHeight);
} else {
filter.addClass('opened');
};
if (filter.hasClass("opened")) {
$("#wrapNav").slideToggle("10", "linear", function(){
$("#sliding-navigation").slideToggle();
var newHeight = $("#container").height() + $("#wrapNav").outerHeight(true);
var newDistance = newHeight - $("#container").height() + 22;
$("#container").animate({height: newHeight}, 50,function(){
$(".box").animate({top: newDistance});
});
});
}
});
talents.click(function(e) {
e.preventDefault();
if (filter.hasClass("opened")) {
$("#sliding-navigation").slideToggle();
$("#wrapNav").slideToggle("10", "linear");
filter.removeClass('opened');
talents.addClass('opened');
$("#container").css("height", genHeight);
} else {
talens.addClass('opened');
};
if (talents.hasClass("opened")) {
$("#wrapNav").slideToggle("10", "linear", function(){
$(".nasco").slideToggle();
var newHeight = $("#container").height() + $("#wrapNav").outerHeight(true);
var newDistance = newHeight - $("#container").height() + 156;
$("#container").animate({height: newHeight}, 50,function(){
$(".box").animate({top: newDistance});
});
});
}
});
有人吗?
因此,基于大约20分钟前我可以从您的测试站点下载的代码,我成功地使用了以下代码:
$(document).ready(function(){
// placeholder to contain the original height...
var original_height = 0;
talents = $(".talenti");
filter = $(".filtra");
filter.click(function(e){
e.preventDefault();
if (filter.hasClass('opened')){
filter.removeClass('opened');
// toggle the wrapping, just with a zero top coordinate...
$("#wrapNav").slideToggle("10", "linear", function(){
$("#sliding-navigation").hide();
$(".box").animate({top: '0px'});
});
// reset to the original height...
$("#container").height(original_height);
}
else {
// get the original height if it's not already set...
if (original_height == 0)
original_height = $("#container").height();
filter.addClass('opened');
if (talents.hasClass("opened"))
{
$(".nasco").hide();
$("#wrapNav").slideToggle();
talents.removeClass('opened');
}
// toggle the wrapping with a height of the nav as top coordinate...
$("#wrapNav").slideToggle("10", "linear", function(){
$("#sliding-navigation").slideToggle(true, function(){
// need the height of the nav before we know how far to move the boxes...
var newHeight = $("#wrapNav").outerHeight(true);
$(".box").animate({top: newHeight});
// set the container's new height, much like you had...
$("#container").height(original_height + newHeight);
});
});
}
});
talents.click(function(e) {
e.preventDefault();
if (talents.hasClass('opened')) {
talents.removeClass('opened');
// toggle the wrapping, just with a zero top coordinate...
$("#wrapNav").slideToggle("10", "linear", function(){
$(".nasco").hide();
$(".box").animate({top: '0px'});
});
// reset to the original height...
$("#container").height(original_height);
}
else {
// get the original height if it's not already set...
if (original_height == 0)
original_height = $("#container").height();
talents.addClass('opened');
if (filter.hasClass("opened"))
{
$("#sliding-navigation").hide();
$("#wrapNav").slideToggle();
filter.removeClass('opened');
}
// toggle the wrapping with a height of the nav as top coordinate...
$("#wrapNav").slideToggle("10", "linear", function(){
// need the height of the nav before we know how far to move the boxes...
$(".nasco").slideToggle(true, function(){
var newHeight = $("#wrapNav").outerHeight(true);
$(".box").animate({top: newHeight});
// set the container's new height, much like you had...
$("#container").height(original_height + newHeight);
});
});
}
});
});
增加思考的几点:
- 我简化了多个
if
语句,使其更易于理解和处理 - 我使用
hide()
是为了避免在连续多次单击FILTER
时出现混乱的动画问题 - 我只调整了盒子的
top
坐标来实现这一点 - 我更喜欢把盒子放在一个更通用的容器里,这样可以更容易地制作动画和管理,但我知道wordpress并不总是给你最大的工作空间,所以这应该会让你走上正轨
它可能不完全是你在动画中想要的,但它是你所拥有的代码的一个工作示例,应该能让你完成90%的工作。。。希望这能有所帮助!:)
使用容器元素的数据收集而不是全局变量,即在顶部记录高度
$("#container").data('height', $("#container").height());
然后使用
$("#container").data('height');
即重置高度
$("#container").css({height: $("#container").data('height') });
我对全局变量的工作方式有点怀疑。值得一试也许
相关文章:
- 主干集合重置和解析
- 如果弹出窗口关闭,Javascript停止/重置计时器
- 如何在玩TimelineMax(GSAP)后重置原始位置
- 将btn下拉切换重置为原始状态
- 将IIFE的原始值存储在Angular中的监视功能(重置过滤器)中
- 加载后将下拉列表重置为原始列表
- 变量不断重置为其原始值
- 停止浮动标签重置回原始位置
- 如何在关闭时将引导模式重置为其原始内容
- 关闭并将高度和宽度重置为“0”上的原始尺寸;关闭“;点击
- 如何重置为原始值
- 对过滤行进行分页 — 重置原始数据集 — jQuery 数据表
- 悬停时将状态重置为原始状态
- 将d3条形图重置为原始数据源时,重复轴节点
- 如何在Angular JS中提交表单后将其重置为原始状态
- 将变量标志重置为原始状态
- 使用 Jquery 做动画后需要重置原始样式
- AngularJS指令在断点处替换html,不会重置为原始元素
- 将SVG重置为原始变换矩阵
- HTML和如何获得一个按钮重置到原始状态后,点击