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

Creating an undo feature for a dynamically generated checklist of checkboxes

I am using a combination of javascript/jquery and html to dynamically populate the page with checkboxes. When you click "add", a new checkbox is created. I am now looking for a way to add an undo button that would delete the last checkbox created. Here is ...

How can you protect against XSS when using React server-side rendering and window.initialState?

When passing my initial state to React, I do it like this: window.__INITIAL_STATE__= ${JSON.stringify(initialState)} I have not implemented any protection against XSS. For example, if user-submitted content contains the following string: "<script>* ...

Having trouble implementing button functionality in a web app using JS, jQuery, HTML, and CSS along with an API integration

I am currently developing a web search engine that utilizes the Giphy API... However, I've encountered difficulties with the button function. I have created buttons to modify the page format and adjust the number of search results displayed.... The We ...

Clickable Slider Picture

I'm having a minor issue with a jQuery script that slides images on my website. The script itself is functioning properly, but I am trying to add links to each image. I've attempted placing <a href> and </a> tags around the <img> ...

How can Vue FullCalendar be configured dynamically?

I am trying to dynamically change some options in Vue FullCalendar, but the calendar is not applying them. You can check out my example here. The button at the bottom should set the maxTime option from 20:00 to 18:00. Is there a way to refresh the calen ...

Navigate to the editing page with Thymeleaf in the spring framework, where the model attribute is passed

My goal is to redirect the request to the edit page if the server response status is failed. The updated code below provides more clarity with changed variable names and IDs for security reasons. Controller: @Controller @RequestMapping("abc") public clas ...

Establishing a JSONP callback function for AJAX across different domains

$('#image_upload_form input').change(function() { if ( $(this).val() == '') return false; $('#image_upload_form').ajaxSubmit({ url: "http://www.test.com/offers/upload_image?callback=?", ...

HeatMap Visualizations Data

I have a HeatMap calendar project () where I am attempting to populate it with dynamic data. My approach involves extracting various dates from a file and converting them into milliseconds in order to create the necessary key-value pair for the calendar. ...

Exploring the possibilities: Establishing a Connection between Node.js and MySQL

I encountered an issue while attempting to connect node.js to MySQL. Despite having installed MySQL and the necessary libraries, I am unable to establish a connection. How can I troubleshoot this error? Additionally, what is the best approach for retrievin ...

Create a custom VueJS component by utilizing an npm package

Looking to navigate around X-frame restrictions? You can check out this npm package: https://www.npmjs.com/package/x-frame-bypass To make it work, include the following tag within your HTML: <iframe is="x-frame-bypass" src="https://example.org/">& ...

Update a table using jQuery/Ajax every 5 seconds

Seeking advice: I have a table displaying data from a database. Is there a way to make it update the information without having to reload the entire page? ...

Setting up scheduled MongoDB collection cleanup tasks within a Meteor application

After developing an app to streamline form submissions for my team, I encountered a problem during testing. Meteor would refresh the page randomly, causing loss of data entered in forms. To solve this, I devised a two-way data binding method by creating a ...

developing a custom modal using a button in a React project with Material UI

Hello everyone, I have a question regarding React. I am fairly new to React and need some assistance with adding a new function that creates a Modal. I want to call this function onClick when the add icon is pressed (line 43). Any help would be appreciated ...

The Material UI React Table onCellClick event is not triggering as expected when clicking on a

I am facing an issue with my react component that contains a material UI Table. I want to add an onCellClick event to each row of the table, but when I add it to each TableRow individually, the event doesn't get fired. However, if I add it to the Tabl ...

Incorporating a vanilla JS library (Pickr) into a NuxtJS page component

Recently, I've been experimenting with integrating the Pickr JS library (specifically from this link: [) into my NuxtJS project. To install it, I simply use NPM: npm install @simonwep/pickr In one of my NuxtJS pages - the create.vue page to be exac ...

A step-by-step guide on executing a callback function once the animation has finished with frame-motion

I'm facing an issue with my component called AnimatedText. After the animation is complete, I want the words filled in the underlineLines prop to be underlined. How can I achieve this? I attempted using the onAnimationEnd function, but it didn't ...

Using the React JS useState hook to toggle the state of a JSON object containing an array when a checkbox is clicked

When I submit my state as a stringified variable from a form to a POST request via a lamda server, it gets parsed and sent to sendgrid for templating. To loop over specific parts (multiple checkboxes) in JSON format, each with the same key but different va ...

Compel the browser to initiate a reflow by adjusting the CSS

I am currently in the process of building a responsive image slider without relying on jQuery, using CSS3 transitions. The setup is quite simple: there's a viewport with a UL inside, containing relatively positioned LIs that are left floated. Howeve ...

Request successful but receiving no response text and content length is zero

let req = new XMLHttpRequest(); req.addEventListener('load', function (data) { console.log(data) }, false); req.open("get", "/bar.txt", true); req.send(); Feeling a bit perplexed at the moment, I can't seem to figure out what's going ...

After clicking a button in AngularJS, how can you retrieve one of the two JSON files and store it in a $scope variable?

For instance, You have two JSON files: Json file #1: [{colorname:blue},{colorname:blue}] Json file #2: [{colorname:red},{colorname:red}] Within the controller, there exists a variable called $scope.color In the HTML code, there are two buttons named " ...