2014-10-10 170 views
-1

嗨,我有这个动画宽度更改器,它似乎自动调整到100%,当我点击展开此脚本然后单击我的标题徽标。任何想法为什么?动画宽度调整大小100%

$(".fluid").hide(); 
$(".fixed").click(function() { 
    $("#mainwidth").animate({width: "1024px"}, 800); 
    $(this).hide(); 
    $(".fluid").show(); 
    $.cookie("width","fixed", {expires: 365}); 
    return false; 
}); 
$(".fluid").click(function() { 
    $("#mainwidth").animate({width: "95%"}, 800); 
    $(this).hide(); 
    $(".fixed").show(); 
    $.cookie("width","fluid", {expires: 365}); 
    return false; 
}); 
if($.cookie("width") == "fixed") { 
    $(".fixed").hide(); 
    $(".fluid").show(); 
    $("#mainwidth").css("width","1024px"); 
}; 

text-align: left; 
line-height: 1.4; 
margin: auto auto; 
margin-top: 40px; 
margin-bottom: 50px; 
+2

你可以做小提琴吗? – 2014-10-10 06:27:08

+0

没有你的HTML和CSS,它不可能说或重现你的问题。请记住,默认情况下,边框会在外边增长,所以如果边框宽度大于2.5%,则会增加到100%。 – Banana 2014-10-10 06:37:23

回答

1

问题发生在以下块:

<script type="text/javascript"> 
jQuery(function($) { 
    $(".fluid").hide(); 
    $(".fixed").click(function() { 
     $("#mainwidth").animate({width: "1024px"}, 800); 
     $(this).hide(); 
     $(".fluid").show(); 
     $.cookie("width","fixed", {expires: 365}); 
     return false; 
    }); 
    $(".fluid").click(function() { 
     $("#mainwidth").animate({width: "95%"}, 800); 
     $(this).hide(); 
     $(".fixed").show(); 
     $.cookie("width","fluid", {expires: 365}); 
     return false; 
    }); 
    if($.cookie("width") == "fixed") { 
     $(".fixed").hide(); 
     $(".fluid").show(); 
     $("#mainwidth").css("width","1024px"); 
    }; 
}); 
</script> 

让我们看看在最后声明:

if($.cookie("width") == "fixed") { 
    $(".fixed").hide(); 
    $(".fluid").show(); 
    $("#mainwidth").css("width","1024px"); 
}; 

它指示浏览器更改宽度页面加载时。如果width Cookie值为“固定”,则将宽度设置为1024px。但是,如果页面重新加载而Cookie值为“fluid”
当您单击徽标时,它会重新加载页面。因此,如果cookie值是流畅的,则宽度将不会被设置为相关值。只需添加另一个代码块即可处理cookie值为“fluid”的情况,并且它可以正常工作。

if($.cookie("width") == "fixed") { 
    $(".fixed").hide(); 
    $(".fluid").show(); 
    $("#mainwidth").css("width","1024px"); 
} 
else if($.cookie("width") == "fluid") { 
    $(".fluid").hide(); 
    $(".fixed").show(); 
    $("#mainwidth").css("width","95%"); 
}; 
+0

好吧,我现在修好了。非常感谢您的时间和帮助。 – andreas 2014-10-10 07:09:12

+0

它不是关于jQuery,它只是简单的逻辑:)你处理一个案件,但不处理其他案件。如果您注意到,只有当您点击徽标,而宽度为95%时,网站才会100%,如果您在宽度为1024像素时点击徽标,则保持相同方式。这是因为你只处理了两种情况中的一种。只需将'else if'块添加到您的代码中,就像我写的那样,它应该可以工作。请不要忘记接受我的答案,如果它帮助:)(绿色V在左边) – Banana 2014-10-10 07:11:37