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

Strategies for deactivating the next button when the input field is blank

I have been working on creating a multiple login form and everything was going well, but I am stuck on how to disable the next button if the email input is empty. The input has a class of "email" and the button has a class of "btn-next." Any assistance w ...

Updating a rails value via ajax: Step-by-step guide

Currently on a journey to master rails and experimenting with new ideas :) My current challenge involves tackling two problems. Firstly, I aim to develop a dropdown feature enabling users to select the number of questions they wish to answer. Secondly, I ...

The Gulp task is not being recognized when I open the project in Visual Studio Code

Whenever I open gulp, I encounter an error related to minifying css and js files using gulp tasks. Despite my efforts to find a solution, the problem remains unresolved. Error Message: assert.js:374 throw err; ^ AssertionError [ERR_ASSERTION]: Task fu ...

Having trouble transferring a JQuery variable to PHP - what am I doing wrong?

Trying to transfer a jQuery variable to PHP, here is the code <label>Turnamen</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text"> <input id="checkturnamen" ...

html div elements may not align correctly

Currently, I am in the process of coding a website that contains numerous images. To assist me in organizing and arranging these images, I have turned to angularjs. Initially, everything was progressing smoothly until this issue arose: The layout is stru ...

Ensure that hyperlinks remain unbroken and do not split over two lines

When I generate links using $_GET [Array] to display in a box, I aim to have 5-10 links per line without any of them appearing on two lines. While I currently use {text-align:justify;} for consistent border spacing, I am seeking a method to prevent the lin ...

Prevent Duplicate Submissions with jQuery Ajax form disabling

Enhancing Ajax form submission. I am looking to disable the form from being submitted again until the previous submission is completed. Would it be sufficient to just disable the input or text area, or should I also disable the submit button? ...

Navigating AngularJS for User Authorization_REDIRECT

I've developed a login system using angularfire and firebase. Within this system, I have implemented a function that checks for the existence of authData when a user logs in or at other points in the application's flow. If authData is present, t ...

Is it possible to add a bottom border without having slanted corners?

I am looking to design a box with different colors - red on the left, right, and top, and grey at the bottom. However, I want the bottom border of the box to be flat. HTML <div class="ts"></div> CSS .ts { height:100px; width:100 ...

Resize the main container to fit the floated elements

I've been working on constructing a family tree, and the last part of the functionality is proving to be quite challenging for me. My family tree consists of list elements that are all floated to the left. Currently, when the tree expands beyond the ...

When hovering over the sidebar, it smoothly slides out. (ideally using only CSS)

I've been spending my free time working on a website, but I'm struggling to figure out how to create a navigation similar to the one featured on FontShop's site: . I want it so that when you hover over their logo (or in my case, a blank rect ...

Utilizing AJAX to seamlessly transfer id elements to a database

I have the following working code: <script> function displayUserData(str) { if (str=="") { document.getElementById("userDetails").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLH ...

Display a single image on the tablet and a distinct image on the computer

Currently, I am encountering an issue with my webpage located at . The problem lies in the right upper corner where a ribbon is located. However, when viewing the page on a tablet, the ribbon overlaps with my menu. To resolve this, I thought about displa ...

Exploring JSON array handling with jquery

Here is the JSON data I am working with: { "category": { "category_identification": "1", "category_name": "C1", "image_one": "1.PNG", "image_two": "1_.PNG", "logo": "LOGO_1.PNG", "category_description": "DESCRIPTION" }, "responseCo ...

Implementing Mouse Scroll Click Functionality in ASP.NET with C# (Without JavaScript)

How can I add a Mouse Scroll Click (Middle Button) event in ASP.NET and C#? I have already looked into the MouseWheel Event, but it did not provide the solution I was looking for. This is the order in which mouse events occur: MouseEnter MouseMo ...

Navigating websites: Clicking buttons and analyzing content

When looking to navigate a website's directory by utilizing buttons on the page or calling the associated functions directly, what language or environment is most suitable for this task? After experimenting with Python, Java, Selenium, and JavaScript, ...

Adjust the transparency of CSS elements using a jQuery selector

I am developing a grid of brand thumbnails with interactive icons. When one of the icons is hovered, I want all the icons to change opacity and become visible. Here is the current HTML structure: <div id="brands-wrapper"> <img class="brands" ...

A guide to implementing drag and drop functionality using JavaScript

I have developed a unique drag and drop application that features both a left and right panel. The issue I am facing is that when I drag the ball from the left panel to the right panel, it does get dragged successfully. However, the problem arises when the ...

What is the method for attaching a div to another div using javascript?

function displayContent(a) { var content = $("#" + a).html().trim(); alert(content); $('#temstoredivid').append( "<div class='maincover' id='maincov1'>" + " <div class='subcover' id='sub ...

jQuery makes it easy to set border colors for different sides using just one function

Hey there! I need some assistance with jQuery. In my project, I have a paragraph with a border and buttons that can change the sides of the border. Currently, I have separate functions for each button to change the border color. However, I'm looking f ...