简体   繁体   English

Rails:如何在模态内渲染平滑滑块

[英]Rails: How to render slick slider inside modal

I have a partial which is being rendered multiple times, and each partial has a Foundation modal. 我有一个被多次渲染的局部,每个局部都有一个Foundation模态。 Inside this modal I want to use slick slider. 在此模态内,我想使用光滑的滑块。 As the element is not shown when slick is being loaded the images are not shown. 由于在加载浮雕时未显示该元素,因此未显示图像。 If I resize the window the picture is displayed. 如果我调整窗口大小,则会显示图片。

There are many issues like this across the internet (mainly with tabs) but non of their fixes helped me. 互联网上有很多类似的问题(主要是选项卡),但没有一个修复程序对我有帮助。

My code looks something like this: 我的代码如下所示:

<% @names.each do |n| %>
  <div class="name-card" id="name-<%= n.id %>">
    <%= render partial: 'name_card', locals: {n: n} %>
  </div>
  <div id="name-modal-<%= n.id %>" class="reveal-modal" data-reveal aria-labelledby="modalTitle" aria-hidden="true" role="dialog">
    <%= render partial: 'name_modal', locals: {n: n} %>
  </div>
<% end %>

_name_card.html.erb

<p> n.name </p>
<p> n.surname </p>
<a href="#" data-reveal-id="name-modal-<%= n.id %>"> OPEN </a>

_name_modal.html.erb

<div id="name-modal-image-<%=n.id%>">
  <%= image_tag "selfie.jpg" %>
  <%= image_tag "portrait.jpg" %>
</div>

<script>
  $("div[id^=name-modal-image-]").not('.slick-initialized').slick();
</script>

I tried various suggestions I found: 我尝试了各种建议:

var selector = $("div[id^=box-modal-image-]");


selector[0].slick.setPosition();  //option1

$('.reveal-modal').on('opened', function() {
    $(selector).slick("setPosition", 0);
});      //option2

$(window).trigger(‘resize’);   //option3

window.dispatchEvent(new Event(‘resize’));   //option4

But none of them worked. 但是他们都不起作用。 Does anyone have any other suggestion how to overcome this problem? 有谁有其他建议如何克服这个问题? Thanks! 谢谢!

Foundation Sites uses the event open.zf.reveal for reveals. Foundation Sites使用事件open.zf.reveal进行揭示。

https://foundation.zurb.com/sites/docs/reveal.html#javascript-reference https://foundation.zurb.com/sites/docs/reveal.html#javascript-reference

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

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