创建一个全新的fancybox并将内容附加到它。

huangapple go评论76阅读模式
英文:

How to create a fancybox from cero and append a content to it

问题

I'm using the FancyBox library, version: 5, to create modals, and everything works fine when I use the basic approach mentioned in the documentation by adding the "data-fancybox" attribute to an tag. So far, so good. Now I'm trying to create a new modal from zero depending if the session has success or not, and need to add the conten.

I've tried the following code, and it works... partially, it creates an empty modal. I can't seem to add the desired content to it. According to the documentation, I should use the setContent() function and pass it an HTMLElement.

Here's my code:

@if(session('success'))
    <script>
      // Create the div element
      var modalDiv = document.createElement('div');
      modalDiv.id = 'modal';
      // Set the content of the div
      modalDiv.textContent = 'Contenido del Modal';
      // Add the div to the document
      // document.body.appendChild(modalDiv);
      var options = {};
      var slide = {
        "isDom": false,
        "transition": false,
        "src": "#modal",
        "type": "inline",
      };
      var fancy = Fancybox.show(slide, options);
      console.dir(typeof modalDiv)
      console.dir(fancy)
      fancy.setContent(modalDiv)
    </script>
@endif

For some reason, the modalDiv variable is an object. What am I doing wrong?

By the way, I'm in a Laravel project and this code is in a blade file, although I don't think this matters too much.

Thank you in advance.

英文:

I'm using the FancyBox library, version:5, to create modals, and everything works fine when I use the basic approach mentioned in the documentation by adding the "data-fancybox" attribute to an <a> tag. So far, so good. Now I'm trying to create a new modal from zero depending if the session has success or not, and need to add the conten.

I've tried the following code, and it works... partially, it creates an empty modal. I can't seem to add the desired content to it. According to the documentation, I should use the setContent() function and pass it an HTMLElement.

Here's my code:

@if(session(&#39;success&#39;))
    &lt;script&gt;
      // Crear el elemento div
      var modalDiv = document.createElement(&#39;div&#39;);
      modalDiv.id = &#39;modal&#39;;
      // Establecer el contenido del div
      modalDiv.textContent = &#39;Contenido del Modal&#39;;
      // Agregar el div al documento
      // document.body.appendChild(modalDiv);
      var options = {};
      var slide = {
        &quot;isDom&quot;: false,
        &quot;transition&quot;: false,
        &quot;src&quot;: &quot;#modal&quot;,
        &quot;type&quot;: &quot;inline&quot;,
      };
      var fancy = Fancybox.show(slide, options);
      console.dir(typeof modalDiv)
      console.dir(fancy)
      fancy.setContent(modalDiv)
    &lt;/script&gt;
@endif

For some reason, the modalDiv variable is an object. What am I doing wrong?

By the way, I'm in a Laravel project and this code is in a blade file, although I don't think this matters too much.

Thank you in advance.

答案1

得分: 1

以下是如何更改幻灯片内容的示例:

const fancyboxInstance = Fancybox.getInstance();
const currentFancyboxSlide = Fancybox.getSlide();

fancyboxInstance.clearContent(currentFancyboxSlide);
fancyboxInstance.setContent(currentFancyboxSlide, "另一种内容", false);

https://jsfiddle.net/j6hnbd4w/

但在您的情况下,可能只需更改具有id“modal”的元素的内容。

英文:

Here is an example of how to change the content on a slide:

const fancyboxInstance = Fancybox.getInstance();
const currentFancyboxSlide = Fancybox.getSlide(); 

fancyboxInstance.clearContent(currentFancyboxSlide);
fancyboxInstance.setContent(currentFancyboxSlide, &quot;Another content&quot;, false);

https://jsfiddle.net/j6hnbd4w/

But in your case, maybe simply change contents of element having id modal.

huangapple
  • 本文由 发表于 2023年5月17日 18:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271150.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定