如何将TwentyTwenty.js图像保存在中心?

问题描述 投票:0回答:2

我想用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

如果我在容器上设置固定宽度,则图像位于中心,但我希望图像不限于宽度,除了它们不比屏幕宽

如果我使用柔性定心而不是手柄不在中心

  display: flex;
  justify-content: center;
jquery css twentytwenty
2个回答
4
投票

我也很难过! 我花了很多时间在我能找到的所有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)"});
});

CodePen


0
投票

看看 - 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
© www.soinside.com 2019 - 2024. All rights reserved.