jQuery animate()干扰css转换,导致越野车动画

时间:2018-11-22 14:02:03

标签: jquery css-transitions transition

我有一个奇怪的问题,其中jQuery animate()似乎干扰了CSS过渡。我将问题归结为此CSS过渡:

 transition: 0.5s;

代码在这里:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">

    $(document).ready(
        function() {
            $(".closebtn").click(
                function() {
                    $('#myNav').animate(
                        {
                            'width': '0%'
                        },
                        5000,
                        function() {
                            window.location.href = "blog.html"; //will redirect to your blog page (an ex: blog.html)
                        }
                    );

                }
            );
        }
    )
</script>

<style>
body {
    font-family: 'Lato', sans-serif;
}

.overlay {
    height: 100%;
    width: 10%;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: rgb(0,0,0);
    background-color: rgba(0,0,0, 0.9);
    overflow-x: hidden;
    transition: 0.5s;
}

.overlay:hover {
    width: 20%;
}

.overlay-content {
    position: relative;
    top: 25%;
    width: 100%;
    text-align: center;
    margin-top: 30px;
}

.overlay a {
    padding: 8px;
    text-decoration: none;
    font-size: 36px;
    color: #818181;
    display: block;
    transition: 0.3s;
}

.overlay a:hover, .overlay a:focus {
    color: #f1f1f1;
}

.overlay .closebtn {
    position: absolute;
    top: 20px;
    right: 45px;
    font-size: 60px;
}

@media screen and (max-height: 450px) {
  .overlay a {font-size: 20px}
  .overlay .closebtn {
    font-size: 40px;
    top: 15px;
    right: 35px;
  }
}
</style>
</head>
<body>

<div id="myNav" class="overlay">
  <a href="#" class="closebtn">&times;</a>
  <div class="overlay-content">
    <a href="#">About</a>
    <a href="#">Services</a>
    <a href="#">Clients</a>
    <a href="#">Contact</a>
  </div>
</div>

<h2>Fullscreen Overlay Nav Example</h2>
<p>Click on the element below to open the fullscreen overlay navigation menu.</p>
<p>In this example, the navigation menu will slide in, from left to right:</p>
<span style="margin-left:200px;font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>

<script>
function openNav() {
  document.getElementById("myNav").style.width = "100%";
}

function closeNav() {

    document.getElementById("myNav").style.width = "0%";

   setTimeout(function () {
    window.location.href = "blog.html"; //will redirect to your blog page (an ex: blog.html)
    }, 1.5); //will call the function after 2 secs.

}
</script>

</body>
</html>

这是一个小提琴:https://jsfiddle.net/sezj2tvq/

您单击“打开”,然后打开覆盖图。单击“ X”关闭覆盖。关闭叠加层时,动画有点呆滞。

当我删除CSS过渡时,动画效果很好。但是,当然,悬停效果已不再过渡。

在我的桌面(Win 7)上,单击“ x”以关闭覆盖层时,它以某种方式奇怪地计算了宽度,您可以在Firefox Code-Inspector中看到它。它不会从100%到0%(关闭叠加层)计数,而是从width =“ 1600%”之类的东西开始,倒数至“ 0%”,从而产生奇怪的动画。

有人可以帮忙吗?

0 个答案:

没有答案