如何重置为原始值

How to reset to original values?

本文关键字:原始 何重置      更新时间:2023-09-26

看起来每次点击都会添加一个新的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') });

我对全局变量的工作方式有点怀疑。值得一试也许