Magnific Popup is causing a glitch in my Ajax cart slider, preventing my code from functioning properly

Currently, I have implemented an Ajax cart slider that slides from right to left whenever an item is added to the cart. Customers are given the option to add a product with an image to their cart and can view the image directly from the cart by clicking on the "view image" hyperlink, which triggers a magnific popup.

https://i.stack.imgur.com/ggwqR.png

However, a problem arises when users click away from the displayed image as it inadvertently triggers the closing of the Ajax cart along with the picture. I have identified the code responsible for closing the Ajax cart:

`

    if(!$(event.target).closest('#cart-notification').length && !$(event.target).closest('[data-href="#cart-notification"]').length){
      if($('[data-href="#cart-notification"]').attr("aria-expanded") == "true" && $('[data-href="#cart-notification"]').attr("imgshow") == "false") {
        $("#cart-notification").removeClass("active");
        $('[data-href="#cart-notification"]').attr("aria-expanded","false");
      }
    }

`

To address this issue, I have decided to introduce an attribute called "imgshow" to the cart-notification class. When users click on "view image" in the cart, imgshow is set to "true" (when the magnific popup is triggered), otherwise it remains at "false" by default to enable the above-mentioned code functionality. If aria-expanded is "true" and imgshow is "false", the Ajax cart closes upon clicking away. Hence, I created the following code to set imgshow to "true" when viewing the image in the cart:

`

$(document).ready(function() {
  $('.imagepopup').magnificPopup({
                callbacks: {
                open: function(){
                   $('[data-href="#cart-notification"]').attr("imgshow","true");
                },
                close: function(){
                   $('[data-href="#cart-notification"]').attr("imgshow","false");
                }
            },
    type:'image'});
});

`

Although all attributes are updating correctly, the script does not seem to work properly, particularly when the magnific popup window is active. Strangely, the first block of script above disregards my callbacks only when the magnific popup is open. I am struggling to understand why this is happening. One possibility could be related to the creation of a new div class when the magnific popup is active, although it seems unlikely to interfere with my script block's functioning.

If anyone has insights on what might be causing this issue or how to resolve it, I would greatly appreciate your input as I am currently unable to identify the root cause of the problem.

Answer №1

If anyone ever needs the solution, I found it! It turns out you have to delay the "close" function to prevent everything from revealing all at once. Here's what worked for me:

$(document).ready(function() {
  $('.imagepopup').magnificPopup({
                callbacks: {
                open: function(){
                   $('[data-href="#cart-notification"]').attr("aria-expanded","false");
                },
                close: function (){
                  setTimeout(function(){
                   $('[data-href="#cart-notification"]').attr("aria-expanded","true");  
                                       }, 500);
                }
            },
    type:'image'});
});

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

Errors encountered: Navigation guard causing infinite redirection due to unhandled runtime issue

My Vue3 router is set up with the following routes: export const routes: Array<RouteRecordRaw> = [ { path: "/", name: "Browse Questions", component: HomeView, meta: { access: "canAdmin", }, ...

Sending a value to a text box using json data transmission

I am having trouble accessing data from a js file and fetching the value to an html text box. Despite trying, I'm unable to get the desired result. Below are the contents of samle.js file and jsonhtml.html file: { "var1": "1", "var2": "2" } <scri ...

Can a custom function be executed using setTimeout in Node.js?

I am currently facing an issue with stopping the execution of my code for 2 seconds so that all ongoing animations can finish. I am attempting to implement a delay using setTimeout in Node.js. Here is what I have tried: this.userActivity = function(a,b, ...

Difficulty encountered with fetching results using jQuery autocomplete with AJAX as the data source

My autocomplete feature is not working properly with my ajax data source. Here is my code: $("#id_q").autocomplete({ source: function (request, response) { $.ajax({ url: "/search/autocomplete/", dataType: "jsonp", ...

Teach me how to utilize Import / require() in Static Folder

I've been attempting this task for a while, but I'm unsure how to incorporate import or require into the express static folder. When I try to use it, I receive an error stating "require not defined" which makes sense since these are not supported ...

Managing the "Accept Cookies" notification using Selenium in JavaScript

As I use Selenium to log in and send messages on a specific website, I am faced with a cookie popup that appears each time. Unfortunately, clicking the accept button to proceed seems to be quite challenging for me. Here is an image of the cookie popup Th ...

Issue: Unable to find a compatible version of chokidar. Attempted chokidar@2 and chokidar@3 after updating npm to version 7.*.*

After using ejected CRA, it compiled successfully but then broke with the following error. The issue started to occur after updating npm from version 6 to 7. You can now view webrms in the browser. Local: http://localhost:3001 On Your Netw ...

How can jQuery determine the amount of line breaks within a div element?

My div wrap size is a percentage of the screen width, containing multiple .item divs that break into new lines as the window size decreases. I attempted to calculate the total number of boxes that could fit based on their widths compared to the container ...

What is the best way to change a JSON string into an array of mysterious objects?

I am currently working on a flashcard generator project and I am dealing with a JSON String that is quite complex. The JSON String contains multiple arrays and objects structured like this: data = [{"front":"What is your name?","back":"Billy"},{"front":"H ...

Are `<text>` nodes unable to utilize ligature fonts in CSS/SVG?

Check out this JsFiddle demo: http://jsfiddle.net/d0t3yggb/ <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <div class="material-icons">add add add add</div> <svg width="100%" height="100% ...

Angular: Exploring the differences between $rootScope variable and event handling

I have a dilemma with an app that handles user logins. As is common in many apps, I need to alter the header once the user logs in. The main file (index.html) utilizes ng-include to incorporate the header.html I've come across two potential solution ...

Attempting to format a number using a computed property and .toLocaleString() fails to execute

I am facing an issue with the formatting of a number displayed in a <p></p> element. The number is coming from a range input element that is bound to an amount data property using v-model. Even though I have a computed property to format the nu ...

Generating various arrays of data

I am currently facing an issue with creating separate datasets based on the month value. Despite my efforts, all month values are being combined into a single dataset in my code. Any assistance in dynamically generating different datasets would be greatly ...

Is there a way to position my character at the exact center of my mouse click coordinates instead of the top left corner?

In my game, players can click on a 2D HTML5 canvas to set a point for their character to move to. However, I've noticed that when I click, the character appears in the lower right corner of where my mouse is clicked. After some research, I realized th ...

Leveraging AJAX to return JSON data to the Android nanoHTTPd method

Hello everyone, I am attempting to send JSON data to my Android app using jQuery Ajax and NanoHTTPd as the web server. However, I'm having trouble accessing the POST data. Here is the code snippet where I make the Ajax call: var reqData = JSON.string ...

PHP functions triggered by ajax fail to function properly when called repeatedly

I have encountered an issue with my javascript function that calls a php script upon clicking a button or link to retrieve data. Everything works well, except when I attempt to call data from an already outputted content via the same function. Let me share ...

Modified the object path and updated the Dirty stages for nested objects within Vue state

Imagine having an object that contains arrays of objects in some properties. Whenever changes are made to any field, whether it's within an object in an array or the main object itself, you want to mark that object as "dirty" using a code snippet like ...

When attempting to set a dynamic src tag for embedding a Google Map in a React application, an X-Frame-Options

I'm attempting to display a specific location using an iframe embed from Google Maps (shown below): <iframe width="100%" height="200" frameBorder="0" scrolling="no" marginHeight={0} marginWidth={0} id="g ...

jquery mouse event does not register on touch-based devices

I have a mouse move event set up to scroll a div. However, when I try to access the functionality using a tab it does not work. How can I integrate this functionality onto a touch device? $(document).ready(function(){ $('#tim').on('mous ...

Steps to display the leave site prompt during the beforeunload event once a function has finished running

While facing a challenge with executing synchronous Ajax methods in page dismissal events, I discovered that modern browsers no longer support this functionality in the "beforeunload" event. To work around this issue, I implemented a new promise that resol ...