【发布时间】:2023-04-01 06:26:01
【问题描述】:
我已经成功实现了 jcrop 和回形针,通过转到另一个页面(即crop.html.erb)来裁剪图像。但我希望能够在当前页面上进行所有裁剪,在弹出的 div / 对话框中上传图像。所以不知何故,我需要在点击时将crop.html.erb 页面加载到弹出 div 中。这是裁剪页面上的代码
<% content_for :javascript do %>
<%= stylesheet_link_tag "jquery.Jcrop" %>
<%= javascript_include_tag "jquery.Jcrop.min" %>
<% end %>
<script type="text/javascript" charset="utf-8">
$(function() {
$('#cropbox').Jcrop({
onChange: update_crop,
onSelect: update_crop
});
});
function update_crop(coords) {
var rx = 100/coords.w;
var ry = 100/coords.h;
$('#preview').css({
width: Math.round(rx * <%= @photo.photo_geometry(:biggest).width %>) + 'px',
height: Math.round(ry * <%= @photo.photo_geometry(:biggest).height %>) + 'px',
marginLeft: '-' + Math.round(rx * coords.x) + 'px',
marginTop: '-' + Math.round(ry * coords.y) + 'px'
});
var ratio = <%= @photo.photo_geometry(:original).width %> / <%= @photo.photo_geometry(:biggest).width %>;
$("#crop_x").val(Math.round(coords.x * ratio));
$("#crop_y").val(Math.round(coords.y * ratio));
$("#crop_w").val(Math.round(coords.w * ratio));
$("#crop_h").val(Math.round(coords.h * ratio));
}
</script>
<%= image_tag @photo.photo.url(:biggest), :id => "cropbox" %>
<h4>Preview:</h4>
<div style="width:100px; height:100px; overflow:hidden">
<%= image_tag @photo.photo.url(:biggest), :id => "preview" %>
</div>
<% form_for @photo do |f| %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= f.hidden_field attribute, :id => attribute %>
<% end %>
<p><%= f.submit "Crop" %></p>
<% end %>
我可以将它附加到一个 div 或类似的东西上,还是我很远?
顺便说一句,我不只是一次上传一张图片,所以我不能让它进入裁剪页面然后回来。我在页面上的弹出 div 中使用 uploadify 一次上传所有文件,然后希望能够单击图像旁边的裁剪链接。
【问题讨论】:
-
1.删除 Ruby 标签,因为它完全是 Rails 的。 2. 在句首使用大写字母。 3. 使用双倍空格进行换行 4. 不要在帖子中使用签名。请参阅:stackoverflow.com/faq 和 stackoverflow.com/editing-help
标签: javascript jquery ruby-on-rails ajax