简体   繁体   English

滑动和淡入div元素

[英]Sliding and fading a div element

I am trying to animate a div element (slide and fade) with a button click. 我正在尝试通过单击按钮来动画div元素(滑动和淡入淡出)。 At first, the element is not visible to a user. 首先,该元素对用户不可见。 When the button is clicked, it will slide to right and fade in. Once the button is clicked again, it will slide to left and fade out. 单击该按钮时,它将向右滑动并淡入。再次单击该按钮时,它将向左滑动并淡出。 I come up with two solutions, with css and with JQuery. 我提出了两种解决方案,分别是css和JQuery。

In the first one, I used JQuery. 在第一个中,我使用了JQuery。 You can find the example in this JSFiddle 1 . 您可以在此JSFiddle 1中找到示例。

HTML HTML

<button id="my-button">Click me!</button>
<div id="my-modal"></div>

CSS CSS

#my-modal {
    opacity: 1;
    position: fixed;
    top: 50px;
    left: 0;
    left: -250px;
    width: 250px;
    height: 100%;
    background-color: red;
}

JQuery JQuery的

$("#my-button").click(function () {
    var $modal = $("#my-modal");
    $modal.stop(true, true).animate({
        left: "toggle",
        opacity: "toggle"
    }, 1000);
});

Here, everything seems working but it does directly opposite of what I want. 在这里,一切似乎都可以正常工作,但与我想要的相反。 It first fades out, and with the second click, it fades in. It is because that the opacity of the element is 1, but if I turn it to 0, nothing happens. 它首先淡出,然后单击第二次淡入。这是因为元素的不透明度为1,但是如果将其设为0,则什么也不会发生。

Secondly, I tried to do that with css animation by using key-frames (changing opacity from 0 to 1) but it has also problem. 其次,我尝试通过使用关键帧(将不透明度从0更改为1)对CSS动画进行处理,但这也存在问题。 It starts the animation exactly the way I want. 它完全按照我想要的方式启动动画。 However, when I click the button again, it disappears immediately. 但是,当我再次单击该按钮时,它会立即消失。 Here is the JSFiddle 2 . 这是JSFiddle 2

HTML HTML

<button id="my-button">Click me!</button>
<div id="my-modal"></div>

CSS CSS

    #my-modal {
    opacity: 0;
    position: fixed;
    top: 50px;
    left: 0;
    left: -250px;
    width: 250px;
    height: 100%;
    background-color: red;
    -moz-transition: all 1s ease;
    -webkit-transition: all 1s ease;
    -o-transition: all 1s ease;
    transition: all 1s ease;
}
.move-my-modal {
    -moz-transform: translate(250px, 0px);
    -webkit-transform: translate(250px, 0px);
    -ms-transform: translate(250px, 0px);
    -o-transform: translate(250px, 0px);
}
.animate-opacity {
    -webkit-animation: toggle-opacity 1s ease;
    -moz-animation: toggle-opacity 1s ease;
    -o-animation: toggle-opacity 1s ease;
    animation: toggle-opacity 1s ease;
    -webkit-animation-fill-mode: forwards;
}
@-webkit-keyframes toggle-opacity {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@-moz-keyframes toggle-opacity {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@-o-keyframes toggle-opacity {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}
@keyframes toggle-opacity {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

JQuery JQuery的

$("#my-button").click(function () {
    var $modal = $("#my-modal");
    $modal.toggleClass("move-my-modal");
    $modal.toggleClass("animate-opacity");
});

To this end, I have these questions; 为此,我有这些问题。

1) What are the problems with these two approaches? 1)这两种方法有什么问题? Is there something that I missed or forgot to use? 有什么我想念或忘记使用的东西吗? How can I correct them to meet the requirements that I mentioned at the beginning. 我如何纠正它们以满足我在开始时提到的要求。

2) Which one is the better way to make this action? 2)哪个是执行此操作的更好方法? Is there any cons or pros of these approaches? 这些方法有什么利弊吗?

3) Is there any other way to make this action? 3)还有其他方法可以执行此操作吗? I am new on this area and I might not notice a simpler way. 我是这个领域的新手,我可能不会注意到更简单的方法。

You can toggle an .active class to the element and use CSS transitions. 您可以将.active类切换到元素并使用CSS过渡。

This way, if the browser is old enough to not support animations, it will still work but it won't slow down computers that do not handle animations well. 这样,如果浏览器足够老而不能支持动画,它仍然可以工作,但不会降低不能很好地处理动画的计算机的速度。

$("#my-button").click(function () {
    $("#my-modal").toggleClass('active');
});
#my-modal.active {
    opacity: 1;
    left: 0;
}

 $("#my-button").click(function () { $("#my-modal").toggleClass('active'); }); 
 #my-modal { opacity: 0; position: fixed; top: 50px; left: -250px; width: 250px; height: 100%; background-color: red; transition: all 1s linear; } #my-modal.active { opacity: 1; left: 0; } 
 <button id="my-button">Click me!</button> <div id="my-modal"></div> <!-- jQuery --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM