【问题标题】:CSS Transitions Not Working After jQuery Animation?jQuery 动画后 CSS 过渡不起作用?
【发布时间】:2015-07-22 01:08:45
【问题描述】:

好的,所以我有三个气泡形式的 div。现在我只是在做第一个。它们应该在单击时爆炸,几乎占据整个屏幕,然后在单击标题时回到原来的气泡。这是代码 - 如果你运行它,整个事情会更容易解释:

<!DOCTYPE html>
<html>
    <head>
        <title>KURB - Home</title>
        <link rel="stylesheet" type="text/css" href="kurb.css"/>
        <link href='http://fonts.googleapis.com/css?family=Questrial' rel='stylesheet' type='text/css'>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    </head>
    <body>
        <div id="main">
            <p id="k">KURB</p>
        </div>
        <div id="about">
            <p class="label">Blah blah blah</p>
        </div>
        <div id="work">
            <p class="label">Blah blah blah</p>
        </div>
        <div id="contact">
            <p class="label">Blah blah blah</p>
        </div>

        <script type="text/javascript">
            var main = $("#main");
            var about = $("#about");
            var work = $("#work");
            var contact = $("#contact");

            var open = false;

            about.click(function() {
                about.animate({
                    height: "100vh",
                    marginTop: "0vh",
                    width: "100%",
                    marginLeft: "0%",
                    borderRadius: "0",
                    opacity: "1"
                }, 300);

                main.animate({
                    marginTop: "1vh"
                }, 300);

                about.css("cursor", "auto");
                work.css("display", "none");
                contact.css("display", "none");
                main.css("cursor", "pointer");

                open=true;
            });

            main.click(function() {
                if (open = true) {
                    work.css("display", "inline");
                    contact.css("display", "inline");
                    main.css("cursor", "auto");

                    about.animate({
                        width: "38%",
                        marginLeft: "31%",
                        height: "30vh",
                        marginTop: "7vh",
                        borderRadius: "100%",
                        opacity: "0.6"
                    }, 300);

                    main.animate({
                        marginTop: "10vh"
                    }, 300);

                    about.css("cursor", "pointer");

                    open=false;
                }
            });
        </script>
    </body>
</html>

CSS:

body {
    overflow: hidden;
}

#main {
    float: left;
    width: 20%;
    height: 10vh;
    margin-top: 10vh;
    margin-left: 40%;
    text-align: center;
}

#k {
    font-family: Questrial;
    font-size: 70px;
    margin-top: -7px;
    margin-left: -2px;
    color: #404040;
    border-bottom: 2px solid #404040;
    line-height: 0.85;
}

#about {
    float: left;
    clear: both;
    width: 38%;
    height: 30vh;
    margin-top: 7vh;
    margin-left: 31%;
    text-align: center;
    background-color: #33CC33;
    border-radius: 100%;
    opacity: 0.6;
    transition: opacity .5s, margin-top .5s, margin-bottom .5s;
    -webkit-transition: opacity .5s, margin-top .5s, margin-bottom .5s;
    cursor: pointer;
}

#about:hover {
    opacity: 0.8;
    margin-top: 5vh;
    margin-bottom: 2vh;
}

#work {
    float: left;
    width: 38%;
    height: 30vh;
    margin-top: 0vh;
    margin-left: 10%;
    text-align: center;
    background-color: #323280;
    border-radius: 100%;
    opacity: 0.6;
    transition: opacity .5s, margin-top .5s;
    -webkit-transition: opacity .5s, margin-top .5s;
    cursor: pointer;
}

#work:hover {
    opacity: 0.8;
    margin-top: -2vh;
}

#contact {
    float: right;
    width: 38%;
    height: 30vh;
    margin-top: 0vh;
    margin-right: 10%;
    text-align: center;
    background-color: #FF6600;
    border-radius: 100%;
    opacity: 0.6;
    transition: opacity .5s, margin-top .5s;
    -webkit-transition: opacity .5s, margin-top .5s;
    cursor: pointer;
}

#contact:hover {
    opacity: 0.8;
    margin-top: -2vh;
}

.label {
    font-family: Questrial;
    color: white;
    font-size: 35px;
    margin-top: 80px;
}

现在我已经能够做到这一切了。问题是,之后,应该改变不透明度和 margin-top/margin-bottom 的 CSS 过渡似乎不起作用。不透明度根本没有改变,div 也没有向上移动 - 底部的两个 div 向下移动。我不知道为什么,但我怀疑这是因为我以某种方式将 jQuery 动画搞砸了。我注意到当 div 是全尺寸时,CSS 过渡被停用。也许他们保持停用状态? ...帮助?

【问题讨论】:

  • 在您的 main.click() 函数中,您的 if 条件语句有错误。如果与 true 相比,它应该是 if (open === true) {。它不能解决你的问题,但我希望它有助于调试:)

标签: jquery html css css-transitions


【解决方案1】:

试试这个:

transition: opacity .5s, margin-top .5s !important;

将 !important 关键字放在样式属性的末尾,它应该在您执行任何与 jQuery 相关的内容后工作

我还建议您创建一个在两个点击事件之间执行任何常见功能的函数。添加一些参数(例如要设置动画的元素和动画参数)并使用这些参数来提供动画信息:

func animateElement(elementToAnimate, height, width //etc {

 elementToAnimate.animate({
                    height: height,
                    marginTop: "0vh",
                    width: width,
                    marginLeft: "0%",
                    borderRadius: "0",
                    opacity: "1"
                }, 300);

}

【讨论】:

  • 那行得通。现在的问题是当 div 是全尺寸时,如果您将鼠标移出,它会移动。是否有任何解决方案,或者我是否必须使用 jQuery 删除 :hover CSS?这甚至可能吗?
  • @Tommay 嗯,给我一秒钟看看和思考
  • 也许答案是制作一个完全不同的 id,除了没有 :hover 之外,它与原始 id 具有相同的特征?看起来那样会很烦人......
  • @Tommay 我已将您的代码移至此小提琴。如果鼠标移开,它会移动是什么意思?当我将鼠标移出jsfiddle.net/m3su2g3g 时,我没有看到任何动作
  • 如果您添加了我们的 !important 解决方案,它就可以了。
猜你喜欢
  • 2016-09-22
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多