【问题标题】:why doesnt div display at proper coordinates?为什么 div 不在正确的坐标上显示?
【发布时间】:2013-07-22 03:53:40
【问题描述】:

所以我创建了一个 erb 块,它遍历每个图像的坐标,然后在给定坐标处显示每个图像的 div.tagged。在这种特殊情况下,该块会遍历 2 个图像,每个图像显示一次 div.tagged,但是每个图像的 div.tagged 位置是相同的。有人对为什么有任何想法吗?这是HTML is generated

erb 块

<div class="container">
<% if @new_manual.present? %>
<% @new_manual.steps.each do |step| %>
    <% i_connection = Contact.find(step.input_contact) %>
        <span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>"  data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>
        <div class="image_panel"><br>
            <%= image_tag(i_connection.image.image.url(:large)) %>
        <div class='planetmap'></div>
    </div>
    <% end %>   
<% end %>
</div>

jQuery

$(document).ready(function(){
$("span.i_connection").each(function() {
    var pos_width = $(this).data('pos-width');
    var pos_height = $(this).data('pos-height');
    var xpos = $(this).data('pos-x');
    var ypos = $(this).data('pos-y');

    $(".tagged_box").css("display","block");
    $(".tagged").css("border","5px solid red");

        $('.planetmap').append('<div class="tagged"  style="width:'+pos_width+'px;height:'+pos_height+'px;left:'+xpos+'px;top:'+ypos+'px;" ><div class="tagged_box" style="width:'+pos_width+'px;height:'+
            pos_height+'px;" ></div>')
});   //END OF SPAN.CONNECTION ITERATION
}); //END OF JQUERY 

【问题讨论】:

    标签: jquery image iterator coordinates erb


    【解决方案1】:

    你有一些问题。

    1) 您没有使用 CSS position: absolute 在您的“标记”类中。 2)您要声明多个 .planetmap(每个循环一个)并每次都附加内容。

    如果您有意需要许多 .planetmap,请限制您的 jQuery 仅使用类似 $(this).next('.image_panel).find('.planetmap') 之类的东西对本地副本进行操作

    这是 CSS 位置添加的 JS Fiddle:http://jsfiddle.net/ksf3A/ 您的左/右已关闭,它根据您的 HTML 显示多个图像。

    也许你想要:

    <div class="container">
        <div class="image_panel"><br>
             <%= image_tag(i_connection.image.image.url(:large)) %>
            <div class='planetmap'></div>
        </div>
    
    <% if @new_manual.present? %>
    <% @new_manual.steps.each do |step| %>
        <% i_connection = Contact.find(step.input_contact) %>
            <span class="i_connection" data-pos-x="<%= i_connection.pos_x %>" data-pos-y="<%= i_connection.pos_y %>"  data-pos-width="<%= i_connection.pos_width %>" data-pos-height="<%= i_connection.pos_height %>"> </span>
        <% end %>   
    <% end %>
    </div>
    

    相反?我当然假设这是一个设备上的多个连接,而不是具有多个连接的多个设备(您需要一个辅助外环设备)

    【讨论】:

    • 感谢您的反馈!它实际上适用于具有多个连接的多个设备。我以为我需要多个 .planetmaps 因为这是附加 .tagged div 的方式,但这样做似乎会导致第二个显示不正确,嗯?
    • 在这种情况下,您需要创建一个设备循环,然后循环通过多个设备。例如。 foreach device > foreach device_connection > 还有,为什么要用JS来制定html——你可以直接写出来。
    • erb 块循环遍历设备图像并根据用户的选择动态显示它们。我是否也必须在我的 JS 中遍历它们?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多