Rotating images in Angular 4 by intervals of 45 degrees

I have an image and two buttons that are supposed to rotate the image either 45 or -45 degrees.

<div>
    <img src="/assets/img1.png">
</div>
<div>
    <button type="submit">rotate left 45</button>
    <button type="submit">rotate right 45</button>
</div>

Is there a way to create a function that can apply a CSS transform rotate to this image? The rotation should occur in increments of 45 degrees in either direction, continuously rotating regardless of how many times the user clicks on the button.

Answer №1

To update the CSS transform property of elements, you can simply click on them.

let rotationAmount = 0;

function adjustRotation(direction) {
  rotationAmount += direction == 'left' ? -45 : 45;
  
  document.querySelector('#image').style.transform = `rotate(${rotationAmount}deg)`;
}
#image {
  height: 100px;
  width: 100px;
}

.button-wrapper {
  margin-top: 30px;
}
<div>
    <img id="image" src="">
</div>
<div class="button-wrapper">
    <button type="submit" onclick="adjustRotation('left')">rotate left 45</button>
    <button type="submit" onclick="adjustRotation('right')">rotate right 45</button>
</div>

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

Having issues with CSS list hovering functionality in Google Chrome

One of the challenges I'm facing with my HTML list is that when hovering over each list item, the text along with the bullet point should change color. Here's the structure: <ul> <li>A bullet point</li> <li>Another ...

Managing unpredictable fields within a TypeScript interface Let me known if you need further assistance

Currently, I am developing a web application using Angular and encountered an issue with the JSON data returned by a service call. The problem arises when the mapped JSON contains an Object within one of the fields with unpredictable content. How can I han ...

What is the best way to add a div to the bottom of a grid layout?

Here is the code I am working with: .my-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); grid-gap: 20px; } .item { background-color: #ddd; height: 300px; } <div class="my-grid"> <div class="ite ...

Using the position property of relative on a DIV element causes a gap to appear between the DIV and the next

Two div elements are placed consecutively. When I move the first div using position: relative and top: -60px, it creates a gap between them. Take a look at this example here: https://codepen.io/dusannis/pen/oNgBpoK If you notice, there is a noticeable ga ...

Failure to apply Bootstrap Stylesheet

On certain pages, I have included Bootstrap. On one particular page, I have an alert styled with Bootstrap. This is how it's defined: $('#wrongPasswordDiv').html('<br/><div class="alert alert-danger" id="wrongPWAlert" role= ...

What is the proper way to tap into the features provided by DefinitelyTyped in typescript?

While working on my Angular2 app that deals with money amounts, I decided to use dinero.js to handle money values. However, I am encountering difficulties in accessing certain features in Typescript. Following the instructions, I installed the DefinitelyT ...

Listening for combinations of keys pressed using HostListener

I've been attempting to detect when a user presses the Shift+Tab key combination on the keyboard, but for some reason I can't get the event to trigger. @HostListener('keyup', ['$event']) @HostListener('keydown', [&a ...

Scrolling through four limited list items automatically

Hey there! I am currently working on a design project. You can check it out here. I'm trying to replicate a section from another site, which you can see here. <div class="latest-winners-container"> <h3 class="header">Latest Winners< ...

Encountering an issue with Auth0 and Angular2: "The supplied parameters do not match any signature of call target."

I'm currently in the process of integrating Auth0 with my Angular2 application using angular2-cli. I've added a new service called auth, but when I try running npm start, I encounter an error stating: src/app/auth.service.ts (21,5): Supplied para ...

While attempting to make my div responsive, it unexpectedly became stuck to the navbar

After successfully creating a website that works fine on desktop, I encountered an issue when viewing it on my phone. The main content section, which features jokes, gets scrolled up along with the navbar. I followed a tutorial on YouTube to make the navba ...

Is it possible to assign a variable to an Ionic datetime input property?

I am trying to pass a variable, someVar, into the max attribute (input property) of an Ionic 2 (and Angular 2) DateTime component. It seems like it only accepts a hardcoded string such as max="2017-08-31". HTML <ion-datetime displayFormat="DD/MM/YYYY" ...

Are the references to clip-path: path() on MDN and other sources inaccurate?

I'm attempting to achieve a simple task by using clip-path with the path property and having it scale to fit the entire size of the containing div. After researching extensively, I came across mentions of the [geometry-box] property in some places, bu ...

Struggling to extract data using Scrapy and facing difficulties with the css selector

I'm struggling to extract only the title. response.css('.goods_property_color a.current').extract() <p id="select-attr-0" class="attr-choose clearfix goods_property_color" data-type="Color"> <span class="property-title">Color ...

Utilizing three.js to import and render SVG paths from images

I am attempting to bring in svg paths into three.js taken from svg images. I came across this example that utilizes the following format: obj.paths = [ /// Taipei City "M366.2182,108.9780 L368.0329,110.3682 L367.5922,112.4411 L369.9258,116.0311 L ...

Struggling with Primeng's KeyFilter functionality?

I've implemented the KeyFilter Module of primeng in my project. Check out the code snippet below: <input type="text" pInputText [(ngModel)]="price.TintCost" [pKeyFilter]="patternDecimal" name="tintCost" required="true" /> Take a look at my Typ ...

Updated the application to Angular 6 but encountered errors when attempting to run npm run build --prod. However, running the command npm run build --env=prod was executed without any issues

Running the command npm run build -- --prod results in the following error messages: 'PropertyName1' is private and can only be accessed within the 'AppComponent' class 'PropertyName2' does not exist in type 'AppCompo ...

Navigating through a diagonal path

Struggling to craft a diagonal navigation system similar to the images below. Despite its seemingly simplicity, I'm stuck investing too much time in figuring it out. My goal is for the menu container to smoothly reveal from right to left when clicking ...

I'm experiencing some unexpected behavior in JavaScript when I try to apply style changes using JavaScript. Is it possible that transitions are not occurring in both cases as expected?

Everything seems to be working fine with the transition, but when I skip using setTimeout and directly apply the transform, the div instantly reaches the end of the transition. function move(x, y, delay) { let el = document.getElementById('myDiv& ...

Styling the alignment of category menu columns using CSS

I'm looking for advice on customizing a menu in Magento. The current menu is functional, but I'm not happy with how the items are aligned on the website. Below, you'll find an image of the current menu and another one displaying my preferred ...

Design the layout of a table by styling alternating columns and using colspan cells

For a project, I need to create a table with alternating column background colors. However, there are certain rows that need to span multiple columns while maintaining the correct background color for each cell. You can view the code on this jsfiddle link ...