What is the method to set up the "materializecss" modal with the "before" (beforeload) feature?

Trying to implement the modal functionality in my project using the materializecss framework but facing some challenges.

I have created a popup modal for an image with the code below:

HTML:

<div class="media-insert"> <a href="#img2" class="modal-trigger"><img src="images/slide1.jpg" class="img-responsive" /></a>
  <div class="modal" id="img2">
    <div class="modal-content"> <img src="images/slide1.jpg" class="img-responsive" /> <a href="javascript:void(0);" class="modal-action modal-close"><span class="mdi-navigation-close"></span></a> </div>
  </div>
</div>

JS:

$('.modal-trigger').leanModal();

CSS:

.media-insert .modal {
    width: auto;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    max-height: 100%;
    overflow: visible;
}

Encountering an issue where the image and modal wrapper are not aligned at the center. Additionally, the modal wrapper is not responsive like the fancybox popup.

Attempted to use the following code for callback:

$('.modal-trigger').leanModal({
    ready: function () {
        var modelImgW = $('.modal-content img').innerWidth();
        var modelImgH = $('.modal-content img').innerHeight();
        $('.media-insert .model').css({
            'height': modelImgH + 'px'
        });
        $('.media-insert .model').css({
            'width': modelImgW + 'px'
        });

    }
});

Still unable to resolve the issue. Any assistance would be greatly appreciated.

Thank you in advance!

Answer №1

I apologize for my previous response, I initially misunderstood the issue with the modal not functioning as expected...

It appears that some of the CSS styles you implemented were affecting the responsiveness and width of the modal. To center align the content within the modal, you simply need to add another rule. I have made adjustments to the CSS, replaced the images, and included the btn class - you can test it out by running the code snippet and clicking on "Full page" to observe the changes.

$('.modal-trigger').leanModal();
.media-insert .modal {
  max-height: 100%;
  overflow: visible;
}
.media-insert .modal .modal-content {
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/css/materialize.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/js/materialize.min.js"></script>
<div class="media-insert">
  <a href="#img2" class="btn modal-trigger">
    <img src="http://cdn.sstatic.net/stackoverflow/img/sprites.svg?v=1bc768be1b3c" class="img-responsive" />
  </a>
  <div class="modal" id="img2">
    <div class="modal-content">
      <img src="http://cdn.sstatic.net/stackoverflow/img/sprites.svg?v=1bc768be1b3c" class="img-responsive" /> <a href="javascript:void(0);" class="modal-action modal-close"><span class="mdi-navigation-close"></span></a> 
    </div>
  </div>
</div>

Referencing their documentation on button-triggered modals may provide further assistance.

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

A Multilingual Application developed using either AngularJS or the Ionic Framework

Having recently delved into both AngularJS and the Ionic Framework, my background is primarily in Microsoft technologies. I am currently working on developing an app that supports three languages: English, Arabic, and French. Drawing from my experience wi ...

Pictures squeezed between the paragraphs - Text takes center stage while images stand side by side

I'm struggling to figure out how to bring the text between the two images to the front without separating them. The images should be positioned next to each other with a negative square in-between, and the text within this square should be centered b ...

Having trouble accessing an injector service within the promise of a dynamically loaded JavaScript function that has been assigned to a global variable

Query I am facing an issue while trying to integrate PayPal with Angular. I am encountering difficulties when attempting to call an injected service inside a function of the promise returned. Any assistance in resolving this would be greatly appreciated. ...

Guide to importing an external JSON file into a JavaScript-based quiz on an HTML webpage

I am currently diving into the world of JavaScript and trying my hand at creating a quiz. Check out my simple quiz here Here are my specific inquiries: Is there a way to save the questions and answers in an external JSON file? Can I use a different fil ...

The script fails to start using npm start, however, it runs smoothly when using node server.js

For a while now, I've been facing an issue that I just can't seem to resolve. When I navigate to my project's src folder and execute `node server.js`, everything functions as expected. I can access the application at http://localhost:3000/cl ...

Encountering: error TS1128 - Expecting declaration or statement in a ReactJS and TypeScript application

My current code for the new component I created is causing this error to be thrown. Error: Failed to compile ./src/components/Hello.tsx (5,1): error TS1128: Declaration or statement expected. I've reviewed other solutions but haven't pinpointed ...

What is the process of invoking a function from a different component in Vue 3?

I attempted to use the $on method and this.$root.$refs.compname_component = this;, but encountered some errors. Please see my code below: formComponent.vue <template> <div v-if="showForm"> create Form </div> </templa ...

Fluid div causing navigation to display improperly

I am currently working on a navigation design where I want to have an image above each list item instead of having individual images for each item. It looks great when the screen is at full size, but as soon as I minimize it, the list items start stacking ...

I'm having trouble with an error in my React project on VS Code. Can anyone provide guidance

Could you please explain the meaning of this error message: errno: 'ENOENT', code: 'ENOENT', syscall: 'spawn cmd', path: 'cmd', spawnargs: [ '/s', '/c', 'start', '""', ...

Issue of Vue not resolving Ionic back button component

I'm having trouble figuring out why Vue is giving me a warning and the back button isn't functioning as expected: runtime-core.esm-bundler.js?5c40:38 [Vue warn]: Failed to resolve component: ion-back-button at <TheHeader titulo="Home&q ...

Unable to retrieve specific elements using this.$refs in Vue.js

I've encountered a peculiar issue. I'm trying to access certain elements within the created() hook, but I seem to be running into some trouble specifically with the refs object: created() { console.log(this.$refs) } // outpu ...

Current page with animated CSS3 menus

I found a cool menu example on this webpage: http://tympanus.net/Tutorials/CreativeCSS3AnimationMenus/index10.html. I want to modify it so that the current page's menu item has a different color from the others. For example, when I'm on the home ...

Even with the text field populated and clearly existing, Firefox is still throwing a null error when trying to retrieve it using

Hey there! I'm currently working on a sample HTML page and I'm facing an issue with retrieving data from a text field. No matter what I try, I keep encountering the following error: TypeError: document.getElementById(...) is null Let me share t ...

What is the best way to highlight specific text in React components that is passed from an object?

This is the page HTML content where an object is created. A portion of the description needs to be emphasized: const agencyProps = { title: "Managed agency selection", paragraph: "Strengten your onboarding process", videoImage: { ...

Tips for correctly formatting a string to use with the setValue() method

I am facing an issue with adding the value US punctuation + alphanumeric lowercase:[a-z0-9,.?;:!&()_'"]+ to a cell in my spreadsheet. The error message I receive is: Missing ) after argument list. I am unsure how to correct the string that inc ...

Utilizing the native cursor feature in Adobe AIR JavaScript using MouseCursorData

I have been exploring the content of this article: which details how to create a native cursor in AIR without resorting to using a sprite to mimic the functionality. However, my project is based on HTML/JavaScript rather than ActionScript. Here is the c ...

Link with javascript onClick attribute

Is there a way to combine a JavaScript "onClick" event with an HTML "a href" link? I would like for the first step to be triggered upon clicking the link using "onClick", and if that is successful, the user should then be redirected to another page using t ...

Unable to interact with elements upon completion of ajax load()

Having trouble executing functions on Ajax-loaded content in a div element. Any suggestions on how to make it work? var key = null; var keyProperties = null; function OnNodeClick(s, e) { key = e.node.name; //alert('key= '+ key); $("# ...

The order of console outputs can be inconsistent and may not always align with the order in which they are called

Hello there! I'm diving into the world of JavaScript and have a query to share on Stackoverflow. If something seems missing in my question, please do point it out for me. Question 1: Can someone shed light on why the output sequence in the console s ...

What is the best way to utilize the GET Method with a hashtag incorporated into the URL?

For instance: www.sample.com#?id=10 Currently, I am not able to retrieve any value from $_GET['id']. Despite my attempt to eliminate the hashtag from the URL using JavaScript, no change occurs: $(document).ready(function(){ $(window.location ...