【发布时间】:2014-03-29 18:01:00
【问题描述】:
我的容器 div 包含两个 div div box1 和 div box2。 box2 是绝对 div,最大宽度为 400px;
html:
<div id="container">
<div class="box1"></div>
<div class="box2 animated"></div>
</div>
css:
#container{position:relative;width:100%;height:100%;background:red;}
#container #box1{position:relative;width:760px;height:300px;background:blue;margin:0;}
#container #box2{position:absolute;width:60px;max-width:350px;height:300px;background:yellow;;top:0;}
jQuery:
$(document).ready(function(){
var box1width = $(".box1").width();
var windowWidth = $(window).width();
var x = windowWidth - (box1width + 350 ) ;
var wdt = windowWidth - (box1width) ;
if (".box2").hasClass("animated"){
if(x > windowWidth ){
$(".box2").animate({width:'350px'}, 2000);
} else {
$(".box2").animate({width: wdt+ 'px'}, 2000);
}
} else {
alert("no animation for box2 nd width remain 60px");
}
});
最初这会为我的 box2width 设置动画;现在我想在窗口调整大小时为 box2 宽度设置动画,并将我的 box2 动画宽度限制为最多 350 像素;
【问题讨论】:
-
您听起来好像这段代码部分工作。但它会抛出错误
Uncaught SyntaxError: Unexpected token .一个工作小提琴会很容易解决问题 -
你还没有分享
animated类.. -
这对我有用;我不知道在写这个问题时是否有任何错误。实际上我可以使用 window.resize 函数,但我不想在每次 window.resize 时触发方法 hasClass("animated")。
-
您分享的代码有很多语法错误。它抛出错误
Uncaught SyntaxError: Unexpected token .尝试复制粘贴确切的工作代码.. 或者工作小提琴可以轻松解决问题 -
这个动画类与css无关,这实际上是一个变量类来指示动画,否则这个cass应该是无动画的;
标签: jquery css jquery-animate window-resize