铁轨和原型 - 插入一个 div 并定位它

发布于 2024-09-10 21:44:09 字数 1530 浏览 5 评论 0原文

对于我的 Rails 站点,当有人尝试删除某个项目时,我希望弹出一个自定义 div(而不是 JavaScript 警报框),要求在执行此操作之前进行确认。

目前,我执行此操作的方式是,单击删除链接将调用一个函数(在 application_helper.rb 中),如下所示:

  def show_delete_box(object, name)
    update_page do |page|
      page.insert_html :before, "content", render(:partial => "shared/generic_delete_box", :locals => { :object => object, :name => name })
      page << "$('delete_box').Center"
    end
  end

渲染的部分旨在出现在屏幕和局部变量将用于在 div 中创建适当的删除链接。

然而,我的问题是试图让它出现在屏幕中间。我一直深受 JavaScript 的困扰,这也让我发疯。 (是的,我也尝试过使用 firebug,但是那些长长的 javascript 行对我来说毫无意义。>.<)

我的 update_page 块中的第二行旨在调用 javascript以插入的 div 为中心的函数。

我编写并粘贴到 application.js 文件中的函数如下:

Element.Center = function(element) {
    var vH, vW, eH, eW;
    var d = Element.getDimensions(element);
    eH = d.height;
    eW = d.width;
    vH = document.viewport.getHeight();
    vW = document.viewport.getWidth();

    var y = (vH/2) - (eH/2) + "px";
    var x = (vW/2) - (eW/2) + "px";
    var style = { position: 'absolute'; top: y + 'px'; left: x + 'px'};

    element.setStyle(style);
}

当我单击时,会创建部分 div,并将其插入到页面的适当位置。然而,据我所知,它并没有以任何方式定位。 所以,我正在寻求帮助!我在 javascript 函数中或尝试调用该函数时做错了什么?

提前致谢!

- 编辑 - 我不确定这是否有帮助...这是 delete_box 的相关 CSS 代码。

#delete_box {
    background-color: green;
    border: 5px solid #999;
    padding: 15px;
    width: 600px;
}

所以 CSS 不会以任何方式影响定位。

For my rails site, when somebody attempts to delete an item, I wish to pop up a custom div (as opposed to a javascript alert box) asking for confirmation before doing so.

At the moment, the way that I am doing this, is that clicking on the delete link will call a function (in application_helper.rb) as below:

  def show_delete_box(object, name)
    update_page do |page|
      page.insert_html :before, "content", render(:partial => "shared/generic_delete_box", :locals => { :object => object, :name => name })
      page << "$('delete_box').Center"
    end
  end

The rendered partial is intended to appear in the middle of the screen, and the locals are what will be used to create the appropriate delete link in the div.

However, my problem here is attempting to make it appear in the middle of the screen. I've always massively suffered with javascript and this is driving me insane as well. (Ya, I've tried to use firebug as well, but those long lines of javascript mean nothing to me. >.< )

The 2nd line in my update_page block is meant to call a javascript function that center's the inserted div.

The function that I've written and stuck into my application.js file is as follows:

Element.Center = function(element) {
    var vH, vW, eH, eW;
    var d = Element.getDimensions(element);
    eH = d.height;
    eW = d.width;
    vH = document.viewport.getHeight();
    vW = document.viewport.getWidth();

    var y = (vH/2) - (eH/2) + "px";
    var x = (vW/2) - (eW/2) + "px";
    var style = { position: 'absolute'; top: y + 'px'; left: x + 'px'};

    element.setStyle(style);
}

What happens when I click, is that the partial div is created, and inserted into the page at the appropriate spot. However it is not being positioned in anyway that I can tell.
So, I'm looking for help! What am I doing wrong in the javascript function, or in attempting to call the function?

Thanks in advance!

--edit--
I'm not sure if this will help... This is the relevant CSS code for delete_box.

#delete_box {
    background-color: green;
    border: 5px solid #999;
    padding: 15px;
    width: 600px;
}

So the CSS doesn't touch the positioning in any way.

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

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

发布评论

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

评论(1

凉宸 2024-09-17 21:44:09

有几个预制的库可以完成您所说的操作,可能比您自己编写更容易。例如,Redbox 是一款在 Prototype 中工作的 Rails 插件。

There are several prebaked libraries out there that do just what you're saying, might be easier than writing it yourself. Redbox is a rails plugin that works in Prototype, for one example.

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