如何让 TwentyTwenty.js 张图片居中?
How to keep TwentyTwenty.js images in center?
我想使用 TwentyTwenty.js 代码来比较图像。除了图像与左侧对齐外,一切都很好。如何在不明确设置容器宽度的情况下将它们保持在中心位置?
<div id="beforeafter" class="twentytwenty-container">
<img src="https://lorempixel.com/800/300/sports/2/">
<img src="https://lorempixel.com/800/300/sports/3"/>
</div>
$(window).load(function() {
$("#beforeafter").twentytwenty();
});
http://codepen.io/anon/pen/EmEYjQ
如果我在容器上设置固定宽度,图像会居中,但我希望图像不限于宽度,除非它们不比屏幕宽
如果我使用 flex 居中而不是手柄不在中心
display: flex;
justify-content: center;
我也辛苦了!
我花了很多时间在我能找到的所有 CSS 上。
最后...这是一个简单的解决方案:
$(window).load(function() {
// Was there.
$("#beforeafter").twentytwenty();
// Mesure your images and divide by 2.
var imgWidth = $("#beforeafter img").width()/2;
// On the container, apply a left offset of 50% (screen center) - minus half image width.
$("#beforeafter").css({"position":"relative","left":"calc(50% - "+ imgWidth+ "px)"});
});
看看 - Container would take the whole width of the page despite of the image width。
基本上在jquery.twentytwenty.js
的第48行加上:
container.css("height", offset.h);
container.css("max-width", offset.w); // See https://github.com/zurb/twentytwenty/issues/50
我想使用 TwentyTwenty.js 代码来比较图像。除了图像与左侧对齐外,一切都很好。如何在不明确设置容器宽度的情况下将它们保持在中心位置?
<div id="beforeafter" class="twentytwenty-container">
<img src="https://lorempixel.com/800/300/sports/2/">
<img src="https://lorempixel.com/800/300/sports/3"/>
</div>
$(window).load(function() {
$("#beforeafter").twentytwenty();
});
http://codepen.io/anon/pen/EmEYjQ
如果我在容器上设置固定宽度,图像会居中,但我希望图像不限于宽度,除非它们不比屏幕宽
如果我使用 flex 居中而不是手柄不在中心
display: flex;
justify-content: center;
我也辛苦了!
我花了很多时间在我能找到的所有 CSS 上。
最后...这是一个简单的解决方案:
$(window).load(function() {
// Was there.
$("#beforeafter").twentytwenty();
// Mesure your images and divide by 2.
var imgWidth = $("#beforeafter img").width()/2;
// On the container, apply a left offset of 50% (screen center) - minus half image width.
$("#beforeafter").css({"position":"relative","left":"calc(50% - "+ imgWidth+ "px)"});
});
看看 - Container would take the whole width of the page despite of the image width。
基本上在jquery.twentytwenty.js
的第48行加上:
container.css("height", offset.h);
container.css("max-width", offset.w); // See https://github.com/zurb/twentytwenty/issues/50