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

How to Handle Empty Input Data in JQuery Serialization

Having an issue with a form that triggers a modal window containing another form when a button is clicked. The second form includes an input field and send/cancel buttons. The goal is to serialize the data from the modal form and send it to a server using ...

How can I ensure that the images span the entire width of the page in ResponsiveSlides?

I am trying to make my images occupy the entire width of the page just like in this example: However, I have not been able to find a property for this. I'm new to using Jquery but I have a good understanding of Javascript Can someone please help me ...

Neglect to notify about the input text's value

Having trouble retrieving the text from a simple <input id="editfileFormTitleinput" type="text>. Despite my efforts, I am unable to alert the content within the input field. This is the code snippet I've attempted: $('#editfileFormTitleinp ...

Unable to assign default value to input field of type "time" in Chrome browser

I'm intrigued by Chrome's feature that automatically applies masking/formatting when using the type="time" attribute/value. However, I'm having trouble setting an initial value for the control. For example: <input type="ti ...

Using the Twit package on the client side: a step-by-step guide

I have been utilizing the Twit package for searching tweets. After executing the js file in the console, I am able to see the tweets but when trying to use them on my webpage, an error 'Uncaught ReferenceError: require is not defined' pops up. Th ...

The div within the button is failing to properly adjust to height settings

Check out this Fiddle I'm currently working on a social thumbs up button and I've encountered some challenges. In my button design, I have included a second div to accommodate the right side of it. However, despite trying to adjust its height us ...

Personalize Material design slider Label - final element

When using Material-ui, the default position of Slider's labels is centered: However, I require the labels to have a space-between position. For example: To achieve this, I tried using the '&:last-child' property for the markLabel clas ...

What impact does reducing the window size have on my header?

While working on a website with a navigation bar, I encountered an issue where the navbar was not responsive. When the window size was reduced, the nav bar shifted to an awkward position vertically, as shown in the first image. The navbar shifts below the ...

Tips for ensuring consistent sizing of card items using flexbox CSS

I'm attempting to create a list with consistent item sizes, regardless of the content inside each card. I have experimented with the following CSS: .GridContainer { display: flex; flex-wrap: wrap; justify-content: space-around; align-item ...

Adding a class to an img tag with TinyMCE

Currently, I am utilizing the TinyMCE editor as a standalone editor in my content management system (CMS). Within the CMS, there is an automatic setting that adds a curved border to any image tags within the cms div ID. In certain cases, I require the op ...

Is it possible to stream audio using a web socket?

I'm currently working on an app that streams audio from a microphone using web sockets. I'm struggling to play the web socket response in an audio control. Can someone please provide guidance on how to play audio buffer in an audio control? Your ...

Incorrect implementation of Bootstrap CSS in Jade template

Currently, I am leveraging Express to construct a website. However, there seems to be an issue with my jade template not displaying the bootstrap grid system accurately. Despite double-checking that my app path is correctly set through app.use(express.stat ...

Remove the bottom border from the active tab by utilizing the <div> and <a> elements

I am facing an issue with a tabbed menu on my webpage. While the menu is functioning correctly, I am struggling to remove the bottom border from the active tab. You can view all of my test code here. While I have come across solutions using <ul> an ...

The CSS code is effective on one page but fails to render on the other

I am in the process of creating a website for my jewelry business and it's my first time doing so. I'm facing an issue where the CSS styles are being applied to one page but not to the other, even though both HTML files have the same code linking ...

Customize Individual Rows in a React DataGrid

I am exploring the world of Material UI React data grid for the first time, and I am looking to add a unique background color to only the initial three rows. Each row should have a different color scheme that remains static, without any interactions trigge ...

The console is showing an error message stating that the variable "i" is not

Currently, I am developing the dashboard for my discord.js bot. While working on it, I encountered an issue where a variable I created to select an array from my JSON file is being reported as undefined by the console. $(function() { $.getJSON( "./data/ ...

Tips for displaying a div near the cursor's location when hovering in React JS

Looking for a way to show a hidden div at cursor position when hovering on Text item1 or item2. Check out the sample GIF animation in this Link My attempt using Jquery code inside React resulted in an error: $(".text-item").mouseenter(function ( ...

Incorporating a HTML layout with a JS backdrop

I have successfully implemented a JavaScript background and now I want to apply this background across all my PHP files. The issue is that the HTML code either overlaps with the JS content or appears behind it. How can I resolve this? Below is the JS fil ...

React JS progress circle bar is a simple and effective way to visualize

Currently, I am in the process of developing a progress circle bar that will function as a timer alongside sliders. Each slide is intended to have its own corresponding progress bar. While I have managed to create the bars individually, I am facing challe ...

Require the capability to bypass inline CSS styling

Currently facing an issue with a wordpress website I am constructing using iThemes Builder. The problem arises when integrating the plugin Jquery Megamenu, as the drop-down menu gets truncated due to an overflow:hidden property that cannot be overridden i ...