Hover and Click Card Turning

My card can both hover and click, but there seems to be a small glitch.

After clicking on the card and then moving the cursor away from the front, the hover effect doesn't work correctly. It immediately flips back to the front side. The hover effect only functions properly when hovering out from the back side.

What part of the code am I overlooking to fix this hover glitch?

Your assistance with this matter is greatly appreciated.

document.querySelector(".card-flip").classList.toggle("flip");
$('.card-flip').bind({
  click: function() {
    $('.card-flip .card').toggleClass('flip');
  }
});
.card-flip {
  -webkit-perspective: 1000px;
  perspective: 1000px;
}

.card-flip:hover .flip, .card-flip.hover .flip {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

.card-flip,
.front,
.back {
  width: 100%;
  height: 480px;
  -webkit-transform-style: preserve-3d;
}

.flip {
  transition: 0.6s;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  position: relative;
}

.front,
.back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
}

.front {
  z-index: 2;
  transform: rotateY(0deg);
  -webkit-transform: rotateY(0deg);
}

.back {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css'>
<section style="height:40px;">&nbsp;</section>
</head>
<body>
    <section>
        <div class="container">
            <div class="row">
                <div class="col-sm-6 col-lg-4">
                    <!-- Card Flip -->
                    <div class="card-flip">
                        <div class="flip">
                            <div class="front">
                                <!-- front content -->
                                <div class="card">
                                    <img class="card-img-top" data-src="holder.js/100px180/" alt="100%x180" style="height: 180px; width: 100%; display: block;" data-holder-rendered="true">
                                    <div class="card-block">
                                        <h4 class="card-title">Front Card</h4>
                                        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                                        <a href="#" class="btn btn-primary">Go somewhere</a>
                                    </div>
                                </div>
                            </div>
                            <div class="back">
                                <!-- back content -->
                                <div class="card">
                                    <div class="card-block">
                                        <h4 class="card-title">Back Card</h4>
                                        <h6 class="card-subtitle text-muted">Support card subtitle</h6>
                                    </div>
                                    <img data-src="holder.js/100px180/?text=Image" alt="Image [100%x180]" data-holder-rendered="true" style="height: 180px; width: 100%; display: block;">
                                    <div class="card-block">
                                        <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- End Card Flip -->
                </div>
            </div>
        </div>
    </section>
    <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js'></script>
    <script src='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/holder/2.9.4/holder.min.js'></script>
</body>

Answer №1

To achieve the desired functionality, mouse events need to be utilized.

Method 1:

Implement the mouseenter and mouseleave events. Rather than relying on CSS, manually add or remove the flip class during these mouse events.

document.querySelector(".card-flip").classList.toggle("flip");
$('.card-flip').bind({
  click: function() {
    $('.card-flip .flip').toggleClass('flip-hover');
  },
  mouseenter: function() {
    $('.card-flip .flip').addClass('flip-hover');
  },
  mouseleave: function() {
    $('.card-flip .flip').removeClass('flip-hover');
  }
});
.card-flip {
  -webkit-perspective: 1000px;
  perspective: 1000px;
}

.card-flip .flip-hover {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

.card-flip,
.front,
.back {
  width: 100%;
  height: 480px;
  -webkit-transform-style: preserve-3d;
}

.flip {
  transition: 0.6s;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  position: relative;
}

.front,
.back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
}

.front {
  z-index: 2;
  transform: rotateY(0deg);
  -webkit-transform: rotateY(0deg);
}

.back {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

View the implementation in action on this fiddle.

Method 2:

If maintaining the hover behavior in CSS is preferred, incorporating the mouseenter event is still necessary. Simply add the flip class on mouseenter.

//document.querySelector(".card-flip").classList.toggle("flip");
$('.card-flip').bind({
  click: function() {
    $('.card-flip .flip-me').removeClass('flip');
  },
  mouseenter: function(){
    $('.card-flip .flip-me').addClass('flip');
  }
});
.card-flip {
  -webkit-perspective: 1000px;
  perspective: 1000px;
  transform: rotateY(0deg);
}

.card-flip:hover .flip, .card-flip.hover .flip {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

.card-flip,
.front,
.back {
  width: 100%;
  height: 480px;
  -webkit-transform-style: preserve-3d;
}

.flip-me {
  transition: 0.6s;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  position: relative;
}

.front,
.back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
}

.front {
  z-index: 2;
  transform: rotateY(0deg);
  -webkit-transform: rotateY(0deg);
}

.back {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

Check out the updated fiddle here.

Just a reminder, a new class flip-me has been introduced in conjunction with the existing flip class.

:)

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

Issue with submitting AJAX PUT request to database

Trying to send data back to the database using a WebAPI, everything works correctly with Insomnia sending this JSON object using the PUT method. { "movieId": 11, "customerId": 6, "dateRented": "2017-12-13T22:50:53.93", "bee ...

Browsing through a JSON document

Currently, I am attempting to extract schedule information from a JSON file called js/schedule.json. The format of this file is as follows: [ {"date":"12/06/2014","day":"Thursday","kick-off":"21:00","team1":"Brazil","team2":"Croatia","group":"A","stage":" ...

Determine the height of the input group when a span, input, and button are included

I have been attempting to create a 3 control input-group using Bootstrap 3.3.7, but I am encountering an issue where the height of the first addon does not match the height of the input field or button that follow. <div class="input-group"> < ...

Trouble aligning items in a fieldset with Bootstrap

While working on a wire frame, I am attempting to create the HTML structure based on it. The required structure that needs to be built is outlined in this diagram: https://i.sstatic.net/ypp2f.png Progress has been made on aligning elements using a two-col ...

Using AngularJS to pass a parameter to a directive's template

My basic set looks like this HTML <linear-chart chartData="myArray" height="666"> </linear-chart> JS ... ... app.directive('linearChart', function($window){ return{ restrict:'EA', template:"<svg ...

Trouble arises when attempting to establish an isolated scope within Angular alongside UI Bootstrap

My table of data is set up with AngularJS, and one of the columns is calculated using a function in the controller. On my webpage, I have a button that opens a modal. When I use UI Bootstrap to open the modal, it creates a new isolated scope (child of the ...

Transfer a file from the file:///var/mobile/Applications/ directory to an accessible location for reading in Cordova/PhoneGap

I have a unique 'whitelabel' app that customizes itself for each client by downloading image files from a configuration server. However, I am facing an issue where the images are not displayed and instead showing a "Not allowed to load local reso ...

Empty response received from AJAX .post request to PHP backend

I'm having trouble with creating a system to check the availability of usernames. I'm not receiving any response back and even though there are no errors in the console, all I get is a 200 OK status without any response, which should be stored in ...

JavaScript form validation issue unresolved

When I attempt to validate form fields using Javascript functions, they seem to not load or check the field upon clicking the submit button. <html> <body> <?php require_once('logic.php'); ?> <h1>New Region/Entit ...

Is it possible to execute a function defined within an eval() function using setInterval()?

const evaluation = eval(document.getElementById("codearea").value); setInterval(evaluation, 10); I am seeking a way to access a function that is declared within the eval function, for example: setInterval(evaluation.examplefunc, 10) I attempted ...

Customizing CSS according to specific URLs

I have two different domains - one ending with .nl and the other ending with .be. For instance, domain.nl and domain.be. Both domains share a similar overall style, but I want certain elements to have distinct styling depending on whether it is the .nl o ...

To align a div or text to the right using inline-flex, simply

I am facing an issue with aligning my animated links to the right. Currently, I am using display: inline-flex, which works well but doesn't align the links properly. I have tried using float: right for alignment, but I feel there must be a better way ...

Getting a file object with v-file-input in Nuxt.js

For my Nuxt.Js apps, I utilized Vuetify.js as the UI framework. In order to obtain the file object when uploading files, I incorporated the v-file-input component from Vuetify.js and wrote the following code snippet: <template> <div> ...

What causes the lack of sorting ability in AJAX responses?

I have integrated sorttable.js for table sorting, and my table is updated every 3 seconds with an ajax response. However, the response is not sorted in the manner I expect. Main Page <div id="resDiv"> <table id="myTable1" class="sortable"> ...

When an Ajax post request is made, the data being sent is appended to the JSON response

Having a dilemma with my ajax call: $.ajax({ url: '/assets/functions.php', type: 'POST', data: { "functionCall": "get-uploads", "type": type }, dataType: 'json', success: function (data ...

Setting headers in Node.js after they have already been sent to the client is not allowed

I'm currently enrolled in a node.js course on Udemy which seems to be outdated. I've encountered some errors that I'm struggling to resolve. Here's what I've tried so far: using next(); adding return res inside all if statements ...

Is it feasible to implement a jQuery dialog with an ASP.NET user control?

Having some trouble opening a user control in a jQuery dialog popup. It seems that none of the server-side events are being triggered, and I suspect that the UpdatePanels are not functioning either. Has anyone encountered this issue before? Is there a wor ...

Executing sequential jQuery ajax requests with a REST API

I need a little help and hope you can assist me with my somewhat challenging problem. I have a REST API collection where URL1 provides a list of names, and URL2 contains properties related to the names from URL1 (e.g., URL2?name=URL1.name). Example: URL1 ...

"Preventing duplicate entries in live search results: harnessing the power of jQuery, AJAX, and mongoos

Currently, the live search feature I'm using works flawlessly, but there is a minor issue. When I type "Chemical," it displays the following results: Chemical Engineer Chemical Entrepreneur Checmical People However, if I add "Engineer" after "Chemi ...

"Troubleshooting: Difficulty with hover function in jqgrid not functioning on colored rows

My JQGrid setup includes the following: <table id="grid"></table> var data = [[48803, "DSK1", "", "02200220", "OPEN"], [48769, "APPR", "", "77733337", "ENTERED"]]; $("#grid").jqGrid({ datatype: "local", height: 250, colNa ...