【问题标题】:how to load pictures only when clicked and not before如何仅在单击而不是之前加载图片
【发布时间】:2012-03-16 01:35:59
【问题描述】:

我编写了一个漂亮的图库,点击的图片覆盖在整个网页上。 问题是,所有的图片和缩略图都是立即加载的,我希望只加载大图片,当女人按下相应的缩略图时。 我没有 ajax 经验,虽然我试图用 :remote => true 做一些事情,但没有成功。有人可以帮忙吗?

<% for painting in @gallery.paintings %>
  <div class="painting">
  <%= image_tag painting.image_url(:thumb), :remote => true, :rel => "##{painting.id}" %>

    <div class="overlay" id="<%= painting.id %>" >
      <%= image_tag painting.image_url %>
    </div>
<% end %>

还有我的 javascript coffe-script 文件

$(document).ready ->
  $("img[rel]").overlay();
  $(".close").click ->
    $("#dim").fadeOut()
    false
  $("div.painting img").click ->
    $("#dim").fadeIn()
    false
  $(".overlay").css "width", $(window).width()
  $("#dim").css "width", $(window).width()

$(window).bind "resize", ->
  $(".overlay").css "width", $(window).width()
  $("#dim").css "width", $(window).width()

编辑 2012/3/24:

我使用以下方法更改了存储库:

  gem 'fancybox-rails', :git => 'https://github.com/sverigemeny/fancybox-rails'

开箱即用,看起来很棒。

【问题讨论】:

    标签: javascript ajax ruby-on-rails-3 coffeescript overlay


    【解决方案1】:

    您无需了解 ajax 即可执行此操作,因为您只需在现有图像上设置 source 属性或创建图像元素即可。现在,如果您想从您的服务器检索 html 或 url 或其他内容,那么您将需要了解 ajax。让我们知道您是否正在这样做。

    http://jsfiddle.net/mrtsherman/nzdpt/1/

    <button id="a">Create element</button>
    <button id="b">Swap src on img</button>
    <img id="c" src="http://placehold.it/150x150/ff0000" />​
    
    //create a new image element and append it to dom
    $('#a').click(function() {
        var $img = $('<img />', {
                 id: 'foobar ',
                 src : 'http://placehold.it/150x150'
        });
        $('body').append($img);
    });
    
    //get an existing img element and change its source 
    $('#b').click( function() {
        $('#c').prop('src', 'http://placehold.it/100x100');
    });
    ​
    

    【讨论】:

    • 哦,我明白了我的错误,但没有更简单的方法,所以我可以像这样离开我的结构,然后告诉服务器加载这个 div:
      当我点击拇指时
    • @NewYearsEve - 看看 jQuery 的 load 方法。这将检索 url 的内容并将其填充到匹配的选择器中。 api.jquery.com/load
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多