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