Adjust the angle of an object precisely using a transform upon hovering over a designated button

I am looking to design a menu that always keeps the arrow pointing at the button I hover over with my cursor. If I don't hover on any button, then it should stay in the position of the last button I hovered over.

HTML:

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Test</title>
</head>
<body>
  <div class="container">
    <div class="first row">
      <button class="two btn btn-default col-xs-2  col-sm-offset-5   left">Second</button>
    </div>
    <div class="second row">
      <button class="one btn btn-default col-xs-2 col-sm-offset-2   left">First</button>
      <p class="col-xs-4 text-center">
        <span class="glyphicon glyphicon-arrow-up"></span>  
      </p>
      <button class="three btn btn-default col-xs-2   left">Third</button>
    </div>
    <div class="third row">
      <button class="four btn btn-default col-xs-2  col-sm-offset-5   left">Fourth</button>
    </div>
    </div>

</body>
</html>

SCSS:

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

.container {
  width: 500px;
  height: 300px;
  background: lightgrey;
  margin-top: 50px;
  .first {
    height: 100px;
    display: flex;
    align-items: center;
  }
  .second {
    height: 100px;
    display: flex;
    align-items: center;
  }
  .third {
    height: 100px;
    display: flex;
    align-items: center;
  }
  p {
    color: white;
    font-size: 50px;
    transform: rotate(-90deg);
} 
}

.rotate-first{
   transform: rotate(0deg);
   transition: all 0.5s ease;
}
.rotate-second{
   transform: rotate(90deg);
   transition: all 0.5s ease;
}

.rotate-third{
   transform: rotate(180deg);
   transition: all 0.5s ease;
}
.rotate-fourth{
   transform: rotate(270deg);
   transition: all 0.5s ease;
}

JQuery:

$("button.one").hover(function(){
    $("span").addClass("rotate-first");
});
$("button.two").hover(function(){
    $("span").addClass("rotate-second");
});
$("button.three").hover(function(){
    $("span").addClass("rotate-third");
});
$("button.four").hover(function(){
    $("span").addClass("rotate-fourth");
});

You can view the codes here.

Is it achievable with CSS alone or does it require JavaScript?

Answer №1

Indeed. The solution involves removing the various rotation classes upon hover before adding back the appropriate one. This ensures that only one of the four classes is active at a time, preventing any getting stuck.

$(function(){
  var spanClasses = 'rotate-first rotate-second rotate-third rotate-fourth';

  $("button.one").hover(function(){
      $("span").removeClass(spanClasses).addClass("rotate-first");
  });
  $("button.two").hover(function(){
      $("span").removeClass(spanClasses).addClass("rotate-second");
  });
  $("button.three").hover(function(){
      $("span").removeClass(spanClasses).addClass("rotate-third");
  });
  $("button.four").hover(function(){
      $("span").removeClass(spanClasses).addClass("rotate-fourth");
  });
});

Regarding whether this can be achieved using just CSS: In my opinion, no. You could apply a rotation on hover with CSS, but the arrow would revert to its default position once the hover state is removed. Therefore, utilizing JavaScript to toggle classes as demonstrated is the most optimal approach.

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

Submitting an intricate item to a WCF REST API using JQuery

Currently, I am attempting to send a complex object to my WCF REST service. Utilizing this method seems to be the most straightforward way to upload a Stream type object along with other parameters to an endpoint simultaneously. Service: [OperationContra ...

Problem with Pathjs routing

Can anyone help with this routing issue I'm having? Path.map("/(:page_1)(/:page_2)/").to(funct); The route is not matching for: /projects/index2/ It matches /anything, but not /anything/anything If you have any ideas, please share! ...

What could be causing the failure of my JQuery AJAX call?

Check out this JQuery AJAX method I wrote: function fetchResource() { alert('start'); $.ajax({ type: "POST", url: "About.aspx/GetRes", data: "{}", contentType: "application/json; charset=utf-8", d ...

Combining DataTables with multiple tables sourced from various JSON arrays

I am trying to display data from two different arrays within the same JSON source in two separate tables, but my code seems to be malfunctioning. JSON Information: { "Policies": [ { "name": "A", "id": "1", "score": "0" } ], ...

Adjust the height of the second column in Bootstrap 4 textarea to fill the remaining space

I am facing an issue with my layout where I have 2 columns, one containing some inputs and the other only a textarea. The problem is that I want the textarea in the second column to extend and fill the remaining height of the first column, but so far I hav ...

When hovering over it, an element within an li tag appears larger than its parent element

I'm currently working on a project for my school classes and running into an issue with CSS. I'm trying to enclose everything in boxes, but it's proving to be quite challenging. The main problem I'm facing is that when I hover over the ...

Struggling to figure out how to make this Vue function work

I am working with a list of tasks, where each task is contained within a div element. I am looking to create a function that changes the border color of a task to red when clicked, and reverts the previous task's border to normal. I have two files: &a ...

Picture appears to be off-center

I created a loginPage.php file with the following content: <?php //some php stuff ?> <!DOCTYPE html> <html> <head> <title>Login Form Design</title> <link rel="stylesheet" type="text/css" href="stylelogin.c ...

Communication between Laravel and controller using AJAX for exchanging information

I have a specific AJAX function being called from a view: function gatherProductData() { var productIds = []; $('#compare-widget tbody tr').each(function(i, ele) { productIds[i] = $(ele).data('product-id'); }); ...

AJAX is currently not operational

I've spent the last couple of hours trying to troubleshoot this issue with no luck. Can anyone provide any assistance? For some reason, the table fails to load when the button is clicked. Below is the relevant code snippet from my header: <head&g ...

How can I prevent a browser from caching a CSS file when the file is updated?

I have a primary layout file that is utilized in the majority of views. Within this layout, I am integrating a module using the Grails resources plugin. <r:require module="core"/> The definition of modules can be found in the conf/ApplicationResour ...

Troubleshooting a CSS problem on a table filled with images - See attached screenshots for

I've been working on a project involving my phpmyadmin database generating a series of 6 images on my website. I've placed them in a table, but now I'm facing some challenges - even though it seemed simple at first! The goal is to display t ...

Footer not disappearing

Need assistance! My footer isn't showing up properly at the bottom even after applying clear:both. Can someone help please? If anyone can provide guidance, it would be greatly appreciated. Thanks for all your help in resolving the issue. ...

Preserving Scroll Location Through Back Button Usage and Ajax Manipulation of the Document Object Model

Currently, I am incorporating a feature where results are loaded in using ajax with an infinite scroll. However, there is an issue when a user clicks on an item in the list and navigates away from the page - upon returning by clicking the back button, they ...

Problem with jQuery ajax redirection

I have been using the script below to dynamically load pages on my website: var xhr; function Ajax(afile,adiv,arun,loader) { // Loading info if(loader==null) { gi("loaderdiv").style.display='inline'; } // Process Ajax var htm = afile.split(&apo ...

How to incorporate Django syntax into CSS styling

Imagine a scenario where a Django site is equipped with numerous stylesheets. CSS and JS files are located in the static/ directory within an app's folder or in the global site directory. Various color themes are employed across different pages, neces ...

What can be done to eliminate the narrow gap between my <input> and <span> components?

Here is what I have: <input style="width: 20px" data-ng-model="row.number" type="text" /> <span style="width: 20px">-</span> Is there a way to eliminate the small gap between the <input> and <span>, as well as centering the ...

The :contains method in jQuery functions smoothly in Firefox, Safari, and Chrome, but unfortunately does not work

My code on JSFiddle is having some compatibility issues with the jQuery :contains selector specifically in Internet Explorer versions 7, 8, and 9. The code works fine in Firefox, Safari, and Chrome. You can find the working code here. I tried making the ...

What is the best way to replicate an Ajax call similar to the way Postman does it?

After successfully retrieving JSON data from a URL using Postman, I received an array as a JSON string. Below are screenshots of the process: https://i.stack.imgur.com/6uLSS.png https://i.stack.imgur.com/vDF0L.png I now want to achieve the same result u ...

Removing all jQuery scripts from the WordPress admin panel: A step-by-step guide

Currently, I am developing a plugin and incorporating the WordPress color picker In the color_picker.js file: jQuery(document).ready(function($){ jQuery('.cp-field').wpColorPicker(); }); And in the index.php file: add_action('admin_i ...