What is the best way to update the hover state of an element when it is being

Simply put: when you click on an image, it will move with a CSS transition. The issue arises when the hover effect of the image persists even after it moves away from under the mouse.

I have an image without a border. Upon clicking, the page zooms in using zoomooz. Hovering over the image reveals a border that stays visible while the page is zoomed in. If you click anywhere on the page, it zooms back out. However, if you click on the image to zoom out and leave the mouse still, the image retains the hover state, keeping the border even when the mouse is not hovering over it.

It's logical that this happens because there is no event triggering the change. I attempted to address this by adding a style change only to the click event, but then there's no animation due to the absence of a CSS transition (

$("img").css("border-color","rgba(0,0,0,0)");)
)

Check out the JSFiddle here

This is my HTML:

<body>
    <img src="https://i.imgur.com/e1TsDx0.png" id="abc"/>
</body>

CSS

  img {
    width: 100px;
    border: 1px solid black;
    border-color: rgba(0, 0, 0, 0);
    margin-left: 10px;
    transition: border-color 600ms;
  }
  img:hover {
    border: 1px solid black;
    transition:border-color 0s;
  }

  .zoomedimg {
    border-color: rgba(0, 0, 0, 1);
  }

Javascript:

$(document).ready(function() {
  $("img").click(function(evt) {
    event.stopPropagation()

    if ($("img").hasClass('zoomedimg')) {
      $("img").removeClass('zoomedimg');
      $("body").zoomTo();

    } else {
      $("img").addClass('zoomedimg');
      $("img").zoomTo();
    }
  });
  $(window).click(function(evt) {
    $("body").zoomTo({});
    $("img").removeClass('zoomedimg');
  });
});

Closely related to these questions:

  • How to remove hover state when the element moves This had a very sober answer, which in that example I could not get to work. I did try setting the border color when I clicked the image like in that solution. But then the changing border doesn't count as a transition so it will not animate.

  • Hover state is maintained during a transition even if the element has gone This had a very extensive answer, but I didn't really understand how to apply it to my situation.

Edit: Junaid Ahmed provided a solution for creating the hover transition using jQuery and a class. When you click to zoom out, removing the "hover" class also removes the border. However, a new issue surfaces:
if you hover over the image while clicking and continue to hover until the zoom out completes, the border disappears and does not return until you mouse out and back over again.
Any suggestions on how to solve this?

Answer №1

According to @Jason, one way to eliminate the hover effect is by using CSS and implementing the hover effect with JS/JQuery instead. Take a look at this modified version on JSFiddle

CSS Code:

img {
  width: 100px;
  border: 1px solid black;
  border-color: rgba(0, 0, 0, 0);
  margin-left: 10px;
  transition: border-color 600ms;
}
img.hover {
  border: 1px solid black;
  transition:border-color 0s;
}

.zoomedimg {
  border-color: rgba(0, 0, 0, 1);
}

JS Code:

$(document).ready(function() {

    $("img").on('mouseover', function(){
    $("img").addClass('hover');
  });

    $("img").on('mouseout', function(){
    $("img").removeClass('hover');
  });

  $("img").click(function(evt) {
    event.stopPropagation()

    if ($("img").hasClass('zoomedimg')) {
      $("img").removeClass('zoomedimg').removeClass('hover');
      $("body").zoomTo();

    } else {
      $("img").addClass('zoomedimg');
      $("img").zoomTo();
    }
  });
  $(window).click(function(evt) {
    $("body").zoomTo({});
    $("img").removeClass('zoomedimg').removeClass('hover');
  });

});

Answer №2

Implement a simple state switch using a variable:

<script>
var currentState;

function toggleState() {
if (currentState == 1){
/* add your code to change the style here */
currentState = 0;
}else{
currentState = 1;
}
}

</script>

<img onclick="toggleState()">

Feel free to modify the code according to your requirements.

Answer №3

I made some changes, take a look.

$(document).ready(function() {
  $("img").hover(function(evt) {
  $("img").addClass('zoom-border');
    event.stopPropagation()

    if ($("img").hasClass('zoomedimg')) {
      $("img").removeClass('zoomedimg');
     

    } else {
      $("img").addClass('zoomedimg');
      $("img").zoomTo();
    }
  });
  $(window).click(function(evt) {
    $("body").zoomTo({});
    $("img").removeClass('zoomedimg');
    $("img").removeClass('zoom-border');
  });

});
img {
    width: 100px;
    
    border-color: rgba(0, 0, 0, 0);
    margin-left: 10px;
    transition: border-color 600ms;
  }
  
  .zoom-border{
    border: 1px solid black;
    transition:border-color 0s;
  }
  .zoomedimg {
    
    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://dropfruitduo.github.io/jquery.zoomooz.min.js"></script>
<body>
    <img src="https://i.imgur.com/e1TsDx0.png" id="abc"/>
</body>

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

Using the react-bootstrap library, I have successfully integrated a Navbar

The navigation bar below is not functioning properly, and the container is causing the links to lead to a 404 error page. I have attempted writing it in various formats: <Nav.Link href="" >Name</Nav.Link> <Nav.Link href={"&qu ...

Align the form and button HTML elements in the center of the page horizontally

I have been utilizing bootstrap for the styling of my page. However, I have successfully centered other elements vertically and horizontally, except for the input field and button. The input field and button are currently being displayed on the left side ...

ES6/When.js - Dealing with promise fulfillment within a recursive chain of promises

I am currently implementing a promise chain that is recursive, meaning it calls itself until a value reaches zero. The functionality works as expected, however, I noticed that when the promise finally resolves, the output contains extra "undefined" values. ...

Unable to establish a connection with the docker container

I have developed a server-api application. When running the app on my localhost, only two simple commands are needed to get it up and running: npm install npm start With just these commands, the app runs perfectly on port 3000. Now, I am trying to dock ...

Leveraging an external JavaScript library within a functional component

I am attempting to utilize the CircleType.js library in order to display text in a circular format. However, as someone who is still learning React, I am unsure of how to properly integrate this library into my project. Despite following the documentation ...

fixed width div with width proportion

experts. I'm currently experimenting with a spinning cube effect for my gallery and I recently came across Paul Hayes' experimental cube on his website (it's best viewed in Google Chrome). However, I am facing a challenge in getting it to w ...

Guide to automatically sending users to a subdomain depending on their IP address location

Currently, my website is built using Node Express and I have a specific requirement. I want to redirect users to different subdomains based on their current location. For example, if a user in Singapore accesses site.com, they should be redirected to sg. ...

Update the text on Bootstrap Tooltip when it is clicked

I am looking to update the content of my tooltip when it is clicked. Below is the current code snippet I am using: function myFunction() { var copyText = document.getElementById("myInput"); copyText.select(); document.execCommand("copy"); ...

Creating an active tab using Material UI on a particular route

I've encountered an issue with Material UI tabs and need help figuring out how to resolve it. I have a navbar with three different tabs that link to separate URLs, but two of them are quite similar. Take a look at my code snippet below: <Tabs indic ...

Is it possible to customize the pagination-control labels from numbers (1 2 3) to different values, such as 'Anything1 Anything2 ...', by utilizing ngFor* with an array in ngx-pagination?

Is there a way to customize ngx-pagination's pagination control? Currently, the page numbers are displayed as '1 2 3' but I would like to replace them with specific string values from an array. <pagination-controls (pageChange)=& ...

Changing various $scope object properties using ng-model

Can you help me figure out why the $scope.data variable is not updating when I input valid values in the form fields? How can I solve this issue? Check out the code example on Codepen: http://codepen.io/uloco/pen/jboorN HTML <div ng-app="app" ng-contr ...

Enhancing Navbar Design with Tailwind CSS in NextJS and React

I have not worked with React in a while and just started learning Next.Js. I am trying to figure out how to change the background of my Navbar using Tailwind CSS based on a boolean value "current" (true/false) depending on which page the user is on with Ne ...

Can an enterprise level web application be effectively built using [HTML5 + jQuery] (without ASP.NET) + WCF?

We have been utilizing ASP.NET web forms for a long time to get some perspective. While we understand the advantages of MVC over web forms, there's now a suggestion on the table to skip all abstraction layers and move directly from a pure .HTML page ...

Switch out the image source and add attributions until the AJAX call is complete

<div id="fbAdsIconDiv" class="social_icon"> <img src="~/Content/images/fbAdsAddOn_1.png" onclick="toggleImage(this)" id="fbAdsAddOn" data-toggle="tooltip" title="click to enable" class="confirmBox f ...

How can I completely alter the content of aaData in jquery.dataTables?

Looking to completely change the content of a datatable purely from a javascript perspective, without relying on Ajax calls. I've attempted using the following script: oTable.fnClearTable(); oTable.fnAddData(R); oTable.fnAdjustColumnSizin ...

Automatically expand a child node in the tree menu

Hey there! I've got a tree menu that I'm working with. I'm looking to have a specific node in the tree menu automatically open by default. Essentially, I want a particular node to be open when the page is refreshed. For instance, in this e ...

Apply the CSS style to make one element identical to another, while modifying a single property

I am currently working with the following CSS: input[type="text"] { border: 2px solid rgb(173, 204, 204); height: 31px; box-shadow: 0px 0px 27px rgb(204, 204, 204) inset; transition:500ms all ease; padding:3px 3px 3px 3px; } My goal i ...

Using Vue to dynamically add a CSS class based on a specific condition

I am in the process of creating a custom image slider with vuex and I would like to assign a specific class to the navigation dots used for sliding through the images. When a dot is active, it should have a class called dotActive. I intend to use the activ ...

Attempting to verify the presence of a parent element with a specific attribute in XML using jQuery

I am currently working on creating an if statement that will check for the existence of a parent with a specific ID in an external XML file. If the parent exists, I want to display its content. However, if the parent does not exist, I would like to output ...

How to achieve a Dropbox-inspired footer effect using CSS?

Take a look at dropbox's website here: Have you noticed how the footer with "Learn more" remains fixed at the bottom regardless of window resizing until you click or scroll down? position: absolute; bottom: 50px; left: 0; width: 100%; text-align: ce ...