What is the best method to implement CSS Animation using Angular when a button is clicked?

I am struggling to figure out how to use an Angular function to activate a CSS animation in my project. Specifically, I want the div "slide1" to slide horizontally to the right when either button "B1" or "B2" is clicked. Despite searching for solutions, I am still confused about how to implement this feature.

Here is the Angular function I have:

directive('click2', () => {
  return{
    restrict: 'A',
      link: (scope) => {
        scope.clicked = () => {

        }
      }
  }
})

The CSS styling for my slide div is as follows:

.slide1 {
  bottom: -500px;
  width: 30%;
  //min-width: 275px;
  height: 50%;
  min-height: 390px;
  box-sizing: border-box;
  position: relative;
  content: '';
  background: #a2e0f7;
  animation:nudge 5s linear alternate;
}

@keyframes nudge {
  0%{
    right: 500px;
  };
  100% {
    transform: translate(500px);
   }
  50% {
    transform: translate(0,0);
  }
}

Can anyone provide guidance on how I can utilize my Angular function to trigger the movement of the Slide1 div upon clicking?

Answer №1

A simple technique to accomplish this task is by dynamically assigning the class to the div.

One approach to achieve this would involve utilizing the following code:

<div ng-controller="MyCtrl">
  <button ng-click="doSlide = !doSlide">A</button>
  <button ng-click="doSlide = !doSlide">B</button>
  <div ng-class="{'slide1': doSlide}">Click a button to animate me</div>
</div>

By clicking on either button, the value of doSlide will be toggled. When doSlide is set to true, the slide1 class will be applied to the div.

Please note that there is no need to explicitly declare doSlide in the controller as Angular will automatically include it in the scope.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am wondering if it is feasible for a POST route to invoke another POST route and retrieve the response ('res') from the second POST in Express using Node.js

Currently, I have a POST route that triggers a function: router.route('/generateSeed').post(function(req,res){ generate_seed(res) }); UPDATE: Here is the genrate_seed() function function generate_seed(res) { var new_seed = lightwallet. ...

Converting Python code into JavaScript for use on an iPhone device

Having a web application with backend developed in Python (using Django) and front end in HTML5 & JavaScript, where Python generated data is transferred to JavaScript/HTML through JSON. Now, I want to transform it into a mobile application, starting with ...

BlendModeGlobal operations

Experimenting with the globalCompositeOperation property in a loop by passing different strings (source-atop, source-over, etc.) to the same 2D context, I observed that Firefox only allowed me to draw a few shapes while Opera only displayed the last one. ...

Configuration of HTTP JSONP variable in Angular

I have successfully implemented http.jsonp for making cross-domain calls. Here is the configuration object I am using: var config = { params: { action: "query", prop: "revisions", format: "json" ...

What to do when VueJs computed method throws an error: "Cannot access property 'words' of undefined"?

Here is some pseudo code that I've written: var vm = new Vue({ el: '#test', data: { words: [{name:'1'}, {name:'2'}, {name:'3'},{name:'4'},{name:'5'},{name:'6'}], } ...

Maintain an equal distance between 2 out of 3 elements while resizing the window to ensure responsiveness

There are two image divs stacked on top of each other, positioned beside a fluid header logo (.svg) also contained in a div. The HTML: <header class="site-header" role="banner" itemscope="itemscope" itemtype="http://schema.org/WPHeader"><div cla ...

template for displaying data in Vue.js using props

In my application, I have a component that I frequently use to display tables. Now, I want to enhance this component by allowing the customization of table fields (templates) through props in the parent component. This way, I can avoid constant edits to th ...

Instead of properly addressing the issue, the error message "Cannot read property 'password' of undefined" is being displayed

I'm currently working on creating a user login system using express js and dynamodb. However, I'm facing an issue where the system works fine when logging in with correct email and password, but fails to handle errors when using an incorrect emai ...

The error occurred at line 12 in the app.js file, where it is unable to access properties of an undefined object, specifically the 'className' property. This occurred within an anonymous function in an HTMLDivElement

I am facing an issue with my website where I have buttons on the right side. I want to use JavaScript to change the style of the button when it is clicked. When the page loads, the home button should have a background-color: green. However, when another bu ...

Clicking on the image does not result in a larger image being displayed

Currently working on an assignment that requires a modal pop-out to display larger versions of photos when clicked, with the option to go back using the X button. Unfortunately, I'm facing issues with the X button not functioning properly and images n ...

Ways to deselect a checkbox if the selected option is anything other than 'All'

I have a set of checkboxes generated from a database, but one checkbox labeled "All" serves as a way to select or deselect all other checkboxes with a single click. If all the options are checked and any individual option (other than 'All') i ...

Unable to access a specific URL for the chosen items

I am looking to navigate to specific links when clicking on particular planets using raycaster. However, the current issue is that I get redirected to the URL of the last planet (referred to as category) whenever I click anywhere on the canvas. I have sepa ...

How can I use the Grails Asset plugin to reference a static HTML file?

Currently, I am working with Grails 2.4.1 and have incorporated version 1.9.7 of the Grails Asset Pipeline Plugin. In my project, there is a javascript file that defines an AngularJS directive. This Javascript file needs to access a static HTML template f ...

When setting the request header in XMLHttpRequest, the $_POST variable will be null

I encountered an issue while trying to send a request using XMLHttpRequest in JavaScript code with PHP. Whenever I set the request header using `setRequestHeader`, the `$_POST` value returns null. However, if I remove `setRequestHeader`, the `$_POST` data ...

How to Adjust Polling Interval in Angular 2 when running npm start

Currently, I am going through the Angular 2 tutorial (https://angular.io/guide/quickstart) and utilizing npm start to monitor the TypeScript files. It automatically transpiles and reloads the built-in web server whenever there are changes. The process wor ...

I am encountering an issue with express-session where it is failing to assign an ID to

Seeking assistance with utilizing express-session to manage user sessions on arcade.ly. I have not specified a value for genid, opting to stick with the default ID generation. However, an ID is not being generated for my session. An example of the issue c ...

The quiet harmony of FeathersJS and Socket.io as they attentively listen to events is

I've been working hard to get everything set up and running smoothly, but it seems like I'm stuck on the last piece of the puzzle. Despite following the documentation closely, I can't seem to figure out what I'm missing. Here is the @an ...

Internet Explorer failing to trigger Ajax requests

I'm encountering an issue with my script on my webpage - it works perfectly in Chrome, but Internet Explorer is not entering the success{} function of Ajax. It does go into the Complete{} function without any problems. I attempted to pass the data var ...

Retrieving Information from an API with Custom Headers in React Native

I have an API that necessitates specific headers for access. Without these headers, a browser displays the following error: Code: 4101 Message: Header X-Candy-Platform is required However, when the headers are provided, the API returns a json response. ...

What is the correct approach to importing the Select class in JavaScript with WebDriver?

I'm having trouble using the Select function in my code to interact with a dropdown menu. I attempted to import Select from the "selenium-webdriver" package, but it doesn't seem to be working. All of the search results I've found on this top ...