简体   繁体   English

如何在.css()方法之后恢复到原始的css值

[英]how to revert to original css values after .css() method

I have an element I am changing with an animation as follows: 我有一个我正在用动画改变的元素,如下所示:

that$.animate({
                    opacity:'0.0'
                },300,
                function() {
                    $('#menuHolder').css({
                        left:'0px',
                        top:'0px',
                        height:'100%',
                        width:'100%',
                    },0);

This is meant to make menuHolder take up the whole screen. 这是为了让menuHolder占据整个屏幕。 When clicking a separate button, I want menuHolder to revert to the original values i assigned in the style sheet. 单击一个单独的按钮时,我希望menuHolder恢复为我在样式表中指定的原始值。 Here is the code for the return button: 这是返回按钮的代码:

$('.return').bind('click',
        function() {
            $(this).hide(300);

            tMT$ = $(this).parent();

            tMT$.animate({
                opacity:'0.0'
            },300,
            function() {
                $('#menuHolder').css({
                    left:$(this).style.left,
                    top:$(this).style.top,
                    height:$(this).style.height,
                    width:$(this).style.width
                },0);
            })

This doesnt work, because I have assigned the original css values with when that$.animate was executed. 这不起作用,因为我已经在执行that$.animate时分配了原始的css值。 How should I assign the values of .return 's click so that menuHolder reverts to its original css? 我应该如何分配.return点击的值,以便menuHolder恢复到原始的CSS?

I don't want to manually reassign values. 我不想手动重新分配值。 I would rather do it programmatically =D. 我宁愿以编程方式= D。 Any help would be great. 任何帮助都会很棒。

Cheers. 干杯。

That is why you should not change individual CSS properties from your jQuery/Javascript code. 这就是为什么你不应该从jQuery / Javascript代码中更改单个CSS属性的原因。 Other than not being elegant, readable and maintainable and not separating presentation from behaviour, it will lead to situations like yours. 除了不优雅,可读和可维护以及不将表现与行为分开之外,它将导致像你这样的情况。

Instead: 代替:

Create classes in your CSS file: 在CSS文件中创建类:

.full-screen { left: 0; top: 0; width: 100%; height: 100%; }

and only use jQuery/Javascript to add/remove them: 并且只使用jQuery / Javascript来添加/删除它们:

$('#menuHolder').addClass('full-screen');
...
$('#menuHolder').removeClass('full-screen');

jQuery Class Manipulation Methods jQuery类操作方法

I agree with the other answers.. BUT... 我同意其他答案..但......

Sometimes adding css in the script is required. 有时需要在脚本中添加css。 For example, when animating or setting dynamic css properties. 例如,动画或设置动态css属性时。

Differences between .addClass() and .css() .addClass()和.css()之间的差异

.addClass() actually adds a class and the CSS for it. .addClass()实际上为它添加了一个类和CSS。

.css() adds the CSS to the tag .css()将CSS添加到标记中

Example

<!-- default -->
<div></div>
<!-- added class -->
<div class="added-class"></div>
<!-- added css -->
<div style="height:100px"></div>

the way to change it back is to just leave the property empty. 改变它的方法是将属性留空。

$(selector).css('height','');

It will remove the style-property completely and output: 它将完全删除style-property并输出:

<!-- removed css -->
<div></div>

Best way to accomplish this is by adding a class, which you can remove when you want the revert the original CSS. 实现此目的的最佳方法是添加一个类,您可以在需要还原原始CSS时删除该类。 This makes it also easier to maintain, all the CSS stays in your stylesheets. 这使得维护更容易,所有CSS都保留在样式表中。

$('#menuHolder').addClass('animate')

and

$('#menuHolder').removeClass('animate')

in your stylesheet: 在你的样式表中:

.animate {
left:0px;
top:0px;
height:100%;
width:100%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM