What is the best way to make the block rotate each time the button is clicked?

Specifically, I am having trouble getting the block to rotate properly.

Currently, the block only rotates once when clicked, but I want it to rotate each time I click in both directions.

Any suggestions on how to achieve this?

var now = 10;
$('.left').click(function() {
  $(".d").css('transform', 'rotate(' + now + 'deg)');
});
.r {
  width: 200px;
  height: 200px;
  border: 20px solid;
  border-radius: 50%;
}

.d {
  width: 200px;
  height: 200px;
  border: 20px solid blue;
  border-radius: 50%;
}

.wrapper {
  width: 230px;
}

.parent {
  width: 240px;
  height: 240px;
  position: relative;
}

.r,
.d {
  position: absolute;
}

.d {
  border-right-color: transparent;
  border-top-color: transparent;
  transform: rotate(45deg);
}

.btns {
  display: table;
  margin: 10px auto;
}

button {
  margin-left: 10px;
}
<div class="wrapper">
  <div class="parent">
    <div class="r"></div>
    <div class="d"></div>
  </div>

  <div class="btns">
    <button class="left">+</button>
    <button class="right">-</button>
  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Originally, I wanted to create a control similar to the one shown in the screenshot.

However, due to my limited knowledge of JS, I opted for an HTML and CSS + jQuery approach.

I considered using SVG but wasn't sure how to implement the necessary changes.

I appreciate any assistance you can provide!

https://i.stack.imgur.com/g1s7Q.png

Answer №1

To ensure continuous incrementing, it is essential to maintain a running total in the variable now. See the example below:

var now = 0;

$('.left').click(function() {
  now += 10;
  $(".d").css('transform', 'rotate(' + now + 'deg)');
});

$('.right').click(function() {
  now -= 10;
  $(".d").css('transform', 'rotate(' + now + 'deg)');
});

Furthermore, the issue with spaces between rotate and ( must also be addressed, as another contributor has rightly pointed out.

If you require additional assistance, refer to this CodePen: https://codepen.io/MSCAU/pen/MZgqop

Answer №2

I believe the issue lies in the excessive spaces which I have removed to assist you better.

var currentAngle = 10;

    $('.left').click(function() {
      currentAngle += 10;
      $(".d").css('transform', 'rotate('+currentAngle+'deg)');
    });

    $('.right').click(function() {
      currentAngle -= 10;
      $(".d").css('transform', 'rotate('+currentAngle+'deg)');
    });

Answer №3

If you need to continuously adjust the angle by `10 deg`, here is a simple example to demonstrate this:

$(function(){
      var currentAngle = 10,
          rotationCount = 0;
      
      $('.left').click(function() {
        rotate(rotationCount + 1);
      });
      
      $('.right').click(function() {
        rotate(rotationCount - 1);
      });

      function rotate(newRotationCount) {
        var calculatedAngle = ((newRotationCount) * (currentAngle));
        $(".d").css('transform', 'rotate('+ calculatedAngle +'deg)');
        rotationCount = newRotationCount;
      }

    });

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 some trouble integrating CSS with JavaFx; encountering an error

Can anyone help me with using CSS in JavaFx? I'm encountering an error while trying to add a CSS file (style.css) to my fxml file through stylesheets. The error message I'm getting is: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged W ...

Is there a way to assign a value of 1 to the input-group spinner?

At first, I thought this problem would be easy to solve, but now I'm finding myself stuck. My goal is to have the input-group spinner display the value 1 when a user opens the application. <input id="option1" type="number" min="1" value ="1" ...

Steps to avoid NuxtJS from merging all CSS files

Currently, I am working on a NuxtJS website that consists of two main pages: index.vue and blog.vue. Each page has its own external CSS file located in the assets directory, named after the respective vue file (index.css and blog.css). However, during test ...

What is the best way to create a div with a smaller background color than its content?

Can someone help me with adjusting the size of a gray box in this code so that it is smaller than the text inside it? .media-body { background-color: gray; } <div class="media-body"> <h5>Lagoa Azul</h5> <span class="texto-medi ...

Prevent the use of repetitive borders by utilizing display: inline-block

I've been experimenting with the behavior of div elements when used as a table, rather than using the traditional table element or display properties. The main reason for this is that I have found tables and display properties to be problematic for my ...

display upcoming schedule and time

How can I display the future date and time in the respective field components? See below for a sample code snippet: require([ "dojo/_base/lang", "dijit/registry", "dojo/ready", "dijit/form/TimeTextBox", "dojo/parser" ], function(lang, registry, ready ...

How to showcase content on bootstrap across varying screen dimensions

When designing my website, I envisioned having three different options presented upon entering the site. Depending on the size of the screen, I wanted to display varying content. For XL screens, I wanted all content to be visible, similar to the picture. ...

Preserve the location of a moveable div using jQuery

I have implemented draggable divs in my JSP page, allowing users to move them around freely. After dragging the divs, I would like to save their positions either in a cookie or database for future reference. Could you please advise on the best way to ach ...

Guide to implementing an ES6 template within an HTML anchor tag href

Hello there, this is my first time seeking assistance here. I have an ajax request returning a JSON response. Now, I am aiming to use the response data to dynamically generate recipe titles and also create clickable links with that data. $( window ).load( ...

Tips for creating a responsive navigation bar using your own custom CSS styling

While working on a website using bootstrap, I encountered an issue with responsiveness when adjusting the text size and gap of the navigation items. By increasing the size and adding a gap through fs-4 and style="gap: 10vh;", the default small si ...

Unable to produce scrolling animation using JavaScript

I'm trying to implement a feature on my website where the page scrolls with a sliding effect when the user presses the "Scroll" button. However, I've encountered issues as it doesn't seem to work despite adding the necessary tags to my HTML ...

Is there a way to automatically transfer the ID from the first table to the id_position field in the second table upon clicking submit?

I'm dealing with two tables and I need to figure out how to add the id of the first table to the position_id field of the second table upon submitting. My request is done through ajax. The fields for the second table are dynamic, meaning you can keep ...

Issues with the use of overflow:hidden

I need to create 5 divs aligned horizontally next to each other, spanning the width and height of the window. Each div should occupy 20% of the window width, and any overflow content should be hidden. Below is the CSS code for two of the divs: #container1 ...

Other options for positioning background in SVG or using CSS within SVG could include center center alignment settings

I am currently developing a website where I have a div covered by an SVG. When I use this SVG as a background image and apply background-position: center center, it functions as expected. However, my issue arises when I try to add additional CSS in this ma ...

Press the HTML link to interact with a text using JAVA

I am currently working on creating a class that is able to interact with an A text/button in HTML. The webpage requires a username and password, so I have utilized JSOUP to parse the document and insert the user and password details using the .data method: ...

Toggle the visibility of HTML elements by utilizing a JavaScript checkbox event

I have put together these JavaScript functions to hide the delivery address fields on my shopping cart address form if the goods are being sent to the billing address. The functions control the visibility of the HTML wrapped by... function getItem(id) { ...

Ensuring that the image perfectly fills the entire screen on an iPhone X using React Native

Looking for a solution to make my image fit the entire screen on the iPhone X simulator. I've tried adjusting the imageContainer width to "100%" and the container that encompasses everything, but haven't had any luck. Would appreciate any suggest ...

choosing a specific element with jQuery to be used for future purposes

I'm having some trouble with the following code snippet: var star = $("._container > favorite"); var symbol = $(star.parentNode.parentNode).attr("symbol"); var exchange = $(star.parentNode.parentNode).attr("exchange"); After running ...

placing a command button to the right side

Currently, I have successfully implemented a graphical solution using jsf2.2 primefaces 6.0 to allow users to view, download, and print pictures through the galleria component of primefaces. However, I am facing an issue with positioning the print command ...

"Sparkling div animation with the use of jQuery's .hover() function

<script> $(document).ready(function () { $("#logo_").hide(); $("#logo_learn").hide(); }) function sl() { $("#logo_learn").slideToggle(500); $("#logo_").fadeIn(); } function hl() { $("#logo_learn").slideToggle(500); $("#logo_ ...