Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将TwentyTwenty.js图像保持在中心位置?_Jquery_Css_Twentytwenty - Fatal编程技术网

Jquery 如何将TwentyTwenty.js图像保持在中心位置?

Jquery 如何将TwentyTwenty.js图像保持在中心位置?,jquery,css,twentytwenty,Jquery,Css,Twentytwenty,我想使用代码来比较图像。这一切都很好,除了图像对齐到左侧。在不明确设置容器宽度的情况下,如何将它们保持在中心位置 <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>

我想使用代码来比较图像。这一切都很好,除了图像对齐到左侧。在不明确设置容器宽度的情况下,如何将它们保持在中心位置

<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();
});

我也经历了一段艰难的时光
我花了很多时间在所有我能找到的相关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)"});
});

我也经历了一段艰难的时光
我花了很多时间在所有我能找到的相关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)"});
});
看看-

基本上在jquery.twentytwenty.js的第48行中添加:

container.css("height", offset.h);
container.css("max-width", offset.w); // See https://github.com/zurb/twentytwenty/issues/50
看看-

基本上在jquery.twentytwenty.js的第48行中添加:

container.css("height", offset.h);
container.css("max-width", offset.w); // See https://github.com/zurb/twentytwenty/issues/50