简体   繁体   English

如何动态加载模态的内容(Bootstrap 4)

[英]How to load dynamically the content of a Modal (Bootstrap 4)

I'm setting up a web site rich of images . 我正在建立一个图像丰富的网站 I've designed it as a landing page: all content has to be on the main HTML page, without flow in others, actually there is the only index.html . 我将其设计为登录页面:所有内容都必须位于主HTML页面上,而其他内容中没有流量,实际上只有index.html

This will be the official website of a Theater, the images are all related to their shows. 这将是剧院的官方网站,图片都与他们的节目有关。 To estimate the number of images: 要估算图​​像数量:

  • 40 shows an almost 40 pics per show (About 600kb per Image) 40显示每个节目近40张照片(每张图片约600kb)

Almost 1gb of pictures in the same page ! 在同一页面上几乎1GB的图片

Every show is represented with an image and when you click the Show image a Modal is opened showing the related pics in a grid and information about it. 每个节目都用图像表示, 当您单击显示图像时,会打开一个模态,显示网格中的相关图片及其相关信息。

I'm using Bootstrap 4, Jquery and CSS. 我正在使用Bootstrap 4,Jquery和CSS。

The problem is: if I add all the modals (with the pics in it) to HTML page my website get very lazy and doesn't ** load properly**. 问题是:如果我将所有模态(带有图片)添加到HTML页面我的网站变得非常懒惰并且不能正确加载**。

I'm searching a way to load dynamically the pics every time you click on the show , avoiding to load the Dom with ALL the pictures from the first moment. 我正在寻找一种方法来动态加载图片每次你点击节目 ,避免从第一时刻加载Dom与所有图片。

I've tried with Jquery injection , but is very hard to handle when the modal is open and when modal is closed and I don't think that is a proper and strict way to code. 我已尝试使用Jquery注入 ,但是当模态打开并且模态关闭时很难处理,我不认为这是一种正确而严格的代码方式。

I was thinking about a PHP server manipulation of images . 我在考虑PHP服务器操作图像 Php elaborate and minimize the images to make thumbnail so when images are seen in a grid they are not in the full size, and only when images are clicked they shown in full size (with lightbox.js). Php精心制作并最小化图像以制作缩略图,因此当在网格中看到图像时,它们不是完整尺寸,并且仅当点击图像时它们以完整尺寸显示(使用lightbox.js)。 But I'm a complete noob with PHP, and I didn't find an easy tutorial. 但我是一个完整的PHP菜鸟,我没有找到一个简单的教程。

I'm opened to any solution you think I can implement, mostly to those who care about SEO optimization and faster loading time for client ! 我打开了你认为我可以实现的任何解决方案,主要是那些关心SEO优化和更快加载客户端的人

 <!-- MODAL EXAMPLE --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog modal-full " data-dismiss="modal"> <div class="modal-content mx-auto"> <div class="modal-body"> <div class="column mx-auto"> <div class="sidebar-box"> <h2 class="title">SHOW TITLE</h2> <h2 class="subtitle"> SHOW SUBTITLE</h2> <p class="middle">MIDDLE INFORMATION</p> <p class="description">SHOW DESCRIPTION</p> </div> </div> <div class="column mx-auto"> <!-- THERE WILL BE ALL THE IMAGES IN A GRID--> <div class="row justify-content-center"> <a href="stages/alesi/alesi1.JPG" data-title="Fotografia ©" data-lightbox="alesi"> <img src="stages/alesi/alesi1.JPG" alt="" /> </a> <a href="stages/alesi/alesi2.JPG" data-title="Fotografia ©" data-lightbox="alesi"> <img src="stages/alesi/alesi2.JPG" alt="" /> </a> <a href="stages/alesi/alesi3.JPG" data-title="Fotografia ©" data-lightbox="alesi"> <img src="stages/alesi/alesi3.JPG" alt="" /> </a> </div> </div> </div> </div> </div> </div> 

Thank you very much to all stack overflow community! 非常感谢所有堆栈溢出社区!

One way to do it is with AJAX, yeah, you can have the content of each modal into a different HTML/PHP file, and on click on the modal to make an AJAX request, and putting the specific modal content(html file) into the page. 一种方法是使用AJAX,是的,您可以将每个模态的内容放入不同的HTML / PHP文件中,然后单击模式以生成AJAX请求,并将特定模式内容(html文件)放入这一页。

So, basically, you will have the whole <div class="modal-body"> div in a different HTML file, and on modal click you will have something like that 所以,基本上,你将在不同的HTML文件中拥有整个<div class="modal-body"> div,并且在模态点击时你会有类似的东西

$("the_modal").click(function(){
  $.ajax({url: "modal1.html", success: function(result){
    $("#myModal modal-content").append(result);
  }});
});

Of course, you can put data attributes on the modal and use them in the AJAX, so it will work for all the modal across the website. 当然,您可以将数据属性放在模态上并在AJAX中使用它们,因此它适用于整个网站的所有模态。

The other thing that will help a lot is optimizing the images, reducing the size, the quality. 另一件有用的事情是优化图像,减小尺寸和质量。

You can use this link to refer how to load dynamic data in a single bootstrap modal. 您可以使用此链接来引用如何在单个引导模式中加载动态数据。 https://getbootstrap.com/docs/4.3/components/modal/#varying-modal-content https://getbootstrap.com/docs/4.3/components/modal/#varying-modal-content

If you have the show data in database or JSON file, you can then pass the id of the show which is clicked in the data attribute as given in above example. 如果您在数据库或JSON文件中有show数据,则可以传递在上面的示例中给出的data attribute单击的show的id。

Then using modal event show.bs.modal , you can fire an ajax call, which will populate the required data in the .modal-body and on close you can clear the data from the modal. 然后使用模态事件show.bs.modal ,您可以触发ajax调用,该调用将填充.modal-body的所需数据,并且在关闭时您可以清除模态中的数据。

If your main problem is the amount of images that will have to be loaded delaying your page so I suggest that you try to implement images lazy-loading. 如果您的主要问题是必须加载的图像数量延迟页面,所以我建议您尝试实现图像延迟加载。 This is going to enhance the performance of your website since your images will load only when they are shown in the viewport. 这将提高您网站的性能,因为只有在视口中显示图像时才会加载图像。

There is a jQuery plugin you can use for that: jQuery.Lazy() 你可以使用一个jQuery插件: jQuery.Lazy()

I would advise against loading 40pics x 600k by default. 我建议不要默认加载40pics x 600k。 You need to be thinking about mobile users. 您需要考虑移动用户。 I would use a gallery script (I recommend Photo Swipe) and a request to a server-side script returning a json that will be used to create the gallery only on demand. 我会使用一个库脚本(我建议使用Photo Swipe)和一个服务器端脚本的请求,返回一个json,用于仅按需创建库。 Check this page: https://photoswipe.com/documentation/custom-html-in-slides.html 查看此页面: https//photoswipe.com/documentation/custom-html-in-slides.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM