【发布时间】:2017-03-23 04:03:45
【问题描述】:
!!!如果有人现在可以回答这个问题,我会等待赏金期结束,直到达到 150 再奖励。 (童子军荣誉!)!!!
我环顾四周,但找不到这个问题的答案:
我从 fb api 获取事件封面图像,并存储 offset_x 和 offset_y 值。然后我将图像放置为 css 背景图像,如下所示:
CSS
.event-image {
margin-bottom: 30px;
width: auto;
width: 500px;
height: 178px;
max-width: 100%;
min-width: 300px;
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
outline: none;
}
高度/宽度基于 facebook 使用的确切比例:2,8:1
HTML
<div class="event-image" style="background-image: url([url of img]); background-position: [offset_x]% [offset_y]%;" >
(我确实有一些内部逻辑,如果 fb api 中有一个设置,则只添加 background-position 属性。)
问题是这只在 90% 的情况下有效。大约 10% 的图像略微未对齐。通常只有几个百分点:-(
这是一个例子。
.event-image {
margin-bottom: 30px;
width: auto;
width: 500px;
height: 178px;
max-width: 100%;
min-width: 300px;
background-size: cover;
background-position: center center;
background-repeat: no-repeat;
outline: none;
}
<div class="event-image" style="background-image: url(https://scontent.xx.fbcdn.net/t31.0-0/p180x540/14566476_1295215523823549_1334771895810946224_o.jpg); background-position: 0% 50%; "> </div>
<!-- Offsets are taken directly from API -->
现在是actual event
您可以看到实际上偏移量在 46% 时是完美的 - 那么为什么 fb 给出 50%?
我能找到的最多信息是pixel calculations,但考虑到我使用的是百分比,这没什么用。
编辑
实现elfan解决方案的新问题:
Here is an event 在 fb 上,其中图像在 -7px 的实际 fb 页面中有 offset_x - 但根据 api,offset_x = 50%
{
"cover": {
"offset_x": 50,
"offset_y": 0,
"source": "https://scontent.xx.fbcdn.net/t31.0-8/s720x720/14681104_1307094859330152_7540791723420117074_o.jpg",
"id": "1307094859330152"
},
"id": "544220282434185"
}
所以,使用计算500px (width of my image) * offset_x % = 250px
我做错了什么:-)
我还注意到有些事件具有疯狂的偏移量,例如 1800。根据 fb docs,它应该在 0-100 之间。
【问题讨论】:
标签: facebook css facebook-graph-api background-image background-position