0

Right now I'm trying to add an animation to a div when I press a button. However, I'm not sure on how to trigger a custom animation on button click with JavaScript or jQuery. As of now I do not have any JavaScript written for this.

Im very new to this, hence me asking the pro's!

HTML:

<div class="container" id="frontPage">
        <div "container">
            <h1 class="header center green-text">ABOUT</h1>
            <div class="row center">

                <p class="header col s12 light">Something here</p>

            </div>        
        </div>
        <div class="row center padding-bottom-1">
            <a a href="{{ url('/loginpage') }}" class="btn-small green" id="loginButton">login</a>
            <a class="btn-small green">apply</a>
        </div>
    </div>

CSS:

#frontPage {
  text-align: center;
  margin-top: 80px;
  position: relative;
  animation-name: slideOutLeft;
  animation-duration: 1s;
  animation-fill-mode: forwards;
  width: 500px;
}

@keyframes slideOutLeft {
  0% {left: 0;}
  100% {left: -1500px;}

}
  • you may do a little search and you will get it : https://www.google.com/search?q=run+animation+using+js+site:stackoverflow.com&sa=X&ved=0ahUKEwj8zar80ubaAhXJCuwKHdlYCNoQrQIIYigEMAM&biw=1600&bih=794 – Temani Afif May 02 '18 at 08:52

1 Answers1

0

To trigger a CSS animation on a button click you can put the CSS rules in their own class. You can then apply that class to the required elements when your button is clicked, like this:

$(function() {
  $('button').click(function() {
    $('#frontPage').addClass('slideOutLeft');
  });
});
#frontPage {
  text-align: center;
  margin-top: 80px;
  position: relative;
  width: 500px;
}

.slideOutLeft {
  animation: slideOutLeft 1s forwards;
}

@keyframes slideOutLeft {
  0% {
    left: 0;
  }
  100% {
    left: -1500px;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click me</button>
<div class="container" id="frontPage">
  <div id="container">
    <h1 class="header center green-text">ABOUT</h1>
    <div class="row center">
      <p class="header col s12 light">Something here</p>
    </div>
  </div>
  <div class="row center padding-bottom-1">
    <a a href="#" class="btn-small green" id="loginButton">login</a>
    <a class="btn-small green">apply</a>
  </div>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339