【问题标题】:Rails 4 Trying to connect CSS attribute to the viewRails 4试图将CSS属性连接到视图
【发布时间】:2015-02-07 16:22:04
【问题描述】:

我在页面上有一个视频网格,点击后会在灯箱中弹出。如果用户将鼠标悬停在网格图像上,我希望显示视频的标题。

现在,在悬停时,我得到了所需的标题背景,但我不知道在哪里/如何将视频标题的 css 属性放在我的 html.erb 中

这是 CSS:

.masonry:hover .item.w2:hover:after {
    content: attr(datatitle);
    display: block;
    text-align: center;
    background-color: #333;
    color: #fff;
    padding: 5px;
    width: 250px;
     height: 40px;
     opacity:0.5;
    position: absolute;
    top: 0px;
}

这是我的看法:

<div class="item w2" ><%= link_to image_tag(Video.find(19).image_url, size: "250x141"), Video.find(19).player_embed_url, class: "fancybox fancybox.iframe"%>

我在哪里放置 datatitle 属性以及如何放置?我想用上面相同的方法提取视频的标题(即 Video.find(19).title)

作为参考,这是我用来制作这个的 jsfiddle:

http://jsfiddle.net/arthurcamara/chbsL3pq/

但显然,我想从 Video 对象中提取 Title,而不是硬编码。

【问题讨论】:

    标签: jquery html css ruby-on-rails


    【解决方案1】:

    您可以添加:

    data: { title: 'title goes here' }
    

    到link_to:

    <%= link_to image_tag(Video.find(19).image_url, size: "250x141"), Video.find(19).player_embed_url, class: "fancybox fancybox.iframe", data: { title: 'title goes here' } %>
    

    【讨论】:

    • 我尝试按照您的建议添加data: {title: 'title goes here'} 以及data: { title: Video.find(19).title },但没有任何反应。感谢您的帮助!
    • 当你添加 data 时,rails 会生成什么 html。你能检查一下浏览器的检查器吗?
    • 在 ERB HTML 中使用 data: {title: 'title goes here'}&lt;div class="masonry js-masonry" data-masonry-options='{ "columnWidth": 50, "gutter": 15 }'&gt; &lt;div class="item w2"&gt;&lt;a class="fancybox fancybox.iframe" data-title="title goes here" href="http://app.ustudio.com/embed/D6rHaGhIFLIV/UlSUJO6watDt"&gt;&lt;img src="https://ce70e1713c6d8fc9cdd0-27f5abc5a0ea24152d882b2f1ce8ed03.ssl.cf1.rackcdn.com/IuQZuq34SGVW.jpg" alt="Iuqzuq34sgvw" width="250" height="141" /&gt;&lt;/a&gt;&lt;/div&gt;
    • 好的,我认为这不是 rails 问题,而是您的 css/html 的问题。我认为使用您当前的 css 确定您需要将数据平铺移动到 item w2 元素:这是一个示例:jsbin.com/dedolereyi/1/edit?html,css,output 还要确保使用 content: attr(data-title) 而不是 content: attr(datatitle)
    • 没关系,明白了! &lt;div class="item w2" data-title = "&lt;%= Video.find(19).title %&gt;"非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多