javascript - 问题滑入/滑出 JQuery

标签 javascript jquery css width slidetoggle

滑出没问题我唯一的问题是滑入没有出现,我认为它没有捕捉到他们的第一个 IF 宽度等于 0px。抱歉,我对 jQuery 真的一窍不通。

代码:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    $("#ShowHideComment").click(function(){
        if ($(".iframe_comment").width() == "0px"){
            $(".iframe_comment").animate({width: "800px"}, {queue:false, duration:1000});
        }
        else{
            $(".iframe_comment").animate({width: "0px"}, {queue:false, duration:1000
           });
        }
    });
});
</script>

最佳答案

来自 the docs :

All animated properties should be animated to a single numeric value

您在这里处理的不是 CSS 属性值,而是普通整数。

$(document).ready(function(){
    $("#ShowHideComment").click(function(){
        var $comment = $(".iframe_comment");
        if ($comment.width() == 0){
            $comment.animate({width: 800}, {queue:false, duration:1000});
        }
        else{
            $comment.animate({width: 0}, {queue:false, duration:1000});
        }
    });
});

还有 see width() :

The difference between .css(width) and .width() is that the latter returns a unit-less pixel value

关于javascript - 问题滑入/滑出 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6007490/

相关文章:

javascript - 在 JSON 对象中嵌入 ES6 箭头函数

jquery - jQuery hide() 和 show() 的问题

javascript - 仅替换出现在第一个元素中

php - 在 div 容器中启用拖放上传

javascript - 查找并滚动到元素

jquery - 具有百分比宽度的滑动 Div

javascript - CSS 动画在 Chrome (iOS) 上不流畅

html - 在图像和 div 上包装文本

javascript - jquery 将数据属性与数组中的值匹配

javascript - 防止 Osama Facebook 蠕虫