Transform cursor with jQuery on hover and click events

Currently, I'm implementing an image zoom feature, and I want to incorporate two different mouse states for hover actions.

#1 Upon hovering over the containing <div>, the cursor should change to a 'plus' symbol.
#2 Once the user clicks on the image, the cursor should switch to a 'minus' symbol indicating that the image is now zoomed in. (while maintaining this state until clicked again)
#loop Clicking on the zoomed image should revert the cursor back to the plus symbol, which is the default hover-view.

I have ruled out using mousedown/up or :active as they do not fit my specific requirements. Therefore, I believe utilizing toggle is the most suitable approach.

As of now, I've attempted to implement this concept using the following code snippet: http://jsfiddle.net/fCe9B/. However, it does not function as intended. Depending on the positioning of the default hover image-call, either the toggle fails to replace the hover, or vice versa. My goal is to ensure both cursor states are displayed correctly. Can anyone assist me in resolving this issue?

CSS

.cursor {cursor:move;}
#box {height:300px;width:300px;background:blue;}
#box:hover {cursor:help;}

JQuery

$(document).ready(function(){
$("#box").click(function(){
$("#box").toggleClass("cursor");
});
});

Answer №1

If you want to change the cursor when hovering over an image, you can use this code snippet that checks for a specific CSS class.

   $(document).ready(function(){

      $(".box").hover(function(){
        if($(this).hasClass("zoomed")){
          $(this).removeClass("zoomed");
        }else{
          $(this).addClass("zoomed");
      } 
    });
  $(".box").click(function(){
      if($(this).hasClass("zoomed")){
        $(this).removeClass("zoomed");
      }else{
          $(this).addClass("zoomed");

      }
  });
});

CSS

.box {
    height:300px;
    width:300px;
    background:blue;
    cursor:w-resize;
}


.zoomed{
    cursor:crosshair;
}

HTML

<div class="box"></div>

Here's the jsfiddle link for you to test it out: http://jsfiddle.net/fCe9B/9/

Answer №2

You have the option to utilize either the crosshair cursor or you can opt for a personalized image such as a plus symbol:)

.cursor {
cursor : url(image/cursor.jpeg);
cursor : crosshair;

}

Afterwards, simply declare

on click .cursor { 
cursor : url(image/cursor.jpeg);
} 

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

Maintained grid column stability amidst various modifications

I have a complex 2-column grid layout, and the example shown here is just one part of it. The complete code follows a similar structure. I am looking for a way to ensure that the green box always stays close to the red box in the layout, without having a ...

Utilize AJAX to fetch and load JSON data for use in a different function

When a button is triggered, I have two functions that run. One function is called immediately, while the other is called once the processing triggered by the button is complete. I want to optimize the loading time by loading the JSON data from the AJAX req ...

Tips for modifying the background color of an individual row within a table using the Document Object Model (DOM)

I am trying to change the color of each row in a table only if the text inside the 4th cell is "DELAYED". I have created a function for this purpose, but the color is not changing as expected... function assignColor() { //retrieve all tr elements from ...

Label it as submit ID rather than value or label

Check out this awesome plugin called https://github.com/aehlke/tag-it. It's really cool! Here is the issue I'm facing: <input type="hidden" name="tags" id="mySingleField" value="Apple, Orange" disabled="true"> Tags:<br ...

Is it possible to capture and handle browser-specific errors that occur during an AJAX call? I am having trouble locating and capturing these errors

My goal is to thoroughly test an AJAX call for potential errors by deliberately breaking it in various ways. The error callback in jQuery ajax does not provide detailed information like what the browser logs, which is what I need to capture. For example, C ...

Incorporating outside content into HTML

Currently, I am working on a project at my job which requires me to create a comprehensive help file with multiple chapters and extensive text all within one large HTML file. However, as the text will need to be translated into different languages, I am lo ...

Troubleshoot: Difficulty with accessing nested tag name using getElementsByTagName

I'm currently working with a div that contains a table and its data pulled from a database. <div id="content"> <table> <tbody> <tr> <th class="header" colspan="2">Food items include:</th> </tr> ...

Menu with a carousel feature in between each item

I am experiencing an issue with a carousel embedded within a menu using Twitter Bootstrap. <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="carousel slide" id="CarouselTest"> <div class="carousel-i ...

Transforming a CSS Clip Path into an SVG Clip Path without the need for an image

I have a basic HTML structure: .btn_clip { display: block; width: 524px; height: 170px; background: #ed1c23; font-size: 25px; color: #fff; text-align: center; clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%); line-heig ...

The AJAX POST response shows [object Object]{}

Here is the snippet of Javascript code I am working with: var paymentForm = $('#payment_form, .payment-invoices-container'); var ocodes = paymentForm.find('[name="ocodes"]:enabled'); alert(ocodes); $.post( 'https://URL/ajax- ...

Can jQuery effortlessly glide downward, come to a stop, continue downward, and then move upwards?

My webpage features a large table created and populated automatically every minute using ajax. The code structure is as follows: $(document).ready(function(){ setInterval(function(){ $.ajax({ //code to call backend, get the data, ...

Issue with datetime picker in JavaScript/jQuery: Input fields added dynamically are not showing the datetime picker

I am currently investigating why the datetime picker does not work on a dynamically added input block within a table cell. A code snippet below serves as a proof of concept for this issue. In its present state, the default input tag (id: dti1) functions co ...

Having trouble with Bootstrap's justify-content property? The "between" or "mr-auto" options

Having trouble with a simple problem, 1 - Why doesn't it work when I add an independent CSS class to the nav_element and set flex and justify-content: between to create space between the anchor tag and the div? 2 - What is the correct approach in Bo ...

The selection form does not display a preview of the options

I recently discovered that the CSS I added is causing the preview of select options in my form to disappear. Despite searching online, I have been unable to find a solution to this issue. Here is the problematic CSS code: .form-control{ height: 50px; ...

What is the best way to add content before a designated h2 heading?

My current struggle lies in using an ajax script to load data from a csv file and then prepending it to a specific h2 tag. Below is the script I have developed so far: <script> $.ajax({ url: 'csv_data.csv', dataType: 'text&ap ...

Utilizing Card Images in a Slider with Bootstrap 4

Is there a way to turn the card-image-top section into a slider, especially for mobile use? I want to showcase multiple product images in this section for my customers to see. Any simple solutions for achieving this? <div class="card" style= ...

Use this jQuery-animated menu to make hyperlinks hover with style!

Currently immersed in my exam project, I have integrated an animated jQuery navigation. I am aiming to set the text color to white when hovering over both the link itself and the menu icon that appears upon hovering over the <li> elements. Despite ...

css Repaint the CSS by filtering out list items with odd indexes

Having a list of over 20 items, I utilized the :nth-child(2n+1) selector to alternate the background color (even item in black, odd item in white). However, when using the jQuery Isotope plugin to filter out specific items by adding a .isotope-hidden class ...

Establishing a parameter name within a jQuery Ajax request for sending JSON data via POST

I am making a POST request with JSON in this format: $.ajax({ type: "POST", url: "myurl.htm", contentType: "application/json", data: '{"val1":"something","stuff":[{"val1":"value1","data":"Someone"}]}', dataType: "json" } ...

Issue with Bootstrap NavBar collapsing properly on first element

I'm currently working on developing a responsive navbar. Everything seems to be functioning well in larger window sizes, but when I view it on an xs screen, the first element of the navbar does not collapse properly. For reference, here are two exampl ...