简体   繁体   English

W3 CSS动画

[英]W3 CSS Animation

I feel dumb for asking this. 我问这个很傻。 Let's start off by I used a W3 responsive template. 让我们从使用W3响应模板开始。 I love everything about it and even incorporated the single page application design aspect using AngularJS. 我喜欢它的所有内容,甚至使用AngularJS合并了单页应用程序设计方面。 Now here's my one question. 现在这是我的一个问题。

When whoever is on a mobile device using the website, they get a menu option that opens a sidenav. 当使用网站的移动设备上的任何人使用时,他们都会获得一个菜单选项,可以打开一个辅助导航。 This animation is called "w3-animate-left" and it pulls the nav from -300px to 0px as shown below. 此动画称为“ w3-animate-left”,它将导航从-300px拉到0px,如下所示。 Now what I want to do is make one that animates from 0px to -300px and incorporate it to my existing CSS. 现在,我要做的是制作一个从0px到-300px的动画,并将其合并到我现有的CSS中。

.w3-animate-left {
    position: relative;
    -webkit-animation: animateleft 0.4s;
    animation: animateleft 0.4s;
}
@-webkit-keyframes animateleft {
    from {
        left: -300px;
        opacity: 0;
    }
    to {
        left: 0;
        opacity: 1;
    }
}
@keyframes animateleft {
    from {
        left: -300px;
        opacity: 0;
    }
    to {
        left: 0;
        opacity: 1;
    }
}

Then I ran into another obstacle, the div containing the class call to the CSS above incorporates it in the class as shown below. 然后我遇到了另一个障碍,包含上面对CSS的类调用的div将其合并到类中,如下所示。

<div class="w3-sidenav w3-white w3-card-2 w3-animate-left w3-hide-medium w3-hide-large" style="display:none" id="mySidenav" ng-controller="mainController">
    <a href="" ng-click="redirectToPackages()" onclick="w3_close()"><i class="fa fa-birthday-cake"></i> PACKAGES</a>
    <a href="" ng-click="redirectToOpenPlay()" onclick="w3_close()"><i class="fa fa-child"></i> OPEN PLAY</a>
    <a href="" ng-click="redirectToGallery()" onclick="w3_close()"><i class="fa fa-camera-retro"></i> GALLERY</a>
    <a href="" ng-click="redirectToContact()" onclick="w3_close()"><i class="fa fa-envelope"></i> CONTACT</a>
</div>

Here's the JavaScript as well: 这也是JavaScript:

<script>
    // Toggle between showing and hiding the sidenav when clicking the menu icon
    var mySidenav = document.getElementById('mySidenav');

    function w3_open() {
        if (mySidenav.style.display === 'block') {
            mySidenav.style.display = 'none';
        } else {
            mySidenav.style.display = 'block';
        }
    }
    function w3_close() {
        mySidenav.style.display = 'none';
    }
</script>

Now the problem here is even if I made the edit to the W3 CSS that I wanted, how would I be able to incorporate it so that when they click to close, it does the custom animation that I asked? 现在的问题是,即使我对所需的W3 CSS进行了编辑,我如何也可以将其合并,以便当他们单击关闭时,它可以执行我要求的自定义动画吗?

Also how can I add it that if they click somewhere off the screen, it exits the side nav? 另外,如果他们单击屏幕外的某个地方,它会退出侧面导航,该如何添加呢? Thank you in advance! 先感谢您!

 var _open = document.getElementById("open"), _navigation = document.getElementById("navigation"); _open.onclick = function() { _navigation.classList.toggle("openNav"); } 
 * { margin: 0; padding: 0; } #open { position: absolute; right: 0; cursor: pointer; } #navigation { background: black; width: 300px; height: 100vh; transform: translateX(-300px); transition: all 0.3s linear; } #navigation.openNav { transform: translateX(0); } 
 <a id="open">Open/Close</a> <div id="navigation"></div> 

This is my method on creating navigation animation and not based on someone's work. 这是我创建导航动画的方法,而不是基于某人的工作。 This will not work in old browsers. 在旧的浏览器中将无法使用。

The technique you just need is the transform, transition and adding class. 您仅需要的技术是转换,过渡和添加类。 You don't need to use animation. 您不需要使用动画。

By adding class of openNav , it will change the value of transform: translateX and it allows the navigation to be shown in browser. 通过添加openNav类,它将更改transform:translateX的值,并允许在浏览器中显示导航。

Take note that you can't animate display attribute in css. 请注意,您无法在CSS中设置显示属性的动画。 You can use opacity for fadeIn/fadeOut. 您可以使用不透明度进行淡入/淡出。

Hope it helps. 希望能帮助到你。 Cheers! 干杯!

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

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