如何手动设置 jquery-lightbox 的活动图像?

发布于 2024-11-06 10:34:55 字数 1761 浏览 4 评论 0原文

我正在尝试使用 jquery-lightbox 在 Ruby on Rails 中创建照片查看器。您可以在下图中看到它的设计。

您单击较小的图像以使所选图像出现在上面的大空间中。如果单击大图像,您可以按照原始上传的分辨率查看所有图像的灯箱幻灯片。

到目前为止,我已经完成了所有这些工作,但是我需要灯箱从当前选定的图像开始,而不是总是从第一个图像开始。我尝试调用changeImage函数(返回TypeError)并更改activeImage属性(不执行任何操作)。

这怎么能做到呢?

这是我正在使用的代码,目前全部位于我的 show.html.erb 中:

<a href="<%= @actor.avatar.url(:large) %>" onclick="javascript:setActiveImage();" class="lightbox" title="<%= @actor.name %>" rel="actor"><%= image_tag @actor.avatar.url(:slide), :id => "actor_headshot", :alt => @actor.name %></a>

<% @actor.photos.each do |photo| %>
  <a href="<%= photo.photo.url(:large) %>" class="lightbox" rel="actor"></a>
<% end %>

<div id="actor_photos">
  <div id="actor_photo_list">
  <% @photos.each_with_index.map do |photo, index| %>
      <a href="#" onclick="javascript:swapHeadshot('<%= photo.url(:slide) %>', <%= index %>);"><%= image_tag photo.url(:thumb), :class => "actor_photo_list_item" %></a>
    <% end %>
  </div>
</div>

<script language="JavaScript">
  $('.lightbox').lightbox();

  var _am = 0;

  function swapHeadshot(source, index)
  {
    $('#actor_headshot').attr('src', source);
    _am = index;
  }

  function setActiveImage()
  {
    $('.lightbox').lightbox.activeImage = _am; // has no effect
    //$('.lightbox').lightbox({activeImage:_am}); // has no effect
    //$('.lightbox').lightbox.changeImage(_am); // returns TypeError
  }
</script>

Photo Viewer

I am trying to create a photo viewer in Ruby on Rails using jquery-lightbox. You can see the design of it in the image below.

You click on the smaller images to have the selected image appear in the large space above. If you click the large image, you can view a lightbox slideshow of all the images at their originally uploaded resolution.

I have all of this working so far, but I need the lightbox to start at the currently selected image, rather than always starting at the first image. I've tried calling the changeImage function (returns TypeError) and changing the activeImage property (does nothing).

How can this be done?

Here is the code I'm using, which is currently all in my show.html.erb:

<a href="<%= @actor.avatar.url(:large) %>" onclick="javascript:setActiveImage();" class="lightbox" title="<%= @actor.name %>" rel="actor"><%= image_tag @actor.avatar.url(:slide), :id => "actor_headshot", :alt => @actor.name %></a>

<% @actor.photos.each do |photo| %>
  <a href="<%= photo.photo.url(:large) %>" class="lightbox" rel="actor"></a>
<% end %>

<div id="actor_photos">
  <div id="actor_photo_list">
  <% @photos.each_with_index.map do |photo, index| %>
      <a href="#" onclick="javascript:swapHeadshot('<%= photo.url(:slide) %>', <%= index %>);"><%= image_tag photo.url(:thumb), :class => "actor_photo_list_item" %></a>
    <% end %>
  </div>
</div>

<script language="JavaScript">
  $('.lightbox').lightbox();

  var _am = 0;

  function swapHeadshot(source, index)
  {
    $('#actor_headshot').attr('src', source);
    _am = index;
  }

  function setActiveImage()
  {
    $('.lightbox').lightbox.activeImage = _am; // has no effect
    //$('.lightbox').lightbox({activeImage:_am}); // has no effect
    //$('.lightbox').lightbox.changeImage(_am); // returns TypeError
  }
</script>

Photo viewer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小…红帽 2024-11-13 10:34:56

changeImage 听起来像一个函数的名称,但你的代码是:

$('.lightbox').lightbox.changeImage =(_am); //tries to change a function variable to a number

你确定它不应该是:

$('.lightbox').lightbox.changeImage(_am); //calls the function passing a variable

changeImage sounds like the name of a function, but your code is:

$('.lightbox').lightbox.changeImage =(_am); //tries to change a function variable to a number

Are you sure it shouldn't be:

$('.lightbox').lightbox.changeImage(_am); //calls the function passing a variable
无语# 2024-11-13 10:34:55

默认情况下,jQuery Lightbox 带有上一个/下一个按钮。您可能想查看该代码并根据其中一个创建一个函数,因为它们很可能会为您提供答案。

代码里面写着:

$('#lightboxImage').attr('src', opts.imageArray[opts.activeImage][0])width(newWidth).height(newHeight);
resizeImageContainer(newWidth, newHeight);

我认为这就是您正在寻找的代码。

// 编辑:犯了与上面相同的错误,只不过他已经给出了答案。

By default jQuery Lightbox comes with prev/next buttons. You might wanna take a look into that code an create a function based on either one of them since they most likely will provide you an answer.

Inside the code it says:

$('#lightboxImage').attr('src', opts.imageArray[opts.activeImage][0])width(newWidth).height(newHeight);
resizeImageContainer(newWidth, newHeight);

I think that's the code you're looking for.

// Edit: made the same mistake as the one above, except he's given an answer already.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文